Java - 生成二维码图片

生成二维码图片

新建 Maven Project,引入依赖:

<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>

新建 QrCodeUtils 工具类:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
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.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeWriter;

public class QrCodeUtils {
    
    private static final BarcodeFormat BARCODE_FORMAT = BarcodeFormat.QR_CODE;
    
    private static final QRCodeWriter qrCodeWriter = new QRCodeWriter();
    
    private static final MultiFormatReader multiFormatReader = new MultiFormatReader();
    

    public static BitMatrix encode(String contents, int width, int height, Map<EncodeHintType, Object> hints) throws WriterException {
        BitMatrix bitMatrix = qrCodeWriter.encode(contents, BARCODE_FORMAT, width, height, hints);
        return bitMatrix;
    }
    
    public static Result decode(File input) throws IOException, NotFoundException {
        BufferedImage bufferedImage = ImageIO.read(input);
        
        LuminanceSource luminanceSource = new BufferedImageLuminanceSource(bufferedImage);
        
        Binarizer binarizer = new HybridBinarizer(luminanceSource);
        
        BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
        
        Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        
        return multiFormatReader.decode(binaryBitmap, hints);
    }
    
    public static void writeToPah(BitMatrix matrix, String format, File file) throws IOException {
        MatrixToImageWriter.writeToPath(matrix, format, file.toPath());
    }
    
    public static void writeToPah(BitMatrix matrix, String format, Path file) throws IOException {
        MatrixToImageWriter.writeToPath(matrix, format, file);
    }
    
    public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
        MatrixToImageWriter.writeToStream(matrix, format, stream);
    }
}

测试类:

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.google.zxing.EncodeHintType;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class Test {

    public static void main(String[] args) throws WriterException, IOException, NotFoundException {
        File file = new File("二维码(QrCode).png");
        String contents = file.getName();
        
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.MARGIN, 1);
        
        BitMatrix bitMatrix = QrCodeUtils.encode(contents, 800, 800, hints);
        
        QrCodeUtils.writeToPah(bitMatrix, "PNG", file); // 生成二维码图片
        
        
//        ByteArrayOutputStream stream = new ByteArrayOutputStream();
//        QrCodeUtils.writeToStream(bitMatrix, "PNG", stream);
//        
//        byte[] byteArray = stream.toByteArray();
        
        Result result = QrCodeUtils.decode(file); // 识别二维码
        
        System.out.println(result.getText());
    }
}

生成的二维码图片位于项目根目录中:

在这里插入图片描述

控制台输出:

二维码(QrCode).png

参考

Java实现二维码生成 Google-Zxing

Java识别二维码【工具类】

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值