编程知识 cdmana.com

Java image with text watermark and image watermark

  1: Convert byte array to local image

 /**
         *  Convert byte array to local image 
         * @param data
         * @param path
         */
    public static void byte2image(byte[] data, String path) {
            if (data.length < 3 || path.equals("")){
                return;
            }
            try {
                FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
                imageOutput.write(data, 0, data.length);
                imageOutput.close();
                System.out.println(" Image generated successfully :" + path);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
    }

2: Watermark the image

/**
 *  Add a watermark image to the background image 
 *
 * @param path           Watermark image , Local address , No network image conversion 
 * @param bgPath         Background image , ditto 
 * @param x               Abscissa , top left corner 0, Increase to the right 
 * @param y               Ordinate , top left corner 0, Up and down 
 * @param width           Watermark image width 
 * @param height          Watermark image height 
 * @return
 */
public static byte[] generateImage(String path,String bgPath , int x, int y, int width, int height) {
    try {
        BufferedImage watermarkImage = ImageIO.read(new File(path));
        BufferedImage bgImage = ImageIO.read(new File(bgPath));
        int bgWidth = bgImage.getWidth(null);
        int bgHeight = bgImage.getHeight(null);
        BufferedImage image = new BufferedImage(bgWidth, bgHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.drawImage(bgImage, 0, 0, bgWidth, bgHeight, null);
        width = width == 0 ? watermarkImage.getWidth() : width;
        height = height == 0 ? watermarkImage.getHeight() : height;
        g.drawImage(watermarkImage, x, y, width, height, null);
        g.dispose();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", out);
        return out.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

  test

public static void main(String[] args) {
    String path = "D:/image/src/main/picture/1218395382.jpg";
    String waterPath = "D:/image/src/main/picture/sijiali_s1.jpg";
    String imgResult = "D:/image/src/mina/picture/ watermark .jpg";
    byte[] bytes = generateImage(waterPath,path, 250, 10,  80, 100);
    byte2image(bytes,imgResult);
}

  effect :

Original picture

Watermark image

Result pictures

 

3: Add a text watermark to the picture

/**
 *  How to add text watermark 
 * @param pressText  Text to add 
 * @param path  File path , Only local files are processed here , Not processing web images 
 * @param x  The text is on the abscissa of the picture , The upper left corner of 0, Increase to the right 
 * @param y  The text is in the ordinate of the picture , The upper left corner of 0, Up and down 
 * @param fontName  The font name of the text 
 * @param fontSize  Size of text 
 * @param fontColor  The eyes of words 
 * @param style  The format of the text , Such as Font.BOLD++Font.ITALIC It means bold and italicized 
 * @return
 */
private static byte[] generateText(String pressText, String path, int x, int y, String fontName,
                         int fontSize, Color fontColor, int style){
    try {
        BufferedImage bgImage= ImageIO.read(new File(path));
        int wideth = bgImage.getWidth(null);
        int height = bgImage.getHeight(null);
        BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.drawImage(bgImage, 0, 0, wideth, height, null);

        // Set font size 、 Color, etc. 
        g.setColor(fontColor);
        g.setFont(new Font(fontName, style, fontSize));

        g.drawString(pressText, x, y);
        g.dispose();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", out);
        return out.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

  test

public static void main(String[] args) {
    String path = "D:/image/src/main/picture/1218395382.jpg";
    String path2 = "D:/image/src/main/picture/ Text watermarking .jpg";
    //Font.BOLD+Font.ITALIC Bold and italicized 
    byte[] bytes = generateText(" Scarlett ", path, 210, 50, " Microsoft YaHei ", 30, Color.RED, Font.BOLD+Font.ITALIC);
    byte2image(bytes,path2);
}

result :

Original picture

  Result pictures

thus , Image watermarking , The text watermark is finished !

版权声明
本文为[zsq_fengchen]所创,转载请带上原文链接,感谢

Scroll to Top