JAVA實現(xiàn)二維碼制作和讀取

項目結(jié)構(gòu):


image.png

pom.xml代碼:

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.0</version>
        </dependency>
    </dependencies>

二維碼制作和解析:

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ZxingCode {
    //二維碼顏色
    private static final int BLACK = 0xFF000000;
    //二維碼顏色
    private static final int WHITE = 0xFFFFFFFF;

    /**
     * ZXing方式生成二維碼
     * @param text    二維碼內(nèi)容
     * @param width    二維碼寬
     * @param height    二維碼高
     * @param outPutPath    二維碼生成保存路徑
     * @param imageType        二維碼生成格式
     */
    public void zxingCodeCreate(String text, int width, int height, String outPutPath, String imageType) {
        Map<EncodeHintType, String> his = new HashMap<EncodeHintType, String>();
        //設(shè)置編碼字符集
        his.put(EncodeHintType.CHARACTER_SET, "utf-8");
        try {
            //1、生成二維碼
            BitMatrix encode = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, his);

            //2、獲取二維碼寬高
            int codeWidth = encode.getWidth();
            int codeHeight = encode.getHeight();

            //3、將二維碼放入緩沖流
            BufferedImage image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB);
            for (int i = 0; i < codeWidth; i++) {
                for (int j = 0; j < codeHeight; j++) {
                    //4、循環(huán)將二維碼內(nèi)容定入圖片
                    image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE);
                }
            }
            File outPutImage = new File(outPutPath);
            //如果圖片不存在創(chuàng)建圖片
            if(!outPutImage.exists()){
                outPutImage.createNewFile();
            }

            //5、將二維碼寫入圖片
            ImageIO.write(image, imageType, outPutImage);

        } catch (WriterException e) {
            e.printStackTrace();
            System.out.println("二維碼生成失敗");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("生成二維碼圖片失敗");
        }
    }

    /**
     * 二維碼解析
     * @param analyzePath    二維碼路徑
     * @return
     * @throws IOException
     */
    public Object zxingCodeAnalyze(String analyzePath) throws Exception{
        MultiFormatReader formatReader = new MultiFormatReader();
        Object result = null;
        try {
            File file = new File(analyzePath);
            if (!file.exists())
            {
                return "二維碼不存在";
            }
            BufferedImage image = ImageIO.read(file);
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            Map hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            result = formatReader.decode(binaryBitmap, hints);
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
        return result;
    }
}

功能測試:

public class Test {
    public static void main(String[] args){
        //測試制作二維碼
        String text = "wzf制作的二維碼!";
        String outputPath = "E://wzf.jpg";
        ZxingCode zc = new ZxingCode();
        zc.zxingCodeCreate(text,300,300,outputPath,"jpg");

        //測試解析二維碼
        try {
            Object object = zc.zxingCodeAnalyze(outputPath);
            System.out.println(object.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,062評論 25 709
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評論 19 139
  • 寫一個簡單的表達式匹配,這題我想了好久用非遞歸的方式來做,最后失敗了。問題點是需要決策.和x出現(xiàn)的時候是否應(yīng)該匹配...
    KedAyA閱讀 186評論 0 0
  • #白馬聲慢,我自手書# 我們的故事,源于校園,止于哪里一切還是未知數(shù)。 我一直認為 男女之間并沒有...
    喃nnyy閱讀 235評論 0 0
  • 昨晚我的來訪說:“老師,我現(xiàn)在開始跑步了,跑半小時,剛開始前十分鐘還可以,中間十分鐘很想停下來,后十分鐘就...
    溫馨草閱讀 346評論 2 3

友情鏈接更多精彩內(nèi)容