【问题标题】:add watermark to the image in MySqlDB in java在java中的MySqlDB中为图像添加水印
【发布时间】:2016-07-29 03:24:28
【问题描述】:

在我的项目中,我正在尝试将水印添加到 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);
        }
    } 

我如何使用这种方法从 DB 中检索图像--> 添加水印--> 更新到 DB?我正在使用 Spring MVC。

我的照片模型类是:

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() 方法。

【问题讨论】:

    标签: java mysql spring-mvc blob


    【解决方案1】:

    五步过程就是你所需要的。

    第 1 步:

    使用选择查询从 MySQL DB 中读取图像(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。

    希望这会有所帮助。

    【讨论】:

    • 很高兴它对您有所帮助..玩得开心编码:)
    • 2 年后,这救了我的命。非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    相关资源
    最近更新 更多