Background:
wrote a test program to read the local image, and to make a watermark image for it. In the use of the process will always encounter: Javax.imageio.IIOException:Can ' t read input file! error, the first thought is that the picture path name is not written, follow the online prompts for forward slashes and backslashes are not. later found that the cause of the problem is: the path of the picture can not be a bit (English point:.);
The specific error exception prompt is as follows:
Javax.imageio.IIOException:Can ' t read input file! At javax.imageio.ImageIO.read (Unknown Source) at Com.bomei.image.ImageUtils.pressImage ( Imageutils.java:at com.bomei.image.ImageUtils.main (Imageutils.java:117)
The specific code is as follows:
PackageCom.bomei.image;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;ImportJava.awt.Image;ImportJava.awt.image.BufferedImage;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJavax.imageio.ImageIO;ImportCom.sun.image.codec.jpeg.JPEGCodec;ImportCom.sun.image.codec.jpeg.JPEGImageEncoder; Public Final classImageutils { Publicimageutils () {}/** Public final static String Getpressimgpath () {return applicationcontext *. Getrealpath ("/template/data/uti L/shuiyin.gif "); } */ /*** Print the picture to the picture * *@parampressimg--* Watermark file *@paramtargetimg--* Target file *@paramx *--x coordinates *@paramy *--y coordinates*/ Public Final Static voidpressimage (String pressimg, String targetimg,intXinty) {Try { //target fileFile _file =NewFile (TARGETIMG); Image src=Imageio.read (_file); intWideth = Src.getwidth (NULL); intHeight = src.getheight (NULL); BufferedImage Image=Newbufferedimage (wideth, height, bufferedimage.type_int_rgb); Graphics g=Image.creategraphics (); G.drawimage (SRC,0, 0, wideth, height,NULL); //Watermark FileFile _filebiao =NewFile (PRESSIMG); Image Src_biao=Imageio.read (_filebiao); intWideth_biao = Src_biao.getwidth (NULL); intHeight_biao = Src_biao.getheight (NULL); G.drawimage (Src_biao, (Wideth-Wideth_biao)/2, (Height-Height_biao)/2, Wideth_biao, Height_biao,NULL); //Watermark File EndG.dispose (); FileOutputStream out=NewFileOutputStream (TARGETIMG); JPEGImageEncoder Encoder=Jpegcodec.createjpegencoder (out); Encoder.encode (image); Out.close (); } Catch(Exception e) {e.printstacktrace (); } } /*** Print Text watermark image * *@paramPresstext *--Text *@paramtargetimg--* Target image *@paramfontname--* Font name *@paramfontstyle--* Font style *@paramColor --* Font colors *@paramfontSize--* Font size *@paramX--* offset *@paramy*/ Public Static voidPresstext (String presstext, String targetimg, String fontname,intFontStyle,intColorintFontSize,intx,inty) {Try{File _file=NewFile (TARGETIMG); Image src=Imageio.read (_file); intWideth = Src.getwidth (NULL); intHeight = src.getheight (NULL); BufferedImage Image=Newbufferedimage (wideth, height, bufferedimage.type_int_rgb); Graphics g=Image.creategraphics (); G.drawimage (SRC,0, 0, wideth, height,NULL); //String s= "www.qhd.com.cn";G.setcolor (color.red); G.setfont (NewFont (FontName, FontStyle, fontSize)); g.DrawString (Presstext, Wideth-Fontsize-x, Height-fontSize/2-y); G.dispose (); FileOutputStream out=NewFileOutputStream (TARGETIMG); JPEGImageEncoder Encoder=Jpegcodec.createjpegencoder (out); Encoder.encode (image); Out.close (); } Catch(Exception e) {System.out.println (e); } } Public Static voidMain (string[] args) {//pressimage ("C:/test/safeqrcode.jpg", "C:/test/poster.png", 0, 0);Pressimage ("C:\\users\\g.wang\\desktop\\two_dimension_code\\safeqrcode.jpg", "C:/test/poster.png", 0, 0); }}
As shown in the main function in the code above, the first parameter of the method invocation contains the path of the G.wang, which contains the full period (.) of the English, which causes the above exception to appear and the exception will be reported at (G.drawimage). Changing the position of the picture to a path that does not contain English points will solve the problem. As follows:
Pressimage ("C:/test/safeqrcode.jpg", "C:/test/poster.png", 0, 0);
Another thing to say is, on the internet a lot of pictures of the watermark image of the code are not so good, are a turn one. Find a long time to find an article so that the original link is as follows:
Http://my.oschina.net/jgy/blog/55702?fromerr=AuPHExZk
This article introduces a watermark image and watermark text method is to make it.
Path name-caused exception: Javax.imageio.IIOException:Can ' t read input file!