zoukankan      html  css  js  c++  java
  • 基于zxing工具生成二维码

    基于zxing生成二维码

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

    工具:

    import com.google.zxing.BarcodeFormat;
    import com.google.zxing.EncodeHintType;
    import com.google.zxing.WriterException;
    import com.google.zxing.client.j2se.MatrixToImageWriter;
    import com.google.zxing.common.BitMatrix;
    import com.google.zxing.qrcode.QRCodeWriter;
    import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
    
    import java.io.ByteArrayOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Hashtable;
    
    public class QRCodeUtil {
    
        public static byte[] createQRCodeImage(String content, int width, int height) throws WriterException, IOException {
            Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);  // 设置纠错等级L/M/Q/H,纠错等级越高越不易识别,当前设置等级为最高等级H
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码
            hints.put(EncodeHintType.MARGIN, 10); // 边距大小
    
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
            MatrixToImageWriter.writeToStream(bitMatrix, "JPG", pngOutputStream);
            byte[] jpgData = pngOutputStream.toByteArray();
            return jpgData;
        }
    
        public static void main(String[] args) throws IOException, WriterException {
            FileOutputStream outputStream = new FileOutputStream("D:\\setup\\set.jpg");
            outputStream.write(createQRCodeImage("test", 350, 350));
        }
    }
  • 相关阅读:
    SharePoint 在文本编辑框中插入图片报错
    SharePoint 修改列表阀值
    SharePoint 获取SPListItem附件地址
    SharePoint 验证用户组是否存在当前用户方法
    SharePoint中查看用户登录IP与用户名
    SharePoint CAML中通过ID查找Lookup字段
    SharePoint 服务器修改密码(前端服务器与数据库服务器分开)
    20191128-1 总结
    康哲 20191121-1 每周例行报告
    对成员的感谢
  • 原文地址:https://www.cnblogs.com/zhexuejun/p/15643457.html
Copyright © 2011-2022 走看看