在 java 中为 MySqlDB 中的图像添加水印
add watermark to the image in MySqlDB in java
在我的项目中,我正在尝试为 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);
}
}
我如何使用此方法从数据库中检索图像--> 添加水印--> 更新到数据库?我正在使用 Spring MVC。
我的照片模特 class 是:
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 步:
使用select 查询
从MySQL 数据库中以字节[] 形式读取图像(blob)
第 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。
希望对您有所帮助。
在我的项目中,我正在尝试为 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);
}
}
我如何使用此方法从数据库中检索图像--> 添加水印--> 更新到数据库?我正在使用 Spring MVC。
我的照片模特 class 是:
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 步:
使用select 查询
从MySQL 数据库中以字节[] 形式读取图像(blob)第 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。
希望对您有所帮助。