二维码图片生成

1.下载jar包

因为QRCODE的jar包文件不在Central Repository中,所以需要手动添加jar包

jar包网盘下载地址:

链接:https://pan.baidu.com/s/1zYHVEg99XSXI2AfhBWDXDg
提取码:ev6r
注:这个jar包与https://mvnrepository.com/artifact/QRCode这个网址下载的是不一样的(已试错,包导不出来)

2.手动导入jar包到maven仓库

先给jar包改个名,随便啥名,这就是maven对应的版本号

这里我改的是QRCode-1.1.9.jar,然后用下面这则命令在cmd里输入

mvn install:install-file -Dfile=E:\18613\DownLoads\QRCode-1.1.9.jar -DgroupId=QRCode -DartifactId=QRCode -Dversion=1.1.9 -Dpackaging=jar

生成后查看本地maven仓库已有jar包

在pom.xml添加下面依赖

 

 这里版本号报红没关系,能够正常使用(导包)

 3.生成二维码图片的工具类

import com.swetake.util.Qrcode;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
import jp.sourceforge.qrcode.exception.DecodingFailedException;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.imageio.ImageIO;

public class QRCodeUtil {

    public static void qrCodeEncode(String encodeddata, File destFile) throws IOException {

        Qrcode qrcode = new Qrcode();
        qrcode.setQrcodeErrorCorrect('M');  // 纠错级别(L 7%、M 15%、Q 25%、H 30%)和版本有关
        qrcode.setQrcodeEncodeMode('B');
        qrcode.setQrcodeVersion(7);     // 设置Qrcode包的版本

        byte[] d = encodeddata.getBytes("GBK"); // 字符集
        BufferedImage bi = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB);
        // createGraphics   // 创建图层
        Graphics2D g = bi.createGraphics();

        g.setBackground(Color.WHITE);   // 设置背景颜色(白色)
        g.clearRect(0, 0, 139, 139);    // 矩形 X、Y、width、height
        g.setColor(Color.BLACK);    // 设置图像颜色(黑色)

        if (d.length > 0 && d.length < 123) {
            boolean[][] b = qrcode.calQrcode(d);
            for (int i = 0; i < b.length; i++) {
                for (int j = 0; j < b.length; j++) {
                    if (b[j][i]) {
                        g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);
                    }
                }
            }
        }

//          Image img = ImageIO.read(new File("D:/tt.png"));  logo
//          g.drawImage(img, 25, 55,60,50, null);

        g.dispose(); // 释放此图形的上下文以及它使用的所有系统资源。调用 dispose 之后,就不能再使用 Graphics 对象
        bi.flush(); // 刷新此 Image 对象正在使用的所有可重构的资源

        ImageIO.write(bi, "png", destFile);
//            System.out.println("Input Encoded data is:" + encodeddata);
    }

    /**
     * 解析二维码,返回解析内容
     *
     * @param imageFile
     * @return
     */
    public static String qrCodeDecode(File imageFile) {
        String decodedData = null;
        QRCodeDecoder decoder = new QRCodeDecoder();
        BufferedImage image = null;
        try {
            image = ImageIO.read(imageFile);
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }

        try {
            decodedData = new String(decoder.decode(new J2SEImage(image)), "GBK");
//                System.out.println("Output Decoded Data is:" + decodedData);
        } catch (DecodingFailedException dfe) {
            System.out.println("Error: " + dfe.getMessage());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return decodedData;
    }

    static class J2SEImage implements QRCodeImage {
        BufferedImage image;

        public J2SEImage(BufferedImage image) {
            this.image = image;
        }

        public int getWidth() {
            return image.getWidth();
        }

        public int getHeight() {
            return image.getHeight();
        }

        public int getPixel(int x, int y) {
            return image.getRGB(x, y);
        }
    }

    public static void main(String[] args) {
        String filePath = "d:/wutongshu-blog.png";
        File qrFile = new File(filePath);

        // 二维码内容
        String encodeddata = "https://www.cnblogs.com/wutongshu-master/";
        try {
            QRCodeUtil.qrCodeEncode(encodeddata, qrFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 解码
        String reText = QRCodeUtil.qrCodeDecode(qrFile);
        System.out.println(reText);
    }
}
View Code

这里main方法就是写在这个类里面的

 

 filePath代表生成的图片路径

 4.效果图

 

 大功告成!

 


吐槽:博客园这是个bug吧,不能以图片结尾,否则就会是加载中的符号

 

posted @ 2019-11-19 16:14  梧桐树master  阅读(333)  评论(0编辑  收藏  举报