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();
/**
* 生成二維碼
* 經(jīng)過(guò)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/";
//生成文件名稱(chēng)
String fileName = "二維碼.png";
String qrCodeBase64 = createQrCodeBase64(url);
System.out.println(qrCodeBase64);
}
}
二維碼生成工具類(lèi)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 首先導(dǎo)入maven依賴(lài),這里使用的是google的一個(gè)多種格式的1D/2D條碼圖像處理庫(kù)zxing 然后寫(xiě)一個(gè)Bu...
- RxTools 工欲善其事必先利其器! Android開(kāi)發(fā)過(guò)程經(jīng)常需要用到各式各樣的工具類(lèi),雖然大部分只需谷歌/百...
- Ceate QRCreator.swift file 完整使用示例 簡(jiǎn)潔使用 代碼及示例下載(有實(shí)現(xiàn)二維碼掃描)N...
- 有些項(xiàng)目中需要用到二維碼,這里我們編寫(xiě)一個(gè)二維碼生成的工具類(lèi)。 我們需要用到zxing這個(gè)二維碼工具類(lèi),然后再封裝...
- 版權(quán)歸作者所有,任何形式轉(zhuǎn)載請(qǐng)聯(lián)系作者。 作者:Blue(來(lái)自豆瓣) 來(lái)源:https://book.douban...