30th section (java-implementation watermark image)

Source: Internet
Author: User

Package Com.tanzhou.imagedemo;import Java.awt.color;import java.awt.font;import java.awt.graphics;import Java.awt.image;import Java.awt.image.bufferedimage;import Java.io.file;import Java.io.FileOutputStream;import Java.io.ioexception;import Javax.imageio.imageio;import Com.sun.image.codec.jpeg.jpegcodec;import Com.sun.image.codec.jpeg.JPEGImageEncoder; Public classImagedemo {/** * Print the image to the target image * @param pressimg watermark file * @param targetimg target file * @param x Horizontal Axis * @param y ordinate*/     PublicFinalStatic voidPressimage (String pressimg,string targetimg,intXinty) {        //target file        Try{File _file=NewFile (TARGETIMG);//Get PicturesImage src = imageio.read (_file);//The file object is converted to an Image object            intwidth = src.getwidth (NULL); intHeight = src.getheight (NULL); System. out. println (width+"========="+height); BufferedImage Image=NewBufferedImage (WIDTH,HEIGHT,BUFFEREDIMAGE.TYPE_INT_RGB); Graphics g= Image.creategraphics ();//Graphics is the class that handles pictures in JavaG.drawimage (SRC,0,0, Width,height,NULL); //Watermark FileFile _filebiao =NewFile (PRESSIMG); Image Src_biao=Imageio.read (_filebiao); intWidth_biao = Src_biao.getwidth (NULL);//Picture Width            intHeight_biao = Src_biao.getheight (NULL);//Picture HeightG.drawimage (Src_biao, (Width-width_biao)/2, (Height-height_biao)/2, Width_biao,height_biao,NULL); G.dispose (); //ending processing of file objectsFileOutputStream out=NewFileOutputStream (TARGETIMG); /** * Jpegcodec's role is to create a jpegimageencoder that is associated with the specified output stream*/JPEGImageEncoder Encoder= Jpegcodec.createjpegencoder ( out);            Encoder.encode (image);  out. Close (); } Catch(IOException e) {e.printstacktrace (); }     }    /** * * * @param presstext text * @param targetimg target picture * @param fontname font name * @param fontstyle Font style * @param color 0-255 * @param fontSize Size * @param x Horizontal axis * @param y ordinate */     Public Static voidPresstext (String presstext,string targetimg,string FontName,intFontStyle,intColorintFontSize,intXinty) {        Try{File File=NewFile (TARGETIMG); Image src=imageio.read (file); intwidth = src.getwidth (NULL); intHeight = src.getheight (NULL); BufferedImage Image=NewBufferedImage (WIDTH,HEIGHT,BUFFEREDIMAGE.TYPE_INT_RGB); Graphics g=Image.creategraphics (); G.drawimage (SRC,0,0, Width,height,NULL);            G.setcolor (color.red); G.setfont (NewFont (fontname,fontstyle,fontsize)); g.DrawString (presstext, Width-fontsize-x, height-fontsize/2-y);                        G.dispose (); FileOutputStream out=NewFileOutputStream (TARGETIMG); JPEGImageEncoder Encoder= Jpegcodec.createjpegencoder ( out);            Encoder.encode (image);  out. Close (); } Catch(IOException e) {e.printstacktrace (); }                    }         Public Static voidMain (string[] args) {pressimage ("C:\\users\\tony liu\\desktop\\image\\4.jpg","C:\\users\\tony liu\\desktop\\image\\5.jpg", -, -); Presstext ("-java software engineer, Zhongshan College","C:\\users\\tony liu\\desktop\\image\\5.jpg","Song Body", -, -, -, -, -); }}

30th section (java-implementation watermark image)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.