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

    一、加入maven依赖

    <!-- 谷歌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>

    二、工具类代码

     1 package com.example.demo.utils;
     2 
     3 import com.google.zxing.*;
     4 import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
     5 import com.google.zxing.common.BitMatrix;
     6 import com.google.zxing.common.HybridBinarizer;
     7 import com.google.zxing.qrcode.QRCodeReader;
     8 import com.google.zxing.qrcode.QRCodeWriter;
     9 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
    10 
    11 import javax.imageio.ImageIO;
    12 import java.awt.*;
    13 import java.awt.image.BufferedImage;
    14 import java.io.*;
    15 import java.util.Hashtable;
    16 
    17 /**
    18  * @author zsh
    19  * @company wlgzs
    20  * @create 2019-03-10 15:17
    21  * @Describe 二维码生成和读的工具类
    22  */
    23 public class QrCodeCreateUtil {
    24     /**
    25      * 生成包含字符串信息的二维码图片
    26      * @param outputStream 文件输出流路径
    27      * @param content 二维码携带信息
    28      * @param qrCodeSize 二维码图片大小
    29      * @param imageFormat 二维码的格式
    30      * @throws WriterException
    31      * @throws IOException
    32      */
    33     public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException{
    34         //设置二维码纠错级别MAP
    35         Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
    36         hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);  // 矫错级别
    37         QRCodeWriter qrCodeWriter = new QRCodeWriter();
    38         //创建比特矩阵(位矩阵)的QR码编码的字符串
    39         BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
    40         // 使BufferedImage勾画QRCode  (matrixWidth 是行二维码像素点)
    41         int matrixWidth = byteMatrix.getWidth();
    42         BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB);
    43         image.createGraphics();
    44         Graphics2D graphics = (Graphics2D) image.getGraphics();
    45         graphics.setColor(Color.WHITE);
    46         graphics.fillRect(0, 0, matrixWidth, matrixWidth);
    47         // 使用比特矩阵画并保存图像
    48         graphics.setColor(Color.BLACK);
    49         for (int i = 0; i < matrixWidth; i++){
    50             for (int j = 0; j < matrixWidth; j++){
    51                 if (byteMatrix.get(i, j)){
    52                     graphics.fillRect(i-100, j-100, 1, 1);
    53                 }
    54             }
    55         }
    56         return ImageIO.write(image, imageFormat, outputStream);
    57     }
    58 
    59     /**
    60      * 读二维码并输出携带的信息
    61      */
    62     public static void readQrCode(InputStream inputStream) throws IOException{
    63         //从输入流中获取字符串信息
    64         BufferedImage image = ImageIO.read(inputStream);
    65         //将图像转换为二进制位图源
    66         LuminanceSource source = new BufferedImageLuminanceSource(image);
    67         BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    68         QRCodeReader reader = new QRCodeReader();
    69         Result result = null ;
    70         try {
    71             result = reader.decode(bitmap);
    72         } catch (ReaderException e) {
    73             e.printStackTrace();
    74         }
    75         System.out.println(result.getText());
    76     }
    77     /**
    78      * 测试代码
    79      * @throws WriterException
    80      */
    81     public static void main(String[] args) throws IOException, WriterException {
    82 
    83         createQrCode(new FileOutputStream(new File("d:\qrcode.jpg")),"WE1231238239128sASDASDSADSDWEWWREWRERWSDFDFSDSDF123123123123213123",900,"JPEG");
    84         readQrCode(new FileInputStream(new File("d:\qrcode.jpg")));
    85     }
    86 }

    效果图:

  • 相关阅读:
    29. Divide Two Integers
    leetCode 17. Letter Combinations of a Phone Number
    查找
    快速排序
    希尔排序
    插入排序
    归并排序,还有非递归方式没写
    堆排序--还有递归法没有写
    c++实现字符串全排序
    归并排序
  • 原文地址:https://www.cnblogs.com/zsh-blogs/p/10505430.html
Copyright © 2011-2022 走看看