当前位置: 技术问答>java相关
90分!求如何将得到的图象存为文件?
来源: 互联网 发布时间:2015-02-27
本文导语: 现在把图象文件显示出来的例子不少,但反过来做的却很少。 现在在我的程序中已经得到一个image对象,可以将它显示在我的jframe上,现在我想把它存为文件,请问怎么做?? 请高手指点!最后有源代码,先谢了! ...
现在把图象文件显示出来的例子不少,但反过来做的却很少。
现在在我的程序中已经得到一个image对象,可以将它显示在我的jframe上,现在我想把它存为文件,请问怎么做??
请高手指点!最后有源代码,先谢了!
现在在我的程序中已经得到一个image对象,可以将它显示在我的jframe上,现在我想把它存为文件,请问怎么做??
请高手指点!最后有源代码,先谢了!
|
import com.sun.image.codec.jpeg.*;
private ByteArrayOutputStream bout;
private BufferedImage bufferedImage;
private void encode() throws IOException
{
//this.render();
this.bout= new ByteArrayOutputStream();
//---need a BufferedImage for JPEG Codec.
//---BufferedImage is a subclass of Image.
//BufferedImage bufferedImage= ( BufferedImage ) super.getImage();
//---sharpen the image.
float[] sharpKernel = { 0.0f, -1.0f, 0.0f,
-1.0f, 5.0f, -1.0f,
0.0f, -1.0f, 0.0f };
BufferedImageOp sharpen= new ConvolveOp( new Kernel(3, 3, sharpKernel), ConvolveOp.EDGE_NO_OP, null);
BufferedImage sharp= sharpen.filter( bufferedImage, null );
//---create an encoder object for the BufferedImage.
JPEGEncodeParam jpegParam= JPEGCodec.getDefaultJPEGEncodeParam( sharp );
jpegParam.setQuality( 1.0f, false );
JPEGImageEncoder jpeg= JPEGCodec.createJPEGEncoder( bout, jpegParam );
//---encode the BufferedImage.
jpeg.encode( bufferedImage );
}
this.bout.writeTo(new FileOutputStream("xx.jpg"));
private ByteArrayOutputStream bout;
private BufferedImage bufferedImage;
private void encode() throws IOException
{
//this.render();
this.bout= new ByteArrayOutputStream();
//---need a BufferedImage for JPEG Codec.
//---BufferedImage is a subclass of Image.
//BufferedImage bufferedImage= ( BufferedImage ) super.getImage();
//---sharpen the image.
float[] sharpKernel = { 0.0f, -1.0f, 0.0f,
-1.0f, 5.0f, -1.0f,
0.0f, -1.0f, 0.0f };
BufferedImageOp sharpen= new ConvolveOp( new Kernel(3, 3, sharpKernel), ConvolveOp.EDGE_NO_OP, null);
BufferedImage sharp= sharpen.filter( bufferedImage, null );
//---create an encoder object for the BufferedImage.
JPEGEncodeParam jpegParam= JPEGCodec.getDefaultJPEGEncodeParam( sharp );
jpegParam.setQuality( 1.0f, false );
JPEGImageEncoder jpeg= JPEGCodec.createJPEGEncoder( bout, jpegParam );
//---encode the BufferedImage.
jpeg.encode( bufferedImage );
}
this.bout.writeTo(new FileOutputStream("xx.jpg"));
|
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();}
Another way is to use the undocumented com.sun.image.codec.jpeg package. // [JDK1.2]
// img is a Java Image
//
BufferedImage bimg = null;
int w = img.getWidth(null);
int h = img.getHeight(null);
int [] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img,0,0,w,h,pixels,0,w);
try {
pg.grabPixels();
}
catch(InterruptedException ie) {
ie.printStackTrace();
}
bimg = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
bimg.setRGB(0,0,w,h,pixels,0,w);
// Encode as a JPEG
FileOutputStream fos = new FileOutputStream("out.jpg");
JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(fos);
jpeg.encode(bimg);
fos.close();
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();}
Another way is to use the undocumented com.sun.image.codec.jpeg package. // [JDK1.2]
// img is a Java Image
//
BufferedImage bimg = null;
int w = img.getWidth(null);
int h = img.getHeight(null);
int [] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img,0,0,w,h,pixels,0,w);
try {
pg.grabPixels();
}
catch(InterruptedException ie) {
ie.printStackTrace();
}
bimg = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
bimg.setRGB(0,0,w,h,pixels,0,w);
// Encode as a JPEG
FileOutputStream fos = new FileOutputStream("out.jpg");
JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(fos);
jpeg.encode(bimg);
fos.close();
|
skyyoung提供的是sun的图像包,你可以从java.sun.com下载,另外还有一个包也可以实现同样的功能,叫acme,如果要我可以发一个给你,或是你自已找一下。
|
If you really want to be fancy, use JAI, for Java Advanced Image. It is very powerful.