These two days, I have been uploading images according to my needs. To be honest, I don’t know that Java can operate like this. Since there is a requirement, I went to find the information and studied it. I will share it now, hoping to help brothers in need.
Adding watermarks to ordinary pictures is different from adding watermarks to animations. Adding watermarks to ordinary pictures is written in Java's own method. The gif4j framework is used for animations. There are many frameworks that can be downloaded in CSDN. It is recommended to download the cracked version, because the original jar package will have a watermark that cannot be used.
import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.ImageIcon; //This is the class of the gif4j framework import com.gif4j.GifDecoder; import com.gif4j.GifEncoder; import com.gif4j.GifImage; import com.gif4j.GifTransformer; import com.gif4j.TextPainter; import com.gif4j.Watermark; /** * Created by ZXD on 2018/1/18. */ public class ImageRemarkUtil { // watermark transparency private float alpha = 0.5f; // watermark horizontal position private int positionWidth = 150; // watermark vertical position private int positionHeight = 300; // watermark width private int width = 80; // watermark high private int height = 80; // watermark text font private Font font = new Font("宋体", Font.BOLD, 72); // watermark text color private Color color = Color.red; /********************* Normal image with watermark**********/ /** * * @param alpha * Watermark transparency* @param positionWidth * Watermark horizontal position* @param positionHeight * Watermark vertical position* @param font * Watermark text font* @param color * Watermark text color*/ public void setImageMarkOptions(float alpha, int positionWidth, int positionHeight,int width,int height, Font font, Color color) { if (alpha != 0.0f) this.alpha = alpha; if (positionWidth != 0) this.positionWidth = positionWidth; if (positionHeight != 0) this.positionHeight = positionHeight; if (height != 0) this.height = height; if (width != 0) this.width = width; if (font != null) this.font = font; if (color != null) this.color = color; } /** * Add a watermark image to the image* * @param iconPath * Watermark image path* @param srcImgPath * Source image path* @param targetPath * Target image path */ public void markImageByIcon(String iconPath, String srcImgPath, String targetPath) { markImageByIcon(iconPath, srcImgPath, targetPath, null); } /** * Add a watermark image to the image, and set the rotation angle of the watermark image* * @param iconPath * Watermark image path* @param srcImgPath * Source image path* @param targetPath * Target image path* @param degree * Watermark image rotation angle*/ public void markImageByIcon(String iconPath, String srcImgPath, String targetPath, Integer degree) { OutputStream os = null; try { Image srcImg = ImageIO.read(new File(srcImgPath)); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); // 1. Get the brush object Graphics2D g = buffImg.createGraphics(); // 2. Set the jagged edge processing of the line segment g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage( srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); // 3. Set the watermark rotation if (null != degree) { g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2); } // 4. The path of the watermark image The watermark image is generally gif or png, so that the transparency can be set ImageIcon imgIcon = new ImageIcon(iconPath); // 5. Get the Image object. Image img = imgIcon.getImage(); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); Integer X = srcImg.getWidth(null); Integer Y = srcImg.getHeight(null); // 6. The location of the watermark image g.drawImage(img, X-(positionWidth+width), Y-(positionHeight+height), width,height,null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); // 7. Release the resource g.dispose(); // 8. Generate image os = new FileOutputStream(targerPath); ImageIO.write(buffImg, "JPG", os); System.out.println("Image Complete Add Watermark Image"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != os) os.close(); } catch (Exception e) { e.printStackTrace(); } } } } /** * Add watermark text to the image * * @param logoText * Watermark text* @param srcImgPath * Source image path* @param targetPath * Target image path*/ public void markImageByText(String logoText, String srcImgPath, String targetPath) { markImageByText(logoText, srcImgPath, targetPath, null); } /** * Add watermark text to the image, and set the rotation angle of the watermark text* * @param logoText * @param logoText * @param srcImgPath * @param targetPath * @param degree */ public void markImageByText(String logoText, String srcImgPath, String targetPath, Integer degree) { InputStream is = null; OutputStream os = null; try { // 1. Source image Image srcImg = ImageIO.read(new File(srcImgPath)); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); // 2. Get the brush object Graphics2D g = buffImg.createGraphics(); // 3. Set the jagged edge processing of the line segment g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage( srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); // 4. Set watermark rotation if (null != degree) { g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2); } // 5. Set watermark text color g.setColor(color); // 6. Set watermark text Font g.setFont(font); // 7. Set watermark text transparency g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 8. The first parameter -> set content, the next two parameters -> coordinate position of the text on the picture (x,y) g.drawString(logoText, positionWidth, positionHeight); // 9. Release the resource g.dispose(); // 10. Generate the image os = new FileOutputStream(targerPath); ImageIO.write(buffImg, "JPG", os); System.out.println("Image complete add watermark text"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != is) is.close(); } catch (Exception e) { e.printStackTrace(); } try { if (null != os) os.close(); } catch (Exception e) { e.printStackTrace(); } } } /********************* The animation image is watermarked****************/ /** * Scaling the gif image, directly passing the File file, you can set the width and height*/ public void makeGif(File src, File dest, int width, int height) throws IOException { GifImage gifImage = GifDecoder.decode(src);// Create a GifImage object. GifImage resizeIMG = GifTransformer.resize(gifImage, width, height, true); GifEncoder.encode(resizeIMG, dest); } //Scale the gif image, directly pass the file path, and set the width and height public void makeGif(String src, String dest, int width, int height) throws IOException { GifImage gifImage = GifDecoder.decode(new File(src));// Create a GifImage object. makeGif(new File(src), new File(dest), gifImage.getScreenWidth() / 2, gifImage.getScreenHeight() / 2); } //Scaling gif image, passing file file, width and height cannot be set public void makeGif(File src, File dest) throws IOException { GifImage gifImage = GifDecoder.decode(src);// Create a GifImage object. makeGif(src, dest, gifImage.getScreenWidth() / 2, gifImage.getScreenHeight() / 2); } //Scaling gif image, passing file path, and not setting width and height public void makeGif(String src, String dest) throws IOException { makeGif(new File(src), new File(dest)); } /** * Add text watermark to the animation*/ public void addTextWatermarkToGif(File src, String watermarkText, File dest)throws IOException { //Watermark initialization and setting (font, style, size, color) TextPainter textPainter = new TextPainter(new Font("bold", Font.ITALIC, 12)); textPainter.setOutlinePaint(Color.WHITE); BufferedImage renderedWatermarkText = textPainter.renderString(watermarkText, true); //Image object GifImage gf = GifDecoder.decode(src); //Get the image size int iw = gf.getScreenWidth(); int ih = gf.getScreenHeight(); //Get the watermark size int tw = renderedWatermarkText.getWidth(); int th = renderedWatermarkText.getHeight(); //Watermark Point p = new Point(); px = iw - tw - 5; py = ih - th - 4; //Add watermark Watermark Watermark = new Watermark(renderedWatermarkText, p); gf = watermark.apply(GifDecoder.decode(src), true); //Output GifEncoder.encode(gf, dest); } /** * Add image watermark in the animation*/ public void addImageWatermarkToGif(File src, String watermarkPath, File dest){ try{ BufferedImage renderedWatermarkText = ImageIO.read(new File(watermarkPath)); //Image object GifImage gf = GifDecoder.decode(src); //Get the image size int iw = gf.getScreenWidth(); int ih = gf.getScreenHeight(); //Get the watermark size int tw = renderedWatermarkText.getWidth(); int th = renderedWatermarkText.getHeight(); //Watermark position Point p = new Point(); px = iw-tw-20; py = ih-th-20; //Watermark Watermark watermark = new Watermark(renderedWatermarkText, p); //Watermark transparency watermark.setTransparency(1); gf = watermark.apply(GifDecoder.decode(src), false); //Output GifEncoder.encode(gf, dest); } catch (IOException e){ e.printStackTrace(); } } public static void main(String[] args) { //Path where the image needs to be watermarked String srcImgPath = "d:/1.jpg"; String logoText = "Copy invalid"; //The path of the image watermark String iconPath = "d:/2.jpg"; //The output path of the watermark file String targetTextPath = "d:/qie_text.jpg"; String targetTextPath2 = "d:/qie_text_rotate.jpg"; String targetIconPath = "d:/qie_icon.jpg"; String targetIconPath2 = "d:/qie_icon_rotate.jpg"; System.out.println("To start adding watermark text to the image..."); //The image watermark text markImageByText(logoText, srcImgPath, targetTextPath); // Add watermark text to the image, watermark text rotation -45 markImageByText(logoText, srcImgPath, targetTextPath2, -45); System.out.println("Add watermark text to the image, end..."); System.out.println("Add watermark image to the image, start..."); setImageMarkOptions(0.3f, 1, 1, null, null); // Add watermark image to the image, watermark image rotation -45 markImageByIcon(iconPath, srcImgPath, targetIconPath2, -45); System.out.println("Add watermark to the image ends..."); //Add watermark in the animation image (add watermark animation file, add watermark, add output file) addTextWatermarkToGif(new File("d://10.gif"), "copy invalid", new File("d://11.gif")); addImageWatermarkToGif(new File("d://gif//10.gif"), "d://gif//3.png", new File("d://gif//4.gif")); } }Here are the methods of adding watermarks to ordinary pictures and adding watermarks to animations. If the method of adding watermarks to ordinary pictures can be successfully added if the image is transmitted, the image will become static. If the method of adding watermarks to animations is transmitted, the error will be reported directly.
I have tried these when I was doing them, so I will take notes and record them here now, and I hope they can help brothers in need.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.