[英]add watermark to the image in MySqlDB in java
In my project , I'm trying to add watermark to existing image in Mysql Db Table with column( Blob ). 在我的项目中,我试图通过列(Blob)向Mysql Db Table中的现有图像添加水印。
I used below method to add watermark to any image file, and it works fine. 我使用下面的方法将水印添加到任何图像文件,并且工作正常。
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);
}
}
How I used this method to retrive image from DB--> Add watermark--> Update to DB?I'm using Spring MVC. 我如何使用这种方法从数据库中检索图像->添加水印->更新到数据库?我正在使用Spring MVC。
My Photo Model class is : 我的照片模型课是:
public class Photo {
@Id @GeneratedValue
private int id;
private int user_id;
private String name;
@Lob
private Blob content;
invoke to get photo in Service Layer : 调用以在Service Layer中获取照片:
Photo photo = photoService.getPhotoById(50);
To update photo : 更新照片:
photoService.updatePhoto(photo);
Anyone please explain me to integrate this addTextWatermark() method in my project. 任何人都请向我解释如何将此addTextWatermark()方法集成到我的项目中。
Five step process is what you need. 您需要五个步骤的过程。
Step 1: 第1步:
Read image (blob) from MySQL DB as byte[] using select query 使用选择查询从MySQL DB作为byte []读取图像(blob)
Step 2: 第2步:
Convert byte[] to BufferedImage like this 像这样将byte []转换为BufferedImage
private BufferedImage createImageFromBytes(byte[] imageData) {
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
try {
return ImageIO.read(bais);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Step 3: 第三步:
Change your addWaterMark method to produce Buffered Image with watermark 更改您的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;
}
Step 4: Convert BufferedImage to byte[] 步骤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);
}
}
Step 5: 步骤5:
Write this byte[] back to MySQL Db using update query. 使用更新查询将此byte []写回到MySQL Db。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.