今天无意间接触到二维码生成了,这个日常如此常见的东西,怎么能不了解一下呢?所以我就找了一篇zxing的博客,然后找到了一个轮子,这里就收下啦~
原文地址:RexFang的zxing轮子
另外参考了:这篇文章
工具类FileUtil.java
package com.yubotao;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
/**
* @Auther: yubt
* @Description:
* @Date: Created in 11:28 2018/5/31
* @Modified By:
*/
public class FileUtil {
public static void mkdirs(String dir){
if (StringUtils.isEmpty(dir)){
return;
}
File file = new File(dir);
if (file.isDirectory()){
return;
}else {
file.mkdir();
}
}
}
主体工作类QRCodeUtil.java
package com.yubotao;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;
/**
* @Auther: yubt
* @Description:
* @Date: Created in 11:30 2018/5/31
* @Modified By:
*/
public class QRCodeUtil {
private static final String CHARSET = "UTF-8";
private static final String FORMAT_NAME = "JPG";
//二维码尺寸
private static final int QRCODE_SIZE = 200;
// LOGO宽度
private static final int WIDTH = 60;
// LOGO高度
private static final int HEIGHT = 60;
private static BufferedImage createImage(String content, String logoImgPath, boolean needCompress) throws WriterException, IOException{
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
// 指定纠错等级,纠错级别(L 7%、M 15%、Q 25%、H 30%)
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 内容所使用字符集编码
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
//设置二维码边的空度,非负数
hints.put(EncodeHintType.MARGIN, 1);
/** content,要编码的内容;
* 编码类型,目前zxing支持:Aztec 2D,CODABAR 1D format,Code 39 1D,Code 93 1D ,Code 128 1D,
* Data Matrix 2D , EAN-8 1D,EAN-13 1D,ITF (Interleaved Two of Five) 1D,
* MaxiCode 2D barcode,PDF417,QR Code 2D,RSS 14,RSS EXPANDED,UPC-A 1D,UPC-E 1D,UPC/EAN extension,UPC_EAN_EXTENSION
* hints,生成条形码时的一些配置,此项可选
**/
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.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++){
// 图案背景色:0xFF000000,BLACK;0xFFFFFFFF,WHITE。
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
if (logoImgPath == null || "".equals(logoImgPath)){
return image;
}
// 插入图片
QRCodeUtil.insertImage(image, logoImgPath, needCompress);
return image;
}
private static void insertImage(BufferedImage source, String logoImgPath, boolean needCompress) throws IOException {
File file = new File(logoImgPath);
if (!file.exists()){
return;
}
Image src = ImageIO.read(new File(logoImgPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress){
// 压缩LOGO
if (width > WIDTH){
width = WIDTH;
}
if (height > HEIGHT){
height = HEIGHT;
}
Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
src = image;
}
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(src, x, y, width, height, null);
//指定弧度的圆角矩形
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
// 设置笔画对象
graph.setStroke(new BasicStroke(3f));
// 绘制圆弧矩形
graph.draw(shape);
graph.dispose();
}
// 生成带Logo的二维码
public static void encode(String content, String logoImgPath, String destPath, boolean needCompress) throws Exception{
BufferedImage image = QRCodeUtil.createImage(content, logoImgPath, needCompress);
FileUtil.mkdirs(destPath);
ImageIO.write(image, FORMAT_NAME, new File(destPath));
}
// 生成不带Logo的二维码
public static void encode(String content, String destPath) throws Exception {
QRCodeUtil.encode(content, null, destPath, false);
}
// 生成带Logo的二维码,并输出到指定输出流
public static void encode(String content, String logoImgPath, OutputStream output, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, logoImgPath, needCompress);
ImageIO.write(image, FORMAT_NAME, output);
}
// 生成不带Logo的二维码,并输出到指定输出流
public static void encode(String content, OutputStream output) throws Exception {
QRCodeUtil.encode(content, null, output, false);
}
// 二维码解析
public static String decode(File file) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null){
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}
// 二维码解析
public static String decode(String path) throws Exception {
return QRCodeUtil.decode(new File(path));
}
}
测试类QRCodeUtilTest.java
package com.yubotao;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* @Auther: yubt
* @Description:
* @Date: Created in 15:03 2018/5/31
* @Modified By:
*/
public class QRCodeUtilTest {
@Before
public void setUp() throws Exception{}
@Test
public void testEncode()throws FileNotFoundException, Exception {
String dir = "F:/test/QRCode/test.jpg";
String content = "Love you~";
String logoImgPath = "F:/test/QRCode/logo.jpg";
File file = new File(dir);
QRCodeUtil.encode(content, logoImgPath, new FileOutputStream(file),true);
}
}
注意dir
,也就是接收输出流的文件,格式为.jpg
,之前测试的时候用了.txt
,就是一堆乱码了。。。