二维码生成,包含文本,网址,图片等

二维码所引入的jar包
pom文件配置

    
    <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>2.1</version>
        </dependency>

 

二维码生成工具类,可自行修改

 

public class Qr {
    private static final int BLACK = 0xff000000;
    private static final int WHITE = 0xFFFFFFFF;
    public static String tomakeMode(String strJson,String path,String fileName) {
        Qr test = new Qr();
        String filePostfix="png";
        File file = new File(path +File.separatorChar+fileName + "."+filePostfix);
        System.out.println(file.getAbsolutePath());
        test.encode(strJson, file,filePostfix, BarcodeFormat.QR_CODE, 5000, 5000, null);
        return fileName+".png";
    }
    /**
     *  生成QRCode二维码<br>
     *  在编码时需要将com.google.zxing.qrcode.encoder.Encoder.java中的<br>
     *  static final String DEFAULT_BYTE_MODE_ENCODING = "ISO8859-1";<br>
     *  修改为UTF-8,否则中文编译后解析不了<br>
     * @param contents 二维码的内容
     * @param file 二维码保存的路径,如:C://test_QR_CODE.png
     * @param filePostfix 生成二维码图片的格式:png,jpeg,gif等格式
     * @param format qrcode码的生成格式
     * @param width 图片宽度
     * @param height 图片高度
     * @param hints
     */
    public  void encode(String contents, File file,String filePostfix, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) {
        try {
            contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, format, width, height);
            writeToFile(bitMatrix, filePostfix, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成二维码图片<br>
     *
     * @param matrix
     * @param format
     *            图片格式
     * @param file
     *            生成二维码图片位置
     * @throws IOException
     */
    public  static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        ImageIO.write(image, format, file);
    }

    /**
     * 生成二维码内容<br>
     *
     * @param matrix
     * @return
     */
    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);
            }
        }
        return image;
    }
//测试二维码生成:
    public static void main(String[] args) {
       // System.out.println(Qr.class.getResource("").getFile());
        Qr.tomakeMode("http://mp.weixin.qq.com/s?__biz=MzU0NDgxODMwNg==&mid=100000067&idx=1&sn=0f02e506c90b73c36ab87891bb363168&chksm=7b7714ae4c009db874289edc990e585690aef6ca751db7dab43d8e1e8dcb59b37030534b9dd7&scene=18#wechat_redirect","F:\\","qr");
    }
}

 

 欢迎关注

 

 

 

posted @ 2020-03-24 23:02  路大师_XA  阅读(559)  评论(0编辑  收藏  举报