java生成二维码并融合模板工具类
二维码融合模板
二维码融合图片
import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.Hashtable; import javax.imageio.ImageIO; import org.springframework.beans.factory.annotation.Autowired; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix; import com.skjd.service.system.driver.DriverService; public class ImageAndQRcode { @Autowired DriverService driverService; private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; ImageAndQRcode() { } private static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); 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, matrix.get(x, y) ? BLACK : WHITE); } } return image; } private static void writeToFile(BitMatrix matrix, String format, File file) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); } } /** * @Title: 构造图片 * @Description: 生成水印并返回java.awt.image.BufferedImage * @param file源文件(图片) * @param waterFile水印文件(图片) * @param x距离右下角的X偏移量 * @param y 距离右下角的Y偏移量 * @param alpha 透明度, 选择值从0.0~1.0: 完全透明~完全不透明 * @return BufferedImage * @throws IOException */ private static BufferedImage watermark(File file, File waterFile, int x, int y, float alpha) throws IOException { Image src=Toolkit.getDefaultToolkit().getImage(file.getPath()); BufferedImage image=BufferedImageBuilder.toBufferedImage(src); Image src1=Toolkit.getDefaultToolkit().getImage(waterFile.getPath()); BufferedImage image1=BufferedImageBuilder.toBufferedImage(src1); Graphics2D g2d = image.createGraphics(); int waterImgWidth = image1.getWidth();// 获取层图的宽度 int waterImgHeight = image1.getHeight();// 获取层图的高度 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); g2d.drawImage(image1, x, y, waterImgWidth, waterImgHeight, null); g2d.dispose();// 释放图形上下文使用的系统资源 return image; } /** * 输出水印图片 * @param buffImg 图像加水印之后的BufferedImage对象 * @param savePath 图像加水印之后的保存路径 */ private void generateWaterFile(BufferedImage buffImg, String savePath) { int temp = savePath.lastIndexOf(".") + 1; try { ImageIO.write(buffImg, savePath.substring(temp), new File(savePath)); } catch (IOException e1) { e1.printStackTrace(); } } /** * * @param text 二维码内容 * @param width 二维码图片宽度 * @param height 二维码图片高度 * @param format 二维码的图片格式 * @param sourceFilePath 底层图片路径 * @param waterFilePath 二维码路径 * @param saveFilePath 合成图片路径 * @param maginx 二维码距离底图x轴距离 * @param maginy 二维码距离底图y轴距离 * @throws Exception * @return */ public static byte[] addImageQRcode(String text, int width, int height, String format, String sourceFilePath, String waterFilePath, String saveFilePath, int maginx, int maginy) throws Exception { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码 BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); File outputFile = new File(waterFilePath); ImageAndQRcode.writeToFile(bitMatrix, format, outputFile); ImageAndQRcode newImageUtils = new ImageAndQRcode(); BufferedImage buffImg = ImageAndQRcode.watermark(new File(sourceFilePath), new File(waterFilePath), maginx, maginy, 1.0f); newImageUtils.generateWaterFile(buffImg, saveFilePath); ByteArrayOutputStream os = new ByteArrayOutputStream(); boolean flag = ImageIO.write(buffImg, "jpg", os); byte[] byteArray = os.toByteArray(); return byteArray; } public static void main(String[] args) throws IOException { ImageAndQRcode add = new ImageAndQRcode(); /** * * @param text 二维码内容 * @param width 二维码图片宽度 * @param height 二维码图片高度 * @param format 二维码的图片格式 * @param sourceFilePath 底层图片路径 * @param waterFilePath 二维码路径 * @param saveFilePath 合成图片路径 * @param maginx 二维码距离底图x轴距离 * @param maginy 二维码距离底图y轴距离 * @throws Exception */ try { add.addImageQRcode("http://www.baidu.com", 300, 300, "jpg", "C:/Users/admin/Desktop/1.png", "C:/Users/admin/Desktop/new.jpg", "C:/Users/admin/Desktop/3.png", 250, 250); } catch (Exception e) { e.printStackTrace(); } } }
调用案例
public static byte[] t4(HttpServletRequest request,String t,String id){ String realPath = ""; if("1".equals(t)){ realPath = request.getSession().getServletContext().getRealPath("/static/images/1.png").replaceAll("\\\\", "/"); }else{ realPath= request.getSession().getServletContext().getRealPath("/static/images/2.png").replaceAll("\\\\", "/"); } String uuid1 = UuidUtils.getUUID(); String uuid3 = UuidUtils.getUUID(); String realPath3= request.getSession().getServletContext().getRealPath("/static/images/)"+uuid1+"(.jpg").replaceAll("\\\\", "/"); String realPath4 = request.getSession().getServletContext().getRealPath("/static/images/)"+uuid3+"(.jpg").replaceAll("\\\\", "/"); String contentz = "www.skjiadao.com?id="+id+"&type="+t; byte[] addImageQRcode1; try { addImageQRcode1 = ImageAndQRcode.addImageQRcode(contentz, 300, 300, "jpg", realPath, realPath4, realPath3, 250, 250); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return new byte[1]; } return addImageQRcode1; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?