Java生成和解析二维码

相关依赖

本文二维码生成采用Google的Zxing相关依赖进行生成。

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

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

介绍

本文为本人使用Java进行I/O练习时所编写的工具类,该工具类有两个方法一个为生成二维码一个是将二维码的内容解析,详细代码内容如下。

生成二维码

public static Path createQrFile(String content, int qrWidth, int qrHeight) throws WriterException, IOException {

        Hashtable<EncodeHintType, Object> hints = new Hashtable();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrWidth, qrHeight, hints);

        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }

        File directory = new File(createFilePath);

        if (!directory.exists()){
            directory.mkdirs();
        }


        File file = new File(createFilePath + "/" + new Date().getTime() + ".png");

        if (!file.exists()) {
            file.createNewFile();
        }

        ImageIO.write(image, "png", file);

        return file.toPath();

    }

解析二维码

public static String qrToContent(File file){

        BufferedImage image = null;
        try {
            image = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("File read failure");
        }

        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
        HybridBinarizer hybridBinarizer = new HybridBinarizer(source);
        BinaryBitmap bitmap = new BinaryBitmap(hybridBinarizer);

        MultiFormatReader multiFormatReader = new MultiFormatReader();
        Result result;
        try {
            result = multiFormatReader.decode(bitmap);
            return result.getText();
        } catch (NotFoundException e) {
            throw new RuntimeException("Qr code cannot be decoded");
        }
    }

工具类全部代码

package utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
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.Path;
import java.util.Date;
import java.util.Hashtable;

public class QrAnalysisUtils {

    private static String createFilePath = "D:/createFile";

    public static String qrToContent(File file){

        BufferedImage image = null;
        try {
            image = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("File read failure");
        }

        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
        HybridBinarizer hybridBinarizer = new HybridBinarizer(source);
        BinaryBitmap bitmap = new BinaryBitmap(hybridBinarizer);

        MultiFormatReader multiFormatReader = new MultiFormatReader();
        Result result;
        try {
            result = multiFormatReader.decode(bitmap);
            return result.getText();
        } catch (NotFoundException e) {
            throw new RuntimeException("Qr code cannot be decoded");
        }
    }

    public static Path createQrFile(String content, int qrWidth, int qrHeight) throws WriterException, IOException {

        Hashtable<EncodeHintType, Object> hints = new Hashtable();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrWidth, qrHeight, hints);

        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }

        File directory = new File(createFilePath);

        if (!directory.exists()){
            directory.mkdirs();
        }


        File file = new File(createFilePath + "/" + new Date().getTime() + ".png");

        if (!file.exists()) {
            file.createNewFile();
        }

        ImageIO.write(image, "png", file);

        return file.toPath();

    }

}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是刘小猿呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值