Java generates custom images based on pictures and text

Source: Internet
Author: User

ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;ImportJava.awt.Graphics2D;ImportJava.awt.image.BufferedImage;ImportJava.io.BufferedOutputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportCom.sun.image.codec.jpeg.JPEGCodec;ImportCom.sun.image.codec.jpeg.JPEGImageEncoder; Public classChartgraphics {Privatebufferedimage image; Private intImageWidth = 300;//the width of the picture    Private intImageHeight = 500;//the height of the picture//Generate picture File@SuppressWarnings ("Restriction")     Public voidcreateimage (String filelocation) {bufferedoutputstream BOS=NULL; if(Image! =NULL){            Try{FileOutputStream fos=NewFileOutputStream (filelocation); Bos=NewBufferedoutputstream (FOS); JPEGImageEncoder Encoder=Jpegcodec.createjpegencoder (BOS);                Encoder.encode (image);            Bos.close (); } Catch(Exception e) {e.printstacktrace (); }finally{                if(bos!=NULL){//turn off the output stream                    Try{bos.close (); } Catch(IOException e) {e.printstacktrace (); }                }            }        }    }      Public voidgraphicsgeneration (string name, string ID, String classname, String imgurl) {intH_title = 30;//Head Height        intH_mainpic = 200;//Carousel Ad Height        intH_tip = 60;//Internet alert Box Height        intH_BTN = 25;//height of button bar        intTip_2_top = (h_title+h_mainpic); intBtns_2_top = tip_2_top+h_tip+20; intBtn1_2_top = btns_2_top+10; intBtn2_2_top = btn1_2_top+h_btn; intShops_2_top = btn2_2_top+h_btn+20; intW_BTN = 280;//width of button barImage=Newbufferedimage (ImageWidth, ImageHeight, Bufferedimage.type_int_rgb); //set the background color of a picturegraphics2d main =Image.creategraphics ();        Main.setcolor (Color.White); Main.fillrect (0, 0, ImageWidth, imageheight); //*********************** Page HeaderGraphics title =Image.creategraphics (); //Set the area colorTitle.setcolor (NewColor (143, 0, 0)); //fills the area and determines the area size positionTitle.fillrect (0, 0, ImageWidth, H_title); //Set the font color, first set the color, then fill the contentTitle.setcolor (Color.White); //Set FontFont Titlefont =NewFont ("Arial", Font.Bold, 14);        Title.setfont (Titlefont); Title.drawstring ("My Head", H_title, (/2+5)); //*********************** inserting an intermediate ad mapGraphics Mainpic =Image.getgraphics (); BufferedImage bimg=NULL; Try{bimg= Javax.imageio.ImageIO.read (NewJava.io.File (Imgurl)); } Catch(Exception e) {}if(bimg!=NULL) {mainpic.drawimage (bimg,0, H_title, ImageWidth, H_mainpic,NULL);        Mainpic.dispose (); }        //*********************** Set the following prompt boxgraphics2d Tip=Image.creategraphics (); //Set the area colorTip.setcolor (NewColor (255, 120, 89)); //fills the area and determines the area size positionTip.fillrect (0, Tip_2_top, ImageWidth, H_tip); //Set the font color, first set the color, then fill the contentTip.setcolor (Color.White); //Set FontFont Tipfont =NewFont ("Arial", Font.plain, 14);        Tip.setfont (Tipfont); Tip.drawstring ("Login successful, this certification time 1 hours", tip_2_top+ (h_tip)/2-10); Tip.drawstring ("Returning to Business Home", tip_2_top+ (H_tip)/2+10); //*********************** Set the following button block//Set the font color, first set the color, then fill the contentTip.setcolor (Color.Black); Tip.drawstring ("You can choose the action:", 20, Btns_2_top); Tip.drawstring ("Small icon below:", 20, Shops_2_top); //*********************** ButtonFont Btnfont =NewFont ("Arial", Font.Bold, 14); Graphics2D btn1=Image.creategraphics (); Btn1.setcolor (NewColor (41,192, 50));//#29C65ABtn1.fillrect (10, Btn1_2_top, w_btn, h_btn);        Btn1.setcolor (Color.Black); Btn1.drawrect (10, Btn1_2_top, w_btn, h_btn); //btn1 TextBtn1.setcolor (Color.White);        Btn1.setfont (Btnfont); Btn1.drawstring ("Click Me", btn1_2_top+ (H_BTN/2) +5); Graphics2D btn2=Image.creategraphics (); Btn2.setcolor (NewColor (141,120, 22));//#29C65ABtn2.fillrect (10, Btn2_2_top, w_btn, h_btn);        Btn2.setcolor (Color.Black); Btn2.drawrect (10, Btn2_2_top, w_btn, h_btn); //btn2 TextBtn2.setcolor (Color.White);        Btn2.setfont (Btnfont); Btn2.drawstring ("Click Me", btn2_2_top+ (H_BTN/2) +5); CreateImage ("C:\\hehe.jpg"); }      Public Static voidMain (string[] args) {chartgraphics CG=NewChartgraphics (); Try{cg.graphicsgeneration ("Ewew", "1", "a", "e:\\work_folder\\picture\\big_pic\\1.jpg"); } Catch(Exception e) {e.printstacktrace (); }    }}

Transferred from: http://www.cnblogs.com/qianna-daisy/p/4572221.html

Java generates custom images based on pictures and text

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.