2016-04-08 10 views
0

私のプロジェクトでは、カラム(Blob)を持つMysql Dbテーブルの既存の画像にウォーターマークを追加しようとしています。javaのMySqlDBの画像にウォーターマークを追加

以下の方法で画像ファイルにウォーターマークを追加しても問題ありません。私はDBから画像をretriveするために、このメソッドを使用する方法

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へ>アップデート私は春の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

5段階のプロセスが必要です。

ステップ1:

読み取り画像

ステップ2 SELECTクエリを使用して、バイト[]としてMySQL DBから(BLOB):

変換バイト[]この

よう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は、バイトを[]

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に書き戻してください更新クエリを歌う。

これが役に立ちます。

+0

ありがとうSanjeev。今それはうまく動作します:) –

+0

それはあなたを助けてくれてうれしい..コーディングを楽しくしてください:) – Sanjeev

+1

2年後、これはちょうど私のお尻を保存しました。どうもありがとうございます :) – user2023608

関連する問題