博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
生成二维码图片的工具类
阅读量:4329 次
发布时间:2019-06-06

本文共 2386 字,大约阅读时间需要 7 分钟。

package utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import models.utils.EWBase64;
import play.Play;
import sun.misc.BASE64Encoder;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.awt.image.BufferedImage;
/**
 * 二维码生成类
 *
 * @author Thierry
 *
 */
public class QrcodeBuilder {
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;
    private QrcodeBuilder() {
    }
    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        return image;
    }
    public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, file)) {
            throw new IOException("Could not write an image of format " + format + " to " + file);
        }
    }
    public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, stream)) {
            throw new IOException("Could not write an image of format " + format);
        }
    }
    /**
     * 生成Qrcode图片
     *
     * @param filePath
     *            图片保存文件夹路径
     * @param content
     *            二维码内容
     * @param username
     *            提交操作的用户名
     * @return
     */
    public static String createQrCode(String filePath, String content, String username) {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        String fileName = EWBase64.encode(username + "_" + timestamp) + ".png";
        try {
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            Map hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400, hints);
            File file = new File(filePath, fileName);
            QrcodeBuilder.writeToFile(bitMatrix, "png", file);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return fileName + ".png";
    }
}

转载于:https://www.cnblogs.com/xunfang123/p/4243317.html

你可能感兴趣的文章
PAT 甲级 1096 Consecutive Factors
查看>>
拓扑排序
查看>>
rtf格式 C#设置字间距 CharacterSpacing
查看>>
JS Date格式化为yyyy-MM-dd类字符串
查看>>
win10安装.net3.5 报错解决
查看>>
福大软工 · 最终作业 - 软件工程实践总结(个人)
查看>>
SSM整合XML版(Maven Project)
查看>>
时间序列分析六:截面数据和时序数据结合的多变量时序分析
查看>>
phpwind 统计代码位置
查看>>
Set up ruby debugging environment
查看>>
初识python
查看>>
Windows 10 部署Enterprise Solution 5.5
查看>>
二叉树 - 前、中、后序遍历
查看>>
Java 反射机制浅析
查看>>
切换svn登录账户
查看>>
oracle配置备份
查看>>
手游后台PVP系统网络同步方案总结
查看>>
luogu1979 华容道 (dijkstra+bfs)
查看>>
ADO.NET基础必备之DataSet
查看>>
Valid Palindrome
查看>>