使用java解析和制作二维码

项目结构

文件源码 QR.zip

第一步:导入zxing的两个架包 core.jarjavase.jar

第二步:使用工具类 MatrixToImageWriter.java

 1 package util;
 2 import com.google.zxing.common.BitMatrix;
 3 import javax.imageio.ImageIO;
 4 import java.io.File;
 5 import java.io.OutputStream;
 6 import java.io.IOException;
 7 import java.awt.image.BufferedImage;
 8 public final class MatrixToImageWriter {
 9     private static final int BLACK = 0xFF000000;
10     private static final int WHITE = 0xFFFFFFFF;
11     private MatrixToImageWriter() {
12     }
13     public static BufferedImage toBufferedImage(BitMatrix matrix) {
14         int width = matrix.getWidth();
15         int height = matrix.getHeight();
16         BufferedImage image = new BufferedImage(width, height,
17                 BufferedImage.TYPE_INT_RGB);
18         for (int x = 0; x < width; x++) {
19             for (int y = 0; y < height; y++) {
20                 image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
21             }
22         }
23         return image;
24     }
25     public static void writeToFile(BitMatrix matrix, String format, File file)
26             throws IOException {
27         BufferedImage image = toBufferedImage(matrix);
28         if (!ImageIO.write(image, format, file)) {
29             throw new IOException("Could not write an image of format "
30                     + format + " to " + file);
31         }
32     }
33     public static void writeToStream(BitMatrix matrix, String format,
34             OutputStream stream) throws IOException {
35         BufferedImage image = toBufferedImage(matrix);
36         if (!ImageIO.write(image, format, stream)) {
37             throw new IOException("Could not write an image of format "
38                     + format);
39         }
40     }
41 }

第三步:创建二维码

package demo;
import java.io.File;
import java.util.Hashtable;
import util.MatrixToImageWriter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
/**
 *将文字转换为二维码
 */
public class Encoder {
    public static void main(String[] args) throws Exception {
        String text = "http://www.tfgzs.com";
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        // 内容所使用编码
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text,BarcodeFormat.QR_CODE, 300, 300, hints);
        // 生成二维码
        MatrixToImageWriter.writeToFile(bitMatrix, "gif", new File("E:/123/new.gif"));
        System.out.println("二维码生成完成");
    }
}

第四步:解析二维码

package demo;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
/**
 * 将二维码解析为文字
 */
public class Decoder {
    public static void main(String[] args) {
        BufferedImage bufferedImage = null;
        try {
            bufferedImage = ImageIO.read(new File("E://123/qrcodeImage.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
        hints.put(DecodeHintType.CHARACTER_SET, "GBK");
        Result result = null;
        try {
            result = new MultiFormatReader().decode(bitmap, hints);
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
        System.out.println(result.toString());
    }
}
原文地址:https://www.cnblogs.com/tfgzs/p/3833269.html