java generates JPG Picture:
Package other.pic;
Import Com.sun.image.codec.jpeg.JPEGCodec;
Import Com.sun.image.codec.jpeg.JPEGImageEncoder;
Import Java.io.BufferedOutputStream;
Import Java.io.FileOutputStream;
Import Java.io.RandomAccessFile;
Import Java.awt.image.BufferedImage;
Import java.awt.*;
Import Java.util.Random;
/** * Created by the IntelliJ idea.
* User:administrator * date:2005-6-10 * time:11:19:49 * This class can create a JPG picture. * To change this template use File | Settings |
File Templates.
* * public class Chartgraphics {bufferedimage image;
void CreateImage (String filelocation) {try {fileoutputstream fos = new FileOutputStream (filelocation);
Bufferedoutputstream BOS = new Bufferedoutputstream (FOS);
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (BOS);
Encoder.encode (image);
Bos.close ();
catch (Exception e) {e.printstacktrace (); } public void Graphicsgeneration (int h1, int h2, int h3, int h4, int h5) {final int X = 10; int imagewidth = 300;//width int imageheight = 300;//picture height int columnWidth = 30;//column width int columnheight = 200;
Maximum height of column//chartgraphics chartgraphics = new Chartgraphics ();
Image = New BufferedImage (ImageWidth, ImageHeight, Bufferedimage.type_int_rgb);
Graphics Graphics = Image.getgraphics ();
Graphics.setcolor (Color.White);
Graphics.fillrect (0, 0, imagewidth, imageheight);
Graphics.setcolor (color.red);
Graphics.drawrect (X + 1 * columnWidth, COLUMNHEIGHT-H1, ColumnWidth, H1);
Graphics.drawrect (X + 2 * columnWidth, COLUMNHEIGHT-H2, ColumnWidth, H2);
Graphics.drawrect (X + 3 * columnWidth, COLUMNHEIGHT-H3, ColumnWidth, H3);
Graphics.drawrect (X + 4 * columnWidth, COLUMNHEIGHT-H4, ColumnWidth, H4);
Graphics.drawrect (X + 5 * columnWidth, Columnheight-h5, ColumnWidth, h5);
CreateImage ("d://temp//chart.jpg");
public static void Main (string[] args) {int[] height = {40,50,16,22,85}; ChartgraphICS Cg=new Chartgraphics (); try {for (int i = 0; i < 5; i++) {cg.graphicsgeneration (height[0],height[1],height[2],height[3],height[
4]);
} catch (Exception e) {e.printstacktrace ();
}
}
}
The following is the generated picture
Java compressed picture for JPG thumbnails
we want to upload information on the site to share with you, will encounter a problem like this, with the popularity of high resolution DC, upload the picture capacity will be very large, For example, 3 million pixel dc out of the file basically less than 600K. In order to manage conveniently, you may not want to use ACDSee to modify it every time, and upload directly to the server. But this approach is not so easy on the client side, for dial-up users is simply a nightmare, although you can set up in the picture area wide and high! problem solution! We can work with a larger picture in a class and narrow it down. The premise is that JDK1.4 needs to be done in order to be processed. Do as follows:
import java.io.File;
Import Java.io.FileOutputStream;
Import Java.awt.Graphics;
Import Java.awt.Image;
Import Java.awt.image.BufferedImage;
Import Com.sun.image.codec.jpeg.JPEGCodec;
Import Com.sun.image.codec.jpeg.JPEGImageEncoder;
public class Jpgtest {public void Jpgtset () throws exception{file _file = new file ("/order005-0001.jpg");/Read files Image src = javax.imageio.ImageIO.read (_file); Constructs an Image object int wideth=src.getwidth (NULL); Get the source map wide int height=src.getheight (NULL);
Get source graph long bufferedimage tag = new BufferedImage (WIDETH/2,HEIGHT/2,BUFFEREDIMAGE.TYPE_INT_RGB); Tag.getgraphics (). DrawImage (Src,0,0,wideth/2,height/2,null); Draw the narrowed figure FileOutputStream out=new fileoutputstream ("newfile.jpg");
Output to file stream JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out); Encoder.encode (tag);
Near-JPEG coding System.out.print (width+ "*" +height);
Out.close (); }}
The process is simple, read the file from the local disk order005-0001.jpg (2032*1524), into the Image object SRC, and then construct the target file tag, set the length of the tag as the source of the half, the tag to encode, output to the file stream out, and finally close the file flow.
There are also some questions to note: First, only jpg (JPEG), GIF, PNG Three formats can be supported at present. Second, for the source map capacity of limited system, it is best not to exceed 1M, otherwise it will throw out the memory error, but I tested the source map of 1.8M, can be successful, but also very easy to throw out memory. Quote a predecessor: The image operation itself is a dense operation, requiring a large amount of memory to store pixel values. I tried with VC, 4M image also has problems, and the more compression than the large picture in memory to restore to bitmap when the memory is larger. Solution, you can rewrite the encoding class, first open a certain amount of memory, and then a paragraph of the code to write to the temporary file, the output of a paragraph read out. Or use the memory image of NiO to operate. JavaMail because of the builder mode, Sir, each part of a message is merged into a full mail object, so that each widget is generated into memory first, and if you send a hundreds of megabytes of attachments, then the memory overflow is definitely in the construction parts, So I rewrote the structure of BodyPart, let him associate with a temporary file, and then save part instead of in memory with a temporary file, so that any attachment of any size (the hard drive can be placed to the limit) can be sent.
Finally, if you have a higher demand for image processing, you might want to focus on open source projects. For example Jmagick, you can use Jmagick to achieve image reproduction, information acquisition, bevel, special effects, combinations, resize, add borders, rotate, slice, change format, go color and so on.
PS: Some old Java code compiled under the JDK will be an error, such as this: the package com.sun.image.codec.jpeg does not exist.
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
JPEGImageEncoder class is Sun company Private class
Typically appears in a code snippet like this:
FileOutputStream out = new FileOutputStream (dstname);
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (Dstimage);
Rewrite as:
String FormatName = dstname.substring (Dstname.lastindexof (".") + 1);
FileOutputStream out = new FileOutputStream (dstname);
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (dstimage);
Imageio.write (dstimage, * "GIF" * * formatname/* Format desired/*, new File (dstname)/* target *);
Use unified ImageIO to read and write image format files, it is not necessary to use the obsolete implementation class JPEGImageEncoder class.