当前位置: 技术问答>java相关
在JDK1.2中如何将Image对象存成文件?
来源: 互联网 发布时间:2015-03-02
本文导语: | Take a look at the following package : http://www.obrador.com/essentialjpeg/jpeg.htm for JPEG http://www.acme.com for GIF http://rsb.info.nih.gov/ij/ can display BMP and save as GIF or TIFF With JDK1.2, Sun introduces a new package called JIMI (a...
|
Take a look at the following package :
http://www.obrador.com/essentialjpeg/jpeg.htm for JPEG
http://www.acme.com for GIF
http://rsb.info.nih.gov/ij/ can display BMP and save as GIF or TIFF
With JDK1.2, Sun introduces a new package called JIMI (available for download at their Web site. With this package, it's easy to convert a Java Image to a JPEG image file. double w = 200.0;
double h = 200.0;
BufferedImage image = new BufferedImage(
(int)w,(int)h,BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)image.getGraphics();
g.drawLine(0,0,w,h);
try {
File f = new File("myimage.jpg");
JimiRasterImage jrf = Jimi.createRasterImage(image.getSource());
Jimi.putImage("image/jpeg",jrf,new FileOutputStream(f));
}
catch (JimiException je) {je.printStackTrace();}
http://www.obrador.com/essentialjpeg/jpeg.htm for JPEG
http://www.acme.com for GIF
http://rsb.info.nih.gov/ij/ can display BMP and save as GIF or TIFF
With JDK1.2, Sun introduces a new package called JIMI (available for download at their Web site. With this package, it's easy to convert a Java Image to a JPEG image file. double w = 200.0;
double h = 200.0;
BufferedImage image = new BufferedImage(
(int)w,(int)h,BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)image.getGraphics();
g.drawLine(0,0,w,h);
try {
File f = new File("myimage.jpg");
JimiRasterImage jrf = Jimi.createRasterImage(image.getSource());
Jimi.putImage("image/jpeg",jrf,new FileOutputStream(f));
}
catch (JimiException je) {je.printStackTrace();}
|
1.JPEGImageEncoder
in com.sun.image.codec.jpeg.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
public class ImageUtil {
/** saves an image as jpeg file
* @param image the image to be saved
* @param file the jpeg file
*/
public static void saveImageAsJPEG(BufferedImage image, File file) {
try {
OutputStream out = new FileOutputStream(file);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
param.setQuality(1.0f, false);
encoder.encode(image, param);
out.flush();
out.close();
}
catch(IOException e) {
System.out.println(e);
}
} // saveImageAsJPEG
} // ImageUtil
2.GIFEnocder:
http://groups.google.com/groups?q=jdk+image+save+file+gif&hl=en&rnum=6&selm=38D62119.A70AB7F%40dl.ac.uk
in com.sun.image.codec.jpeg.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
public class ImageUtil {
/** saves an image as jpeg file
* @param image the image to be saved
* @param file the jpeg file
*/
public static void saveImageAsJPEG(BufferedImage image, File file) {
try {
OutputStream out = new FileOutputStream(file);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
param.setQuality(1.0f, false);
encoder.encode(image, param);
out.flush();
out.close();
}
catch(IOException e) {
System.out.println(e);
}
} // saveImageAsJPEG
} // ImageUtil
2.GIFEnocder:
http://groups.google.com/groups?q=jdk+image+save+file+gif&hl=en&rnum=6&selm=38D62119.A70AB7F%40dl.ac.uk