Home > Back-end >  To embed the watermark image, the local environment can be normal operation, the test environment wi
To embed the watermark image, the local environment can be normal operation, the test environment wi

Time:10-10

Code snippets:
/* *
* to add text watermarking images, image watermark
* @ param map
*/
Public void markImageByText (Map Map) {
MarkImageByText System. Out. Println (" -- -- -- -- -- -- -- -- -- -- to -- -- -- -- -- -- -- -- -- -- Start ");
String srcImgPath=map. Get (" srcImgPath ");//the original image path
String newImagePath=map. Get (" newImgPath ");//target image path
String logText=map. Get (" logText ");//watermark text
InputStream is=null;
OutputStream OS=null;
Try {
//to add a watermark image
Image srcImg=ImageIO. Read (new File (srcImgPath));
Int width=srcImg. GetWidth (null);//the original width
Int height=srcImg. GetHeight (null);//the original height
BufferedImage buffImg=new BufferedImage (srcImg getWidth (null), srcImg. GetHeight (null), BufferedImage. TYPE_INT_RGB);
//get the brush object
Graphics2D g=buffImg. CreateGraphics ();
//set of line segments serrated edge processing
G.s etRenderingHint (RenderingHints KEY_INTERPOLATION, RenderingHints VALUE_INTERPOLATION_BILINEAR);
G.d rawImage (srcImg getScaledInstance (srcImg getWidth (null), srcImg. GetHeight (null), Image, SCALE_SMOOTH), 0, 0, null);
//set the watermark rotate
G.r otate (math.h toRadians (45), (double) buffImg. GetWidth ()/2, (double) buffImg. GetHeight ()/2);
//set the watermark text color
G.s etColor (Color. GRAY);
//set text watermark transparency
G.s etComposite (AlphaComposite. GetInstance (0.3 f) AlphaComposite. SRC_ATOP);
For (int j=0; J & lt; Height; J++) {
for (int i=0; i G.d rawString (logText, 30, 120 + j * I * - I * 120 + j * 30);//draw the watermarking, and sets the watermark position
}
}
Try {
//get the brush object
Graphics2D g1=buffImg. CreateGraphics ();
//the path of the watermark
ClassPathResource c2=new ClassPathResource (" template/manage/watermark. JPG ");
InputStream inputStream2=c2. GetInputStream ();
The File f2=File. CreateTempFile (" test ", "JPG");
Try {
FileUtils. CopyInputStreamToFile (inputStream2, f2);
} the finally {
IOUtils. CloseQuietly (inputStream2);
}
//watermark picture
ImageIcon imgIcon=new ImageIcon (f2 getPath ());
//get the Image object,
Image syImg=imgIcon. GetImage ();
Float alpha=0.5 f;//transparency
Int syWidth=syImg. GetWidth (null);//the width of the watermark
Int syHeight=syImg. GetHeight (null);//the height of the watermark
//if the watermark image is doing high or wide in the target image processing, make the watermark width or height is equal to the target image is high, wide and scaling, such as
Int newSyWidth=syWidth;
Int newSyHeight=syHeight;
If (syWidth & gt; Width) {
NewSyWidth=width;
NewSyHeight=(int) ((double) newSyWidth/syWidth * height);
}
If (newSyHeight & gt; Height) {
NewSyHeight=height;
NewSyWidth=(int) ((double) newSyHeight/syHeight * newSyWidth);
}
//according to the location parameter to determine the coordinates
Int a=0, b=0;
//the top right corner
A=width - newSyWidth;
G1. SetComposite (AlphaComposite. GetInstance (AlphaComposite. SRC_ATOP, alpha));
//said the location of the watermark image
G1. DrawImage (syImg, a, b, newSyWidth, newSyHeight, null);
G.d ispose ();//release resources
//watermark end of file
G1. The dispose ();
OS=new FileOutputStream (newImagePath);
ImageIO. Write (buffImg, "JPG", OS).
MarkImageByText System. Out. Println (" -- -- -- -- -- -- -- -- -- -- end -- -- -- -- -- -- -- -- -- -- Start ");
} the catch (Exception e) {
e.printStackTrace();
} the finally {
Try {
if (null !=is)
is.close();
} the catch (Exception e) {
e.printStackTrace();
}
Try {
if (null !=OS)
os.close();
} the catch (Exception e) {
e.printStackTrace();
}
}
} the catch (Exception e) {
e.printStackTrace();
}
}


Error message:
Java. Lang. NullPointerException
At the sun. The awt. FontConfiguration. GetVersion (FontConfiguration. Java: 1264)
At the sun. The awt. FontConfiguration. ReadFontConfigFile (FontConfiguration. Java: 219)
At the sun. The awt. FontConfiguration. Init (FontConfiguration. Java: 107)
At the sun. The awt. X11FontManager. CreateFontConfiguration (X11FontManager. Java: 774)
At sun. The font. SunFontManager $2. The run (431) SunFontManager. Java:
The at Java. Security. The AccessController. DoPrivileged (Native Method)
At the sun. The font. SunFontManager. & lt; Init> (SunFontManager. Java: 376)
At the sun. The awt. FcFontManager. & lt; Init> (FcFontManager. Java: 35)
At the sun. The awt. X11FontManager. & lt; Init> (X11FontManager. Java: 57)
At sun. Reflect. NativeConstructorAccessorImpl. NewInstance0 (Native Method)
At sun. Reflect. NativeConstructorAccessorImpl. NewInstance (NativeConstructorAccessorImpl. Java: 62)
At sun. Reflect. DelegatingConstructorAccessorImpl. NewInstance (DelegatingConstructorAccessorImpl. Java: 45)
The at Java. Lang. Reflect. Constructor. NewInstance (423) Constructor. Java:
The at Java. Lang. Class. NewInstance (Class. Java: 442)
At sun. The font. The FontManagerFactory $1. The run (83) FontManagerFactory. Java:
The at Java. Security. The AccessController. DoPrivileged (Native Method)
At the sun. The font. FontManagerFactory. GetInstance (FontManagerFactory. Java: 74)
At the sun. The font. SunFontManager. GetInstance (SunFontManager. Java: 250)
At the sun. The font. FontDesignMetrics. GetMetrics (FontDesignMetrics. Java: 264)
At sun. Swing. SwingUtilities2. GetFontMetrics (SwingUtilities2. Java: 1107)
The at javax.mail. Swing. Jcomponents. GetFontMetrics (jcomponents. Java: 1617)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related