【发布时间】:2016-07-29 03:24:28
【问题描述】:
在我的项目中,我正在尝试将水印添加到 Mysql Db Table 中的现有图像中,其中包含列(Blob)。
我用下面的方法给任何图片文件添加水印,效果很好。
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 中检索图像--> 添加水印--> 更新到 DB?我正在使用 Spring MVC。
我的照片模型类是:
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() 方法。
【问题讨论】:
标签: java mysql spring-mvc blob