1.主程序
package zxingDemo;
public class ZXingDemo {
public static void main(String[] args) throws Exception {
String imgPath = "src/img/zxing.png";//二维码存放路径
String content = "Hello World";//二维码内容
String imgType = "png";//二维码类型
int width = 300;//图片的宽
int height= 300;//图片的高
String logoPath="src/img/guo.jpg";//logo的路径
ZXingUtil zXingUtil = new ZXingUtil();
//将文本生成为二维码图像
zXingUtil.encodeImg(imgPath,imgType,content,width,height,logoPath);
String s = zXingUtil.decodeImg(imgPath);
System.out.println(s);
}
}
2.生成二维码的工具类
package zxingDemo;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import jp.sourceforge.qrcode.util.Color;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
public class ZXingUtil {
public void encodeImg(String imgPath,String imgType,String context,int width,int height,String logoPath) throws Exception{
//加密涉及的参数
HashMap<EncodeHintType,Object> map = new HashMap<>();
//排错率
map.put(EncodeHintType.ERROR_CORRECTION,"Q");
//编码格式
map.put(EncodeHintType.CHARACTER_SET,"utf-8");
//外边距
map.put(EncodeHintType.MARGIN,1);
//bitMatrix 包含了一个二维boolean数组
/*
* context: 文本内容
* BarcodeFormat.QR_CODE : 二维码格式
* map: 加密涉及的参数
* */
BitMatrix bitMatrix = new MultiFormatWriter().encode(context, BarcodeFormat.QR_CODE, width, height,map);
//获取文件对象
File file = new File(imgPath);
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
for (int i =0 ; i < width ;i++){
for (int j =0 ; j < height ; j++)
// if (bitMatrix.get(i, j)) {
// image.setRGB(i, j, Color.BLACK);
// } else {
// image.setRGB(i,j,Color.WHITE);
// }
//将此 BufferedImage 中的像素设置为指定的 RGB 值。
/*
* x - 要设置的像素的 X 坐标
y - 要设置的像素的 Y 坐标
rgb - RGB 值
* */
image.setRGB(i,j,bitMatrix.get(i,j)?Color.BLACK:Color.WHITE);
}
//将logo画入二维码
addLogo(image,logoPath);
//将内存中的图像写入磁盘:将image的图像写入file
ImageIO.write(image,imgType,file);
}
public void addLogo(BufferedImage image,String logoPath) throws Exception{
/*
* 将logo读入内存画入二维码图片
* */
//将logo读入内存
BufferedImage logo = ImageIO.read(new File(logoPath));
//创建一个画板
Graphics2D graphics2D = image.createGraphics();
//获取二维码的宽和高
int maxHeight = image.getHeight();
int maxWidth = image.getWidth();
//画入画板
/*
* drawImage():绘制指定图像中已缩放到适合指定矩形内部的图像。
* 图像绘制在此图形上下文坐标空间的指定矩形内部,如果需要,则进行缩放。
* 以指定的背景色绘制透明像素。此操作等同于用给定颜色填充指定图像宽度和
* 高度的矩形,然后在其上绘制图像,
* img - 要绘制的指定图像。如果 img 为 null,则此方法不执行任何操作。
x - x 坐标。
y - y 坐标。
width - 矩形的宽度。
height - 矩形的高度。
bgcolor - 在图像非透明部分下绘制的背景色。
observer - 当转换了更多图像时要通知的对象。
* */
graphics2D.drawImage(logo,maxWidth/5*2,maxHeight/5*2,maxWidth/5,maxHeight/5,null);
/*
* BasicStroke 类定义针对图形图元轮廓呈现属性的一个基本集合,这些图元使用 Graphics2D 对象呈现,
* 而该对象的 Stroke 属性设置为此 BasicStroke。由 BasicStroke 定义的呈现属性描述了用画笔沿 Shape
* 的轮廓绘制的某个标记的形状,以及应用在 Shape 路径线段的末端和连接处的装饰。
* */
//创建一个画笔
BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
//将画笔绑定画板
graphics2D.setStroke(stroke);
/*
* Float 类定义一个所有圆角都使用 float 坐标指定的矩形。
* */
RoundRectangle2D.Float r = new RoundRectangle2D.Float(maxWidth/5*2,maxHeight/5*2,maxWidth/5,maxHeight/5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
//将此图形上下文的当前颜色设置为指定颜色。
graphics2D.setColor(java.awt.Color.WHITE);
//使用当前 Graphics2D 上下文的设置勾画 Shape 的轮廓。
graphics2D.draw(r);
BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
graphics2D.setStroke(stroke2);
RoundRectangle2D.Float r2 = new RoundRectangle2D.Float
(maxWidth/5*2+2,maxHeight/5*2+2,maxWidth/5-4,maxHeight/5-4,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
graphics2D.setColor(java.awt.Color.GRAY);
graphics2D.draw(r2);
}
public String decodeImg(String imgPath) throws Exception {
File file = new File(imgPath);
if (!file.exists()){
return null;
}
//将图片读入内存
BufferedImage bufferedImage = ImageIO.read(file);
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Result result = new MultiFormatReader().decode(binaryBitmap);
return result.toString();
}
}