package zxing;
import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import jp.sourceforge.qrcode.util.Color;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
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.util.HashMap;
import java.util.Map;
/**
* 需要的jar包
* 1. zxing-javase-3.1.0.jar
* 2.zxing-3.1.0.jar
*/
public class ZXingUtil {
/**
* 生成二维码
* @param content 内容
* @param imgPath 输出图片路径
* @param logoPath logo路径
* @param format 二维码类型
*/
public static void encodeImg(String content,String imgPath,String logoPath,String format,int width,int height) throws IOException, WriterException {
//设置二维码相关属性
HashMap<EncodeHintType, Object> hints = new HashMap<>();
//设置排错率 7% L<M<Q<H 30%
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//编码
hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
//外边距
hints.put(EncodeHintType.MARGIN,1);
/*
BitMatrix 可以将内容 -> boolean[][]
* content : 需要加密的 文字
* BarcodeFormat.QR_CODE:要解析的类型(二维码)
* hints:加密涉及的一些参数:编码、排错率
*/
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
//产生内存中的图片
//BufferedImage.TYPE_INT_RGB :图片产生的颜色(绘图基础颜色)
BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < width; i++) {
for(int j = 0; j < height; j++){
bufferedImage.setRGB(i,j,(bitMatrix.get(i,j)? Color.BLACK : Color.WHITE));
}
}
//画logo
bufferedImage = drawLogo(bufferedImage,logoPath);
//String->file
File file = new File(imgPath);
//生成图片
ImageIO.write(bufferedImage,format,file);
}
public static void decodeImg(File file) throws IOException, NotFoundException {
if(!file.exists()) return;
//file->内存中的一张图片
BufferedImage imge = ImageIO.read(file) ;
LuminanceSource source = new BufferedImageLuminanceSource(imge);
HybridBinarizer hybridBinarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(hybridBinarizer);
MultiFormatReader formatReader = new MultiFormatReader() ;
//图片 ->result
Map map = new HashMap();
map.put(EncodeHintType.CHARACTER_SET, "utf-8") ;
Result result = formatReader.decode(binaryBitmap ,map ) ;
System.out.println("解析结果:"+ result.toString());
}
private static BufferedImage drawLogo(BufferedImage bufferedImage, String logoPath) throws IOException {
//创建一个画板
Graphics2D gs = bufferedImage.createGraphics();
//读取硬盘中的logo到内存
BufferedImage logo = ImageIO.read(new File(logoPath));
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
//纯logo
gs.drawImage(logo,width*2/5,height*2/5,width/5,height/5,null);
//产生一个 画 白色圆角正方形的 画笔
//画笔粗细
BasicStroke basicStroke = new BasicStroke(5,BasicStroke.CAP_ROUND ,BasicStroke.JOIN_ROUND);
//将画笔画板关联
gs.setStroke(basicStroke);
//创建一个正方形
RoundRectangle2D.Float round = new RoundRectangle2D.Float(width * 2 / 5, height * 2 / 5, width / 5, height / 5, BasicStroke.CAP_ROUND, BasicStroke
.JOIN_ROUND);
gs.setColor(java.awt.Color.white);
gs.draw(round);
RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(width*2/5+3,height*2/5+3,width/5-6,height/5-6,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
gs.setColor(java.awt.Color.gray);
gs.draw(round2);
//释放空间
gs.dispose();
//清理缓冲区
bufferedImage.flush();
return bufferedImage;
}
public static void main(String[] args) throws IOException, WriterException, NotFoundException {
String imgPath = "E:\\temp\\二维码2.png";
String content = "http://www.baidu.com";
String logoPath = "E:\\temp\\previewFix.jpg";
ZXingUtil.encodeImg(content,imgPath,logoPath,"png",430,430);
ZXingUtil.decodeImg(new File(imgPath));
}
}
二维码ZXing
最新推荐文章于 2024-05-06 23:38:08 发布