Convert java.awt.image.BufferedImage to java.awt.Image

参考http://www.java2s.com/Code/Java/2D-Graphics-GUI/ConvertjavaawtimageBufferedImagetojavaawtImage.htm

使用javax.swing.GrayFilter.createDisabledImage(Image i)得到的是java.awt.Image

无法使用ImageIO.write()来输出文件

可以使用下面的方法将

staticpublic BufferedImagecreateBufferedImage ( ImageimageIn,
Componentcomp ){
return createBufferedImage ( imageIn,BufferedImage.TYPE_INT_ARGB,comp ) ;
}

staticpublic BufferedImagecreateBufferedImage ( ImageimageIn,
int imageType,Componentcomp ){
MediaTrackermt= new MediaTracker ( comp ) ;
mt.addImage ( imageIn, 0 ) ;
try {
mt.waitForID ( 0 ) ;
} catch ( InterruptedExceptionie ){
}
BufferedImagebufferedImageOut= new BufferedImage ( imageIn
.getWidth ( null ) ,imageIn.getHeight ( null ) ,imageType ) ;
Graphicsg=bufferedImageOut.getGraphics () ;
g.drawImage ( imageIn, 0 , 0 , null ) ;

return bufferedImageOut;
}


使用下面方法可以将图片灰度化:

public void grayOut (){
ColorConvertOpcolorConvert= new ColorConvertOp ( ColorSpace
.getInstance ( ColorSpace.CS_GRAY ) , null ) ;
colorConvert.filter ( bi,bi ) ;
}

将图片灰度化后保存为jpg和gif都会出错,只能保存成bmp格式

你可能感兴趣的:(java,swing)