0
在我的项目中,我试图在带有列(Blob)的Mysql Db表中的现有图像中添加水印。在Java中的MySqlDB中为图像添加水印
我用下面的方法给任何图像文件添加水印,并且它工作正常。
public static void addTextWatermark(String text, File sourceImageFile, File destImageFile) {
try {
BufferedImage sourceImage = ImageIO.read(sourceImageFile);
Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();
// initializes necessary graphic properties
AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
g2d.setComposite(alphaChannel);
g2d.setColor(Color.WHITE);
g2d.setFont(new Font("Arial", Font.BOLD, 64));
FontMetrics fontMetrics = g2d.getFontMetrics();
Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);
// calculates the coordinate where the String is painted
int centerX = (sourceImage.getWidth() - (int) rect.getWidth())/2;
int centerY = sourceImage.getHeight()/2;
// paints the textual watermark
g2d.drawString(text, centerX, centerY);
ImageIO.write(sourceImage, "png", destImageFile);
g2d.dispose();
System.out.println("The tex watermark is added to the image.");
} catch (IOException ex) {
System.err.println(ex);
}
}
我如何使用这种方法从DB中检索图像 - >添加水印 - >更新到数据库?我使用Spring MVC。
我的照片Model类是:
public class Photo {
@Id @GeneratedValue
private int id;
private int user_id;
private String name;
@Lob
private Blob content;
调用到服务层获得的照片:
Photo photo = photoService.getPhotoById(50);
要更新的照片:
photoService.updatePhoto(photo);
谁能请解释我来整合这个我的项目中的addTextWatermark()方法。
非常感谢Sanjeev。现在它工作正常:) –
很高兴它帮助你..玩得开心编码:) – Sanjeev
2年后,这只是救了我的屁股。非常感谢 :) – user2023608