效果图
1. 安装依赖
com.google.zxing
core
3.1.0
com.twelvemonkeys.imageio
imageio-jpeg
3.0-rc5
2. 生成二维码
public class ZXingCode {
private static final int QRCOLOR = 0xFF000000; // 默认是黑色
private static final int BGWHITE = 0xFFFFFFFF; // 背景颜色
private static final int WIDTH = 200; // 二维码宽
private static final int HEIGHT = 200; // 二维码高
// 用于设置QR二维码参数
private static Map hints = new HashMap() {
private static final long serialVersionUID = 1L;
{
put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置编码方式
put(EncodeHintType.MARGIN, 0);
}
};
// 生成带logo的二维码图片
public static void drawLogoQRCode(File logoFile, File codeFile, String qrUrl) {
try {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
// 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
}
}
int width = image.getWidth();
int height = image.getHeight();
if (Objects.nonNull(logoFile) && logoFile.exists()) {
// 构建绘图对象
Graphics2D g = image.createGraphics();
// 读取Logo图片
BufferedImage logo = ImageIO.read(logoFile);
// 开始绘制logo图片
g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
g.dispose();
logo.flush();
}
image.flush();
ImageIO.write(image, "png", codeFile); // TODO
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws WriterException {
File logoFile = new File("C://Users//mayn//Desktop//型号//小米//hongmi 5A//5a9d2490N317048d9.jpg");
File QrCodeFile = new File("C://Users//mayn//Desktop//05.png");
String url = "https://www.baidu.com/";
String note = "访问百度连接";
drawLogoQRCode(logoFile, QrCodeFile, url);
}
3. 合并图片和二维码
public class mergeImage {
public static void mergeImage(String bigPath, String smallPath, String x, String y) throws IOException {
try {
BufferedImage small;
BufferedImage big = ImageIO.read(new File(bigPath));
if (smallPath.contains("http")) {
URL url = new URL(smallPath);
small = ImageIO.read(url);
} else {
small = ImageIO.read(new File(smallPath));
}
Graphics2D g = big.createGraphics();
float fx = Float.parseFloat(x);
float fy = Float.parseFloat(y);
int x_i = (int) fx;
int y_i = (int) fy;
g.drawImage(small, x_i, y_i, small.getWidth(), small.getHeight(), null);
g.dispose();
ImageIO.write(big, "png", new File(bigPath));
} catch (Exception e) {
e.printStackTrace();
}
}
}
4, 执行
public static void main(String[] args) throws WriterException {
try {
mergeImage.mergeImage("C://Users//mayn//Desktop//origin.png", "C://Users//mayn//Desktop//05.png", "63", "163");
} catch (IOException e) {
e.printStackTrace();
}
}