2016-04-08 19 views
0

Projemde, Mysql Db Tablosunda (Blob) varolan resme filigran eklemeye çalışıyorum.java uygulamasında MySqlDB görüntüsüne filigran ekleme

Herhangi bir görüntü dosyasına filigran eklemek için aşağıdaki yöntemi kullandım ve iyi çalışıyor. > Filigran ekleme -? - Spring MVC kullanıyorum DB> Güncelleme DB'den görüntü alınamadı için bu yöntemi kullanılır Nasıl

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

.

Fotoğrafım Modeli sınıftır:

public class Photo { 
    @Id @GeneratedValue 
    private int id; 
    private int user_id; 
    private String name; 
    @Lob 
    private Blob content; 

çağırmak Servis Katmanı Fotoğrafı almak için: Bu entegre etmek

photoService.updatePhoto(photo); 

Herkes beni açıklayınız:

Photo photo = photoService.getPhotoById(50); 

fotoğrafını güncellemek projemde addTextWatermark() yöntemi.

cevap

1

Beş adımlı işlem, ihtiyacınız olan şeydir.

Adım 1:

oku görüntü

Adım 2 seçme sorgusu kullanarak bayt [] MySQL DB (damla):

dönüştürme bayt [] Bu

gibi 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:

değiştirin addWaterMark yöntemi filigran Tamponlastirilmis Image üretmek için

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

Adım 4: dönüştürme 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); 
    } 
} 

Adım 5:

Bu bayt [] 'ı tekrar MySQL Db'ye yazınız. güncelleme sorgusu söyle.

Bu yardımcı olur umarım.

+0

Çok teşekkürler Sanjeev. Şimdi iyi çalışıyor :) –

+0

Size yardımcı olduğuna sevindim .. Eğlenceli kodlama var :) – Sanjeev

+1

2 yıl sonra ve bu sadece kıçımı kurtardı. Çok teşekkür ederim :) – user2023608