🌷🍁 博主猫头虎 带您 Go to New World.✨🍁 🦄 博客首页——猫头虎的博客🎐 🐳《面试题大全专栏》 文章图文并茂🦕生动形象🦖简单易学!欢迎大家来踩踩~🌺 🌊 《IDEA开发秘籍专栏》学会IDEA常用操作,工作效率翻倍~💐 🌊 《100天精通Golang(基础入门篇)》学会Golang语言,畅玩云原生,走遍大小厂~💐
🪁🍁 希望本文能够给您带来一定的帮助🌸文章粗浅,敬请批评指正!🍁🐥
ImageIO
类加载你想要添加到二维码中的 Logo 图片。确保 Logo 图片的尺寸适当,不会过大。
Graphics2D
对象来操作图像。
/**
* 将照片logo添加到二维码中间
*
* @param image 生成的二维码照片对象
* @param imagePath 照片保存路径
* @param logoPath logo照片路径
* @param formate 照片格式
*/
public static void overlapImage(
BufferedImage image,
String formate,
String imagePath,
String logoPath,
MatrixToLogoImageConfig logoConfig) {
try {
BufferedImage logo = ImageIO.read(new File(logoPath));
Graphics2D g = image.createGraphics();
// 考虑到logo照片贴到二维码中,建议大小不要超过二维码的1/5;
int width = image.getWidth() / logoConfig.getLogoPart();
int height = image.getHeight() / logoConfig.getLogoPart();
// logo起始位置,此目的是为logo居中显示
int x = (image.getWidth() - width) / 2;
int y = (image.getHeight() - height) / 2;
// 绘制图
g.drawImage(logo, x, y, width, height, null);
// 给logo画边框
// 构造一个具有指定线条宽度以及 cap 和 join 风格的默认值的实心 BasicStroke
g.setStroke(new BasicStroke(logoConfig.getBorder()));
g.setColor(logoConfig.getBorderColor());
g.drawRect(x, y, width, height);
g.dispose();
// 写入logo照片到二维码
ImageIO.write(image, formate, new File(imagePath));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
/**
* 插入LOGO
*
* @param source 二维码图片
* @param imgPath LOGO图片地址
* @param needCompress 是否压缩
* @throws Exception
*/
private static void insertImage(BufferedImage source, String imgPath, boolean needCompress)
throws Exception {
File file = new File(imgPath);
if (!file.exists()) {
System.err.println("" + imgPath + " 该文件不存在!");
return;
}
Image src = ImageIO.read(new File(imgPath));
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_WIDTH - width) / 2;
int y = (QRCODE_HEIGHT - 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 添加到二维码中间的思路可以分为以下步骤:
ImageIO
类加载你想要添加到二维码中的 Logo 图片。确保 Logo 图片的尺寸适当,不会过大。
Graphics2D
对象来操作图像。
以下是一个简单的示例代码框架,演示了将 Logo 添加到二维码中间的过程:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class QRCodeWithLogo {
public static void main(String[] args) {
try {
// Step 1: Generate QR code
String qrCodeContent = "Your QR code content here";
BufferedImage qrCodeImage = generateQRCode(qrCodeContent);
// Step 2: Load and resize logo
BufferedImage logoImage = ImageIO.read(new File("logo.png"));
int logoWidth = 50; // Adjust the logo size as needed
int logoHeight = 50;
Image resizedLogo = logoImage.getScaledInstance(logoWidth, logoHeight, Image.SCALE_SMOOTH);
// Step 3: Calculate center position
int centerX = (qrCodeImage.getWidth() - logoWidth) / 2;
int centerY = (qrCodeImage.getHeight() - logoHeight) / 2;
// Step 4: Add logo to QR code
Graphics2D graphics = qrCodeImage.createGraphics();
graphics.drawImage(resizedLogo, centerX, centerY, null);
graphics.dispose();
// Step 5: Save the final QR code with logo
File outputImage = new File("qrCodeWithLogo.png");
ImageIO.write(qrCodeImage, "png", outputImage);
System.out.println("QR code with logo created successfully!");
} catch (Exception e) {
e.printStackTrace();
}
}
private static BufferedImage generateQRCode(String content) throws Exception {
// Use a QR code library like ZXing to generate QR code
// Return the BufferedImage containing the QR code
// Example: return QRCode.from(content).withSize(200, 200).bufferedImage();
return null;
}
}