2016-04-08 50 views
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()方法。

回答

1

五步法就是你需要的。

步骤1:

读取的图像(斑点)从MySQL数据库使用选择查询

步骤2作为字节[]:

转换字节[]到的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:

将这个byte []写回到MySQL Db u唱更新查询。

希望这会有所帮助。

+0

非常感谢Sanjeev。现在它工作正常:) –

+0

很高兴它帮助你..玩得开心编码:) – Sanjeev

+1

2年后,这只是救了我的屁股。非常感谢 :) – user2023608