当前位置: 技术问答>java相关
怎样才能把Jpg文件转换为Gif文件啊???
来源: 互联网 发布时间:2015-07-19
本文导语: 我用GifEncoder,出错说:too many color for a gif,该怎么办啊? | there should be such program available, search at www.google.com | //ofileDirAndName = "c:\1.jpg" public void saveImage(Image pImage,...
我用GifEncoder,出错说:too many color for a gif,该怎么办啊?
|
there should be such program available, search at
www.google.com
www.google.com
|
//ofileDirAndName = "c:\1.jpg"
public void saveImage(Image pImage,String ofileDirAndName)
{
BufferedImage bimg = null;
int w = pImage.getWidth(null);
int h = pImage.getHeight(null);
int [] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(pImage,0, 0, w, h,pixels, 0, w);
try {
pg.grabPixels();
}
catch(InterruptedException e) {
e.printStackTrace();
}
this.bout= new ByteArrayOutputStream();System.out.println("3");
//---need a BufferedImage for JPEG Codec.
//---BufferedImage is a subclass of Image.
System.out.println("图像宽高:" +w+":"+h );
BufferedImage bufferedImage= new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
bufferedImage.setRGB(0,0,w,h,pixels,0,w);
//---sharpen the image.
float[] sharpKernel = { 0.0f, -1.0f, 0.0f,
-1.0f, 5.0f, -1.0f,
0.0f, -1.0f, 0.0f };System.out.println("5");
BufferedImageOp sharpen= new ConvolveOp( new Kernel(3, 3, sharpKernel), ConvolveOp.EDGE_NO_OP, null);
BufferedImage sharp= sharpen.filter( bufferedImage, null );
try
{
//---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(ofileDirAndName));
}
catch(Exception ee)
{
System.err.println("出错了"+ee);
}
}
}
public void saveImage(Image pImage,String ofileDirAndName)
{
BufferedImage bimg = null;
int w = pImage.getWidth(null);
int h = pImage.getHeight(null);
int [] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(pImage,0, 0, w, h,pixels, 0, w);
try {
pg.grabPixels();
}
catch(InterruptedException e) {
e.printStackTrace();
}
this.bout= new ByteArrayOutputStream();System.out.println("3");
//---need a BufferedImage for JPEG Codec.
//---BufferedImage is a subclass of Image.
System.out.println("图像宽高:" +w+":"+h );
BufferedImage bufferedImage= new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
bufferedImage.setRGB(0,0,w,h,pixels,0,w);
//---sharpen the image.
float[] sharpKernel = { 0.0f, -1.0f, 0.0f,
-1.0f, 5.0f, -1.0f,
0.0f, -1.0f, 0.0f };System.out.println("5");
BufferedImageOp sharpen= new ConvolveOp( new Kernel(3, 3, sharpKernel), ConvolveOp.EDGE_NO_OP, null);
BufferedImage sharp= sharpen.filter( bufferedImage, null );
try
{
//---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(ofileDirAndName));
}
catch(Exception ee)
{
System.err.println("出错了"+ee);
}
}
}