gpt4 book ai didi

java - java中MySqlDB中的图像添加水印

转载 作者:行者123 更新时间:2023-11-29 11:36:31 24 4
gpt4 key购买 nike

在我的项目中,我尝试使用列(Blob)向 Mysql Db 表中的现有图像添加水印。

我使用下面的方法向任何图像文件添加水印,效果很好。

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);
}
}

我如何使用此方法从数据库检索图像-->添加水印-->更新数据库?我正在使用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() 方法集成到我的项目中。

最佳答案

五步流程正是您所需要的。

第 1 步:

使用选择查询从 MySQL DB 读取图像(blob)作为 byte[]

第 2 步:

像这样将 byte[] 转换为 BufferedImage

private BufferedImage createImageFromBytes(byte[] imageData) {
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
try {
return ImageIO.read(bais);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

第 3 步:

更改 addWaterMark 方法以生成带水印的缓冲图像

public static BufferedImage addTextWatermark(String text, BufferedImage sourceImage) {
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);

return sourceImage;
}

第 4 步:将 BufferedImage 转换为 byte[]

private byte[] createBytesFromImage(BufferedImage image) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();

ImageIO.write(image,"png",baos);

byte[] imageBytes = baos.toByteArray();
baos.close();
return imageBytes;

} catch (IOException e) {
throw new RuntimeException(e);
}
}

第 5 步:

使用更新查询将此字节[]写回到MySQL Db。

希望这有帮助。

关于java - java中MySqlDB中的图像添加水印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36489855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com