简单生成二维码

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.alibaba.fastjson.JSONObject;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by robin on 2018/6/4.
 */
public class QRCodeUtil {
    private final static String FILE_URL_YOUR_CONFIG = "C:\\Users\\29007\\Desktop\\";//二维吗存储路径
    private final static String IMAGE_FORMAT = ".png";//文件后缀
    private final static Integer IMAGE_SIZE = 900;//二维码尺寸 最好900 实际像素 700*700 8k左右大小 必须 > 200

    /**
     * 生成包含字符串信息的二维码图片
     * @param fileName 文件名
     * @param qrCode 二维码携带信息
     * @throws WriterException
     * @throws IOException
     */
    public static boolean createQrCode(String fileName, QRCode qrCode) throws WriterException, IOException{
        return createQrCode(FILE_URL_YOUR_CONFIG,fileName,qrCode,IMAGE_SIZE,IMAGE_FORMAT);
    }

    /**
     * 生成包含字符串信息的二维码图片
     * @param outputUrl 文件输出流路径
     * @param fileName 文件名
     * @param qrCode 二维码携带信息
     * @param qrCodeSize 二维码图片大小
     * @param imageFormat 二维码的格式
     * @throws WriterException
     * @throws IOException
     */
    public static boolean createQrCode(String outputUrl,String fileName, QRCode qrCode, int qrCodeSize, String imageFormat) throws WriterException, IOException{
        String url = outputUrl + fileName + imageFormat;
        FileOutputStream outputStream = new FileOutputStream(new File(url));
        String content = JSONObject.toJSONString(qrCode);
        return createQrCode(outputStream,content,qrCodeSize,imageFormat);
    }

    /**
     * 生成包含字符串信息的二维码图片
     * @param outputStream 文件输出流路径
     * @param content 二维码携带信息
     * @param qrCodeSize 二维码图片大小
     * @param imageFormat 二维码的格式
     * @throws WriterException
     * @throws IOException
     */
    public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException{
    //解决中文乱码问题
    content = new String(content.getBytes("UTF-8"),"ISO-8859-1");
    //设置二维码纠错级别MAP
        Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);  // 矫错级别
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        //创建比特矩阵(位矩阵)的QR码编码的字符串
        BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
        // 使BufferedImage勾画QRCode  (matrixWidth 是行二维码像素点)
        int matrixWidth = byteMatrix.getWidth();
        BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, matrixWidth, matrixWidth);
        // 使用比特矩阵画并保存图像
        graphics.setColor(Color.BLACK);
        for (int i = 0; i < matrixWidth; i++){
            for (int j = 0; j < matrixWidth; j++){
                if (byteMatrix.get(i, j)){
                    graphics.fillRect(i-100, j-100, 1, 1);
                }
            }
        }
        return ImageIO.write(image,imageFormat.substring(1,imageFormat.length()), outputStream);
    }

    /**
     * 读二维码并输出携带的信息
     */
    public static QRCode readQrCode(String fileName) throws IOException, FormatException, ChecksumException, NotFoundException {
        String inputUrl = FILE_URL_YOUR_CONFIG + fileName + IMAGE_FORMAT;
        FileInputStream inputStream = new FileInputStream(new File(inputUrl));
        return readQrCode(inputStream);
    }

    /**
     * 读二维码并输出携带的信息
     */
    public static QRCode readQrCode(String inputUrl,String fileName,String imageFormat) throws IOException, FormatException, ChecksumException, NotFoundException {
        String url = inputUrl + fileName + imageFormat;
        FileInputStream inputStream = new FileInputStream(new File(url));
        return readQrCode(inputStream);
    }

    /**
     * 读二维码并输出携带的信息
     */
    public static QRCode readQrCode(InputStream inputStream) throws IOException, FormatException, ChecksumException, NotFoundException {
        //从输入流中获取字符串信息
        BufferedImage image = ImageIO.read(inputStream);
        //将图像转换为二进制位图源
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        QRCodeReader reader = new QRCodeReader();
        QRCode qrCode = new QRCode();
        Result result = reader.decode(bitmap);
        String text = result.getText();
        if(StringUtils.isNotBlank(text)){
            qrCode = JSONObject.parseObject(text, QRCode.class);
        }
        return qrCode;
    }
    /**
     * 测试代码
     * @throws WriterException
     */
    public static void main(String[] args) throws IOException, WriterException {
        try {
            QRCode qrCode = new QRCode();
            qrCode.setPhone("17712340000");
            String fileName = "3";
            /*生成二维码*/
            createQrCode(fileName,qrCode);
            /*读取二维码*/
            readQrCode(fileName);
        } catch (FormatException e) {
            e.printStackTrace();
        } catch (ChecksumException e) {
            e.printStackTrace();
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
    }


    /**
     * 二维码信息bean
     */
    public static class QRCode {
        private String phone;

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }
    }

}

猜你喜欢

转载自blog.csdn.net/lixuegen/article/details/80623558
今日推荐