package com.mydataway.common.utils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
/**
* 生成二维码
* Date: 2019/12/2 10:35
* @author hetao
*/
public class QrCodeUtil {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private static final MatrixToImageConfig DEFAULT_CONFIG = new MatrixToImageConfig();
/**
* 生成二维码
* 经过base64编码
*
* @param url
* @return
*/
public static String createQrCodeBase64(String url) {
String base64String = "";
try {
Map<EncodeHintType, String> hints = new HashMap<>(4);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BufferedImage bufferedImage = toBufferedImage(bitMatrix);
ImageIO.write(bufferedImage, "jpg", outputStream);
BASE64Encoder encoder = new BASE64Encoder();
base64String = encoder.encode(outputStream.toByteArray());
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}
return "data:image/jpg;base64," + base64String;
}
/**
* 生成二维码
* 生成流
*
* @param url
* @return
*/
public static String createQrCodeStream(String url) {
String base64String = "";
try {
Map<EncodeHintType, String> hints = new HashMap<>(4);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);
MatrixToImageWriter.toBufferedImage(bitMatrix, DEFAULT_CONFIG);
} catch (Exception e) {
e.printStackTrace();
}
return base64String;
}
/**
* 生成二维码
* 直接生成图片文件
* @param url
* @param path
* @param fileName
* @return
*/
public static String createQrCodeFile(String url, String path, String fileName) {
try {
Map<EncodeHintType, String> hints = new HashMap<>(4);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);
File file = new File(path, fileName);
boolean isTrue = file.exists() || ((file.getParentFile().exists() || file.getParentFile().mkdirs()) && file.createNewFile());
if (isTrue) {
writeToFile(bitMatrix, "jpg", file);
System.out.println("成功:" + file);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
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);
}
}
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);
}
}
private 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 main(String[] args) {
//生成链接
String url = "https://www.baidu.com";
//配置生成路径
String path = "C:users/photo/";
//生成文件名称
String fileName = "二维码.png";
String qrCodeBase64 = createQrCodeBase64(url);
System.out.println(qrCodeBase64);
}
}
二维码生成工具类
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- RxTools 工欲善其事必先利其器! Android开发过程经常需要用到各式各样的工具类,虽然大部分只需谷歌/百...
- Ceate QRCreator.swift file 完整使用示例 简洁使用 代码及示例下载(有实现二维码扫描)N...
- 版权归作者所有,任何形式转载请联系作者。 作者:Blue(来自豆瓣) 来源:https://book.douban...