将多张图片合并成一张图片并附加文字说明。
导入jar包
<!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator --> <dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency>
代码:
public static void main(String[] args) { try { int width = 750; int height = 1334; BufferedImage backgroundImage = Thumbnails.of("C:\\Users\\Jzdsh\\Desktop\\项目\\ss\\bj.jpg").size(1342, 934).asBufferedImage( ); BufferedImage materialImage = Thumbnails.of("C:\\Users\\Jzdsh\\Desktop\\项目\\ss\\cz.jpg").size(740, 934).asBufferedImage( ); BufferedImage doorImage = Thumbnails.of("C:\\Users\\Jzdsh\\Desktop\\项目\\ss\\mbs.png").size(375, 375).asBufferedImage( ); BufferedImage qrImage = Thumbnails.of("C:\\Users\\Jzdsh\\Desktop\\项目\\ss\\qr.png").size(280, 280).asBufferedImage( ); BufferedImage textImage = Thumbnails.of(handleTextWaterMark( )).size(400,400).asBufferedImage(); Thumbnails.of("C:\\Users\\Jzdsh\\Desktop\\项目\\ss\\1.jpg").size(width, height) .watermark(new Coordinate(0, 0),backgroundImage,1f) .watermark(new Coordinate((int)(width*0.6), 0),materialImage,1f) .watermark(new Coordinate((int)(width*0.4),(int)(height*0.29)),doorImage,1f) .watermark(new Coordinate((int)(width*0.6),(int)(height*0.74)),qrImage,1f) .watermark(new Coordinate((int)(width*0.08),(int)(height*0.74)), textImage, 1f) .toFile("d:/aaaa/ss.jpg"); } catch (Exception e) { e.printStackTrace( ); } } /** * @throws * @description 操纵文字型水印 */ private static BufferedImage handleTextWaterMark( ) { // 设置文字样式 Font font = new Font("微软雅黑", Font.BOLD, 24); //创建缓存图 BufferedImage image = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB); //生成图片 Graphics2D g = image.createGraphics( ); //设置图片透明度为透明 image = g.getDeviceConfiguration( ).createCompatibleImage(400, 400, Transparency.TRANSLUCENT); //设置文本 距离 int y = 0; int distTop = 45; g = image.createGraphics( ); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(Color.black); g.setFont(font); g.drawString("Avignon", 5, y += distTop); g.setColor(new Color(102,102,102)); g.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 20)); g.drawString("1400/42K/24KS", 5, y += distTop); g.drawString("材质:不锈钢", 5, y += distTop); g.drawString("颜色:古铜色", 5, y += distTop); g.drawString("专利:AMTWAVAVG", 5, y += distTop); g.dispose( ); return image; }
效果图:
原文:https://www.cnblogs.com/lccsdncnblogs/p/thumbnails.html