Java generates JPEG image files

Source: Internet
Author: User
Java generates JPEG image files. The code is as follows:





My code is mainly provided for the Workflow Designer object that I write myself (Eworkflowcontrol EWF), Eworkflowcontrol inherits the method that JComponent overloads the drawing, and if a brother needs to use this function, You just need to replace Eworkflowcontrol with your own JComponent object.





/** generates a JPEG image file for graphics on the current input workflow object.





* Each node corresponds to a picture.





* Established files are stored under the Jar file folder and a subfolder is created





* With the name of the node as the name of the folder.





* @param EWF A Workflow object that needs to generate a JPEG picture file.





* @return Boolean: Returns a Boolean value that indicates whether the save was successful.





*/





public static Boolean buildthepictrue (Eworkflowcontrol EWF)





{





//modify by MW





//Lists all node nodes in the process.





//Set these nodes as selected, and coexist into pictures (serial numbers with name nodes)





INode RootNode = Ewf.getnote (); Rectangle bounds = Ewf.getbounds ();





inode[] Children = Rootnode.getnodechildren ();





int childrenlen = Rootnode.getnodechildcount ();





String nodenumber = null;





Boolean flag = true;





//Eliminate all nodes in the selected state.





for (int j = 0; J < Childrenlen; J + +) {





Children[j].setselect (FALSE); }





//Generate general graph (I am a node an image)





try {





java.awt.image.BufferedImage image; Image = New Java.awt.image.BufferedImage (Bounds.width, Bounds.height, Java.awt.image.BufferedImage.TYPE_INT_RGB);





Graphics g = image.getgraphics (); G.setcolor (Color.White);





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





Ewf.paintcomponent (g);





String Path = System.getproperty ("User.dir") +system.getproperty ("File.separator") +ewf.getworkflowname () + "[" + Ewf.getworkflowcode () + "]";





file FD = new file (path);





if (!fd.exists ()) {





Fd.mkdirs ();





}





String filePath = path + system.getproperty ("file.separator") + "0.jpeg";





file Imgfile = new file (FilePath);





FileOutputStream fos = new FileOutputStream (imgfile);





bufferedoutputstream bos = new Bufferedoutputstream (FOS);





JPEGImageEncoder encoder = jpegcodec.createjpegencoder (BOS);





Encoder.encode (image); Bos.close ();





}





catch (Exception ex) {





flag = false;





}





//To this first image has been generated!





//looping to generate pictures





for (int i = 0; i < Childrenlen; i++)





{





Nodenumber = Children[i].getnodenumber ();





if (Nodenumber = null)





{





flag = false; Continue





} else {





Children[i].setselect (TRUE);





try {





java.awt.image.BufferedImage image; Image = New Java.awt.image.BufferedImage (Bounds.width, Bounds.height, Java.awt.image.BufferedImage.TYPE_INT_RGB);





Graphics g = image.getgraphics ();





G.setcolor (Color.White);





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





Ewf.paintcomponent (g);





String Path = System.getproperty ("User.dir") +system.getproperty ("File.separator") +ewf.getworkflowname () + "[" + Ewf.getworkflowcode () + "]";





file FD = new file (path);





if (!fd.exists ()) {





Fd.mkdirs ();





}





String FilePath = path + system.getproperty ("File.separator") +nodenumber+ ". jpeg";





file Imgfile = new file (FilePath);





FileOutputStream fos = new FileOutputStream (imgfile);





bufferedoutputstream bos = new Bufferedoutputstream (FOS); JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (BOS);





Encoder.encode (image); Bos.close ();





Children[i].setselect (FALSE);





} catch (Exception ex) {





flag = false;





} } }





if (flag) {





New Exceptiondialog (Pic_save_ok,pic_save_dialog_title, Desktopframe.getcurrentinstance (), true). SetVisible ( true);





} else {





New Exceptiondialog (Pic_save_error,pic_save_dialog_title, Desktopframe.getcurrentinstance (), true). setVisible (true);





}





return flag;





}

















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.