Dynamically generating JPG files directly with java command line

Source: Internet
Author: User
Tags integer

/**


* Jerugraphics v 1.0


*


* See some examples of dynamically generating images are all servlet-completed,


* and the program is very long, feel is not whether from the practicality or readability is not too good.


* Here is a snippet of code that generates an image file on the command line. Is this simple and easy to use?


*


* Create a BufferedImage object, put your "painting" into this buffer,


* Then open a file, encode the image stream and enter the file, so there is a


* jpg file appears, try it ...


* 


* Mender:


* Jeru Liu


* Homepage:


* http://javaren.126.com


* Email:jeru@163.net


*


* This is just a sample program, nothing practical, but very reference value.


*


*/


import java.io.*;


import java.util.*;


import com.sun.image.codec.jpeg.*;


import java.awt.image.*;


import java.awt.*;


public class Jerugraphics {


bufferedimage image;


//create JPG files to the specified path


public void Createjpg (String path) {


try {


FileOutputStream fos = new FileOutputStream (path);


bufferedoutputstream bos = new Bufferedoutputstream (FOS);


JPEGImageEncoder encoder = jpegcodec.createjpegencoder (BOS);


Encoder.encode (image);


Bos.close ();


} catch (FileNotFoundException Fnfe) {


System.out.println (FNFE);


} catch (IOException IoE) {


System.out.println (IoE);


  }


 } 


public static void Main (string[] args) {


int width=400, height=200;


int xlength=300, ylength=150;


int count=5;


vector data=new vector ();


data.addelement (New Integer (100));


data.addelement (New Integer (120));


data.addelement (new Integer (150));


Data.addelement (New Integer (40));


data.addelement (New Integer (5));


jerugraphics JG = new Jerugraphics ();


jg.image = new BufferedImage (width, height, bufferedimage.type_int_rgb);


Graphics g = jg.image.getGraphics ();


//Draw coordinates


G.setcolor (Color.White);


g.fillrect (0, 0, width, height);


G.setcolor (Color.Blue);


G.drawline (10,height-10,10,height-10-ylength);


G.drawline (10,HEIGHT-10,10+XLENGTH,HEIGHT-10);


//Wired


int yto;


int yfrom = ((Integer) (Data.elementat (0)). Intvalue ();


for (int i=1; i<count; i++) {


yto= ((Integer) (Data.elementat (i)). Intvalue ();


G.drawline (10+i*xlength/count,height-10,10+i*xlength/count,height-15);


G.drawline (10+ (i-1) *xlength/count,yfrom,10+i*xlength/count,yto);


Yfrom=yto;


  }


jg.createjpg ("d:\\aaa.jpg");


 }


}

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.