ZXing生成二维码

pom.xml

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


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

具体事务代码

package cn.silica.Utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
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.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import static com.google.zxing.client.j2se.MatrixToImageConfig.BLACK;
import static com.google.zxing.client.j2se.MatrixToImageConfig.WHITE;

/**
 * 二维码生成及解析工具
 */
public class QRCodeUtil {
    /**
     * 生成无中间log的二维码
     * @param contexts  二位中内容
     * @param qrWidth 二维码宽度
     * @param qrHeight 二维码长度
     * @param outputStream 输出流
     */
    public static void createNoLogQRCode(String contexts, int qrWidth, int qrHeight ,OutputStream outputStream){
        Map<EncodeHintType,Object> hints = new HashMap<EncodeHintType, Object>();
        //设置二维码校错级别 LMQH  7%,15%,25%,30%表示二维码被图形被污染时可自动复原的比例?
        //同时纠错级别越高,纠错信息占用的空间就越多,能存储的有用信息就越少
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        //设置编码格式
        hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
        //设置二维码边界大小1,2,3,4 默认为4,最大
        hints.put(EncodeHintType.MARGIN,1);
        try {
            //MultiFormatWriter 二维码格式化输出
            //MultiFormatWriter
            BitMatrix bitMatrix = (new MultiFormatWriter()).encode(contexts, BarcodeFormat.QR_CODE,qrWidth,qrHeight,hints);
            //二维码转换为图像
            MatrixToImageWriter.writeToStream(bitMatrix,"png",outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成带logo的二维码
     * @param contexts 二维码中的内容
     * @param qrWidth 二维码的宽度
     * @param qrHeight 二维码的高度
     * @param outputStream 二维码输出地
     * @param imgWidth 二维码中logo的宽度
     * @param imgHeight 二维码中logo的高度
     * @param imgPath 二维码中logo的原地址
     * @throws IOException
     */
    public static void createLogQRCode(String contexts, int qrWidth, int qrHeight ,OutputStream outputStream,int imgWidth,int imgHeight,String imgPath) throws IOException {
        Map<EncodeHintType,Object> hints = new HashMap<EncodeHintType, Object>();
        //设置二维码校错级别 LMQH  7%,15%,25%,30%表示二维码被图形被污染时可自动复原的比例?
        //同时纠错级别越高,纠错信息占用的空间就越多,能存储的有用信息就越少
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        //设置编码格式
        hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
        //设置二维码边界大小1,2,3,4 默认为4,最大
        hints.put(EncodeHintType.MARGIN,1);
        try {
            //MultiFormatWriter 二维码格式化输出
            BitMatrix bitMatrix = (new MultiFormatWriter()).encode(contexts, BarcodeFormat.QR_CODE,qrWidth,qrHeight,hints);
            //获取二维码高度
            qrWidth = bitMatrix.getWidth();
            qrHeight = bitMatrix.getHeight();

            //创建缓冲流
            BufferedImage bufferedImage = new BufferedImage(qrWidth, qrHeight, BufferedImage.TYPE_INT_RGB);

            //将二维码放入缓冲流
            for (int i = 0; i < qrWidth; i++) {
                for (int j = 0; j < qrHeight; j++) {
                    // 循环将二维码内容写入图片
                    bufferedImage.setRGB(i, j, bitMatrix.get(i, j) ? BLACK : WHITE);
                }
            }

            //写入logo图像
            File logoFile = new File(imgPath);
            Graphics2D gs2 = bufferedImage.createGraphics();//该对象可以绘制BufferedImage对象
            BufferedImage logoImg = ImageIO.read(logoFile);//将图片写入到BufferedImage对象中
            //如果logo长宽超过二维码长宽的20%则设定其长宽为二维码的20%,如果小于20%则以logo实际长宽为准
            int logoWidth = logoImg.getWidth()>bufferedImage.getWidth()*2/10 ? bufferedImage.getWidth()*2/10 : logoImg.getWidth();
            int logoHeight = logoImg.getHeight()>bufferedImage.getHeight()*2/10 ? bufferedImage.getHeight()*2/10 : logoImg.getHeight();

            //x,y为logo相对于二维码图片的位置
            int x = (qrWidth - logoWidth) / 2;
            int y = (qrHeight - logoHeight) / 2;

            //开始绘制logo图片
            gs2.drawImage(logoImg,x,y,logoWidth,logoHeight,null);
            //生成一个可设置圆角的矩形
            gs2.drawRoundRect(x,y,logoWidth,logoHeight,0,0);
            //边框宽度
            gs2.setStroke(new BasicStroke(3));
            gs2.setColor(Color.green);//设置背景颜色
            //生成特定矩形的轮廓
            gs2.drawRect(x,y,logoWidth,logoHeight);
            gs2.dispose();//处理图像内容并释放资源,此后gs2将不能再被调用
            logoImg.flush();
            bufferedImage.flush();
            //将二维码返回到页面
            ImageIO.write(bufferedImage,"png",outputStream);

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

    }
}

参考:

https://blog.csdn.net/rongku/article/details/51872156


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值