码迷,mamicode.com
首页 > 其他好文 > 详细

生成二维码

时间:2019-10-22 16:48:46      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:uid   mon   bit   character   public   final   artifact   dom   image   

package cn.com.utils;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.UUID;

public class QrCodeUtils {

    public File createQrCode(String content) throws Exception {
        final int width = 300;
        final int height = 300;
        final String format = "png";
        //定义二维码的参数
        HashMap hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN, 2);
        //生成二维码
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);

        String folder = System.getProperty("java.io.tmpdir");
        String fileName = UUID.randomUUID().toString()+".png";

        Path file = new File(folder,fileName).toPath();
        MatrixToImageWriter.writeToPath(bitMatrix, format, file);

        return file.toFile();
    }


}
<!-- 二维码相关 -->
        <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>
            

 

生成二维码

标签:uid   mon   bit   character   public   final   artifact   dom   image   

原文地址:https://www.cnblogs.com/bevis-byf/p/11720691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!