Java code
Package image;
Import java. awt. AlphaComposite;
Import java. awt. Color;
Import java. awt. Font;
Import java. awt. Graphics;
Import java. awt. Graphics2D;
Import java. awt. Image;
Import java. awt. image. BufferedImage;
Import java. awt. image. ColorModel;
Import java. awt. image. PixelGrabber;
Import java. io. File;
Import java. io. FileOutputStream;
Import javax. imageio. ImageIO;
Import com.sun.image.codec.jpeg. Unzip codec;
Import com.sun.image.codec.jpeg. encode imageencoder;
Public class WaterMark {
/**
* Obtain the pixel matrix in the specified rectangle.
*
* @ Param imageSrc
* @ Param startX
* @ Param startY
* @ Param w
* @ Param h
* @ Return
*/
Private int [] getPixArray (Image imageSrc, int startX, int startY,
Int w, int h ){
Int [] pix = new int [(w-startX) * (h-startY)];
/* The following is a section in other people's programs. I really don't understand why I want to add this section, because I have no problem removing it, and an error will be reported */
PixelGrabber pg = null;
Try {
Pg = new PixelGrabber (imageSrc, startX, startY, w-startX, h-startY, pix, 0, w );
If (pg. grabPixels ()! = True ){
Try {
Throw new java. awt. AWTException ("pg error" + pg. status ());
} Catch (Exception eq ){
Eq. printStackTrace ();
}
}
} Catch (Exception ex ){
Ex. printStackTrace ();
}
Return pix;
}
/**
* Overlap one image with the specified area of the other image. It can be used to make watermarks. The coordinates in the upper left corner of the image are 0, 0.
*
* @ Param lightnessWaterImg
* Bright watermark image, suitable for darker background color
* @ Param darknessWaterImg
* A watermark image with relatively dark colors is suitable for scenes with relatively bright background colors. If you do not want to distinguish it, the input is null, and the average gray border is invalid at the same time.
* @ Param targetImg
* Source image
* @ Param startX
* @ Param startY
* @ Param x
* @ Param y
* @ Param alpha
* Transparency. 0f indicates full transparency, 1f indicates full opacity, and 0.5f indicates translucent.
* @ Param averageGray
* Average gray border (0-255). If it is greater than this value, the watermark image is dimmed. If it is smaller than this value, the watermark image is highlighted.
* The default value is 128. If the value exceeds the specified range, use the default value.
*/
Private final void pressImage (String lightnessWaterImg,
String darknessWaterImg, String targetImg, int startX, int startY,
Int x, int y, float alpha, float averageGray ){
Try {
// First, determine whether the watermark and source file values are null. Otherwise, an exception is thrown.
If (lightnessWaterImg = null | lightnessWaterImg = ""
| TargetImg = null | targetImg = ""){
Throw new Exception ("the address of the highlighted watermark or source image cannot be blank ");
}
// Determine whether the average gray-scale boundary is out of bounds
If (averageGray> 255 | averageGray <0 ){
Averagegray= 128;
}
// Load the source Image
File _ file = new File (targetImg );
// Attach images to memory
BufferedImage src = ImageIO. read (_ file );
// Obtain the image size
Int width = src. getWidth (null );
Int height = src. getHeight (null );
// Set a pre-loaded image based on the source image size. The default format is RGB.
BufferedImage image = new BufferedImage (width, height,
BufferedImage. TYPE_INT_RGB );
Graphics2D graphics = image. createGraphics ();
// Draw the source image in the memory to the specified rectangle
Graphics. drawImage (src, 0, 0, width, height, null );
// Add a transparency channel to the drawn Image
Graphics. setComposite (AlphaComposite. getInstance (