JAVA生成二维码总结。

一丶添加依赖

<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.3</version>
</dependency>

二丶二维码生成方式

  1. MultiFormatWriter(生成二维码)
  2. AztecWriter(生成 Aztec码)
  3. DataMatrixWriter(DataMatrix二维码)
  4. OneDimensionalCodeWriter
  5. PDF417Writer(生成条形码)
  6. QRCodeWriter(生成二维码)
  7. UPCAWriter(生成UPCA码)

三丶生成二维码实现Dome

package com.zpf.homeworkBy18.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.aztec.AztecWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.datamatrix.DataMatrixWriter;
import com.google.zxing.oned.OneDimensionalCodeWriter;
import com.google.zxing.oned.UPCAWriter;
import com.google.zxing.pdf417.PDF417Writer;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;

public class TwoImgUtil {

    private static final String QR_CODE_IMAGE_PATH = "static\\images\\two.jpg";

    private static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
        MultiFormatWriter qrCodeWriter = new MultiFormatWriter();
        HashMap hints = new HashMap(16);
        // 指定要使用的纠错程度,例如在二维码中。
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 指定字符编码
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // 指定生成条形码时要使用的边距(以像素为单位)。
        hints.put(EncodeHintType.MARGIN, 1);

        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints);

        Path path = FileSystems.getDefault().getPath(filePath);
        System.out.println(path.getFileName().toString());

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        //自己用java的画图画
//        for (int x = 0; x < width; x++) {
//            for (int y = 0; y < height; y++) {
//                // true就是黑色,false就是白色
//                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
//            }
//        }
//        ImageIO.write(image, "png", new File(QR_CODE_IMAGE_PATH));
        //MatrixToImageWriter:提供了二维码的输出方式
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG",path );


    }

    public static void main(String[] args) {
        try {
            generateQRCodeImage("小黑同学!", 350, 350, QR_CODE_IMAGE_PATH);
        } catch (WriterException e) {
            System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
        }

    }
}

四丶“扫描”二维码

/**
     * 解码,将二维码里的信息解码出来
     * @param path
     * @return
     * @throws Exception
     */
    public static String decode(String path) throws Exception {
        File file = new File(path);
        BufferedImage image;
        image = ImageIO.read(file);
        if (image == null) {
            return null;
        }
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result;
        Hashtable hints = new Hashtable();
        hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
        result = new MultiFormatReader().decode(bitmap, hints);
        String resultStr = result.getText();
        return resultStr;
    }

总结:用java生成二维码比较简单,熟悉API即可。码的实现其实就是加解密的一个过程。码其实就是加密对象留下的线索。在解码的时候通过线索解码即可。

上一篇:Java 生成二维码实战


下一篇:超好用的条形码/二维码识别Python包pyzxing