Today I encountered a need to fill the uploaded image with watermarks. I searched online for a long time and set the watermark at the specified location. The following code was transformed by the code I found online. Let's take a look at the effect picture first
The implementation method is discussed below:
Step 1: Use ps or Meitu software to design watermark pictures, such as:
Step 2: Put the following code into the class:
package org.platform.framework.commons.util;import java.awt.AlphaComposite;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.Image;import java.awt.RenderingHints;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import com.founder.cms.watermark.util.ImageMarkLogoUtil;/** * * Generate watermark* */public class ImageMarkUtil { /** Watermark transparency*/ private static float alpha = 0.5f; /** Watermark image rotation angle*/ private static double degree = 0f; private static int interval = 0; /** * Set watermark parameters, use the default value without setting* * @param alpha * Watermark transparency* @param degree * Watermark image rotation angle* * @param interval * Watermark image interval*/ public static void setImageMarkOptions(float alpha, int degree, int interval) { if (alpha != 0.0f) { ImageMarkUtil.alpha = alpha; } if (degree != 0f) { ImageMarkUtil.degree = degree; } if (interval != 0f) { ImageMarkUtil.interval = interval; } } /** * Add a watermark image to the image* * @param waterImgPath * Watermark image path* @param srcImgPath * Source image path* @param targetPath * Target image path*/ public static void waterMarkByImg(String waterImgPath, String srcImgPath, String targetPath) throws Exception { waterMarkByImg(waterImgPath, srcImgPath, targetPath, 0); } /** * Add a watermark image to the image* * @param waterImgPath * Watermark image path* @param srcImgPath * Source image path* @param targetPath * Target image path*/ public static void waterMarkByImg(String waterImgPath, String srcImgPath) { try { waterMarkByImg(waterImgPath, srcImgPath, srcImgPath, 0); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * Add a watermark image to the image, and set the rotation angle of the watermark image* * @param waterImgPath * Watermark image path* @param srcImgPath * Source image path* @param targetPath * Target image path* @param degree * Watermark image rotation angle*/ public static void waterMarkByImg(String waterImgPath, String srcImgPath, String targetPath, double degree) throws Exception { 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 (0 != 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(waterImgPath); // 5. Get the Image object. Image img = imgIcon.getImage(); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 6. The location of the watermark image is for (int height = interval + imgIcon.getIconHeight(); height < buffImg .getHeight(); height = height +interval+ imgIcon.getIconHeight()) { for (int weight = interval + imgIcon.getIconWidth(); weight < buffImg .getWidth(); weight = weight +interval+ imgIcon.getIconWidth()) { g.drawImage(img, weight - imgIcon.getIconWidth(), height - imgIcon.getIconHeight(), 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 finished adding watermark image"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != os) os.close(); } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) { System.out.println("..begin adding watermark image..."); /** * watermarkPath Watermark image address uploadPath File address after successful upload*/ //Modify the default parameters//ImageMarkUtil.setImageMarkOptions(0.0f, 0, 20); //ImageMarkUtil.waterMarkByImg(watermarkPath, uploadPath); System.out.println(".. Add watermark image to end..."); }} Step 3: Where the upload is successful, call this class, such as:
/** * watermarkPath Watermark image address* uploadPath File address after successful upload*/ ImageMarkUtil.waterMarkByImg(watermarkPath, uploadPath); If you think the watermark interval is too close, you can set the interval through the following code: //Modify the default parameters ImageMarkUtil.setImageMarkOptions(0.0f, 0, 20); ImageMarkUtil.waterMarkByImg(watermarkPath, uploadPath);
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.