首先,我们需要下载Google的ZXing库。
下载地址:https://github.com/zxing/zxing
根据内容创建二维码
/**
* TODO 根据给定的内容生成二维码
*
* @param content 二维码内容
* @param logoImagePath logo图标的路径
* @param needCompressed 是否需要压缩logo
* @return 生成的二维码
* @throws IOException
* @throws WriterException
*/
private static BufferedImage createImage(String content, String logoImagePath, boolean needCompressed)
throws IOException, WriterException {
HashMap hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 纠错等级
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1); // 二维码两边空白区域大小
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
int height = bitMatrix.getHeight();
int width = bitMatrix.getWidth();
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, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
if (logoImagePath == null || "".equals(logoImagePath))
return image;
// 如果有logo,则插入logo图片
QRCode.InsertImage(image, logoImagePath, needCompressed);
return image;
}
如果有logo则将logo插入到二维码中
```java
/**
* TODO 插入logo图片
* @param sourceImage 原图片
* @param logoImagePath logo图片所在的路径
* @param needCompressed 是否需要压缩
* @throws IOException
*/
private static void InsertImage(BufferedImage sourceImage, String logoImagePath, boolean needCompressed) throws IOException {
File file = new File(logoImagePath);
if (!file.exists()){
System.out.println("logo文件不存在!\n");
return;
}
Image src = ImageIO.read(file);
int width = src.getWidth(null);
int height = src.getHeight(null);
// 压缩二维码图片
if (needCompressed) {
if (width > LOGO_WIDTH)
width = LOGO_WIDTH;
if (height > LOGO_HEIGHT)
height = LOGO_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;
// 直观的理解:Graphics2D 就相当于画笔,而BufferedImage 就是画笔绘制的结果。
}
// 插入logo
Graphics2D graph = sourceImage.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();
}
解码过程
/**
* TODO 解析二维码内容
* @param file 二维码
* @return 二维码包含的信息
* @throws Exception
*/
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));
HashMap hints = new HashMap<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
Result result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}
源码下载地址:https://github.com/Liyzy/ZXing-QRCode
开发环境:idea 2018.2
转载:原文链接包含生成原理:https://blog.csdn.net/weixin_36191602/article/details/82466148