二维码生成及读取

一. maven依赖

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

二. 开始码代码

package QRCode;

import com.google.zxing.*;
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.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Author Li Kun
 * @date 12/15 0015 8:27
 */
public class ZxingQRCode {

    public static void createQrCode(int width, int height, String content){
        //设置参数
        Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN,1);

        //生成二维码
        try{
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height,hints);
            Path path2 = Paths.get("D:/"+ UUID.randomUUID()+".jpg");
            MatrixToImageWriter.writeToPath(bitMatrix,"jpg",path2);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void createLogoQrCode(int width, int height, String content){
        Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.MARGIN,1);

        try{
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height);

            File qrFile = new File("D:/QRCode/"+UUID.randomUUID()+".png");

            File logoFile = new File("D:/QRCode/jack.li.png");

            MatrixToImageWriter.writeToPath(bitMatrix,"png", qrFile.toPath());

            addLogo(qrFile, logoFile);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void addLogo(File qrFile, File logoFile){
        if(!qrFile.isFile() || !logoFile.isFile()){
            System.out.println("File is not exist");
            return;
        }
        try{
            BufferedImage image = ImageIO.read(qrFile);
            Graphics2D graphics2D = image.createGraphics();

            BufferedImage logo = ImageIO.read(logoFile);

            int widthLogo = image.getWidth() / 6;
            int heightLogo = image.getHeight() / 6;

            int x = (image.getWidth() - widthLogo) / 2;
            int y = (image.getHeight() - heightLogo) / 2;

            graphics2D.drawImage(logo, x, y, widthLogo, heightLogo, null);
            graphics2D.drawRoundRect(x, y, widthLogo, heightLogo, 10, 10);
            graphics2D.setStroke(new BasicStroke(2));
            graphics2D.setColor(Color.BLUE);
            graphics2D.drawRect(x, y, widthLogo, heightLogo);
            graphics2D.dispose();

            ImageIO.write(image, "png", new File("D:/QRCode/newPic.png"));

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static Result readQrCode(String filePath){
        Result result = null;
        try{
            MultiFormatReader formatReader = new MultiFormatReader();
            File qrCode = new File(filePath);

            BufferedImage image = ImageIO.read(qrCode);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
            HashMap hints = new HashMap();
            hints.put(DecodeHintType.CHARACTER_SET,"utf-8");
            result = formatReader.decode(bitmap, hints);

        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args){
        //createQrCode(500,500,"Hello Jack");
        //createLogoQrCode(500,500,"Hello Jack");
        //createLogoQrCode(300,300,"https://www.baidu.com/");
        readQrCode("D:/QRCode/newPic.png");
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值