Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将BMP转换为Gif_Java_Image_Gif_Javax.imageio_Bmp - Fatal编程技术网

Java 将BMP转换为Gif

Java 将BMP转换为Gif,java,image,gif,javax.imageio,bmp,Java,Image,Gif,Javax.imageio,Bmp,我需要帮助将.BMP图像转换为.gif 我正在努力保持转换后的gif图像的质量。我需要帮助。以下是相关代码。感谢您的帮助 //now Convert the names to lower case and change the extension to the file as a .gif file under the images directory //you can either use URL or File for reading image using ImageIO buffere

我需要帮助将.BMP图像转换为.gif

我正在努力保持转换后的gif图像的质量。我需要帮助。以下是相关代码。感谢您的帮助

//now Convert the names to lower case and change the extension to the file as a .gif file under the images directory
//you can either use URL or File for reading image using ImageIO
bufferedSourceImg = ImageIO.read(fSource);
//getting the RGB mode color value
int color = bufferedSourceImg.getRGB(0,0);
Image image = makeColorTransparent(bufferedSourceImg,new Color(color));
//getting the transparent image
BufferedImage transparent = imageToBufferedImage(image);

//generating the name of the gif image which will be created from the bmp
int idx = destFName.lastIndexOf(".");
destFName = destFName.substring(0, idx);
destFName = destFName + ".gif";
gifFile = new File(getImagesDirectory(), destFName);
//copying the gif copy of the image
Iterator itr = ImageIO.getImageWritersByFormatName("gif");
ImageWriter gifWriter = (ImageWriter)itr.next();
ImageWriteParam iwp = gifWriter.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
String compTypes[] = iwp.getCompressionTypes();
iwp.setCompressionType(compTypes[compTypes.length - 1]);
iwp.setCompressionQuality(0);
//ImageIO.write(transparent, "gif", gifFile);
ImageIO.write(bufferedSourceImg, "gif", gifFile);
FileImageOutputStream output = new FileImageOutputStream(gifFile);
gifWriter.setOutput(output);
IIOImage iioImage = new IIOImage(transparent, null, null);
gifWriter.write(null, iioImage, iwp);
gifWriter.dispose();

查看图像4j。但是我没有使用过它。那么,您所展示的代码有什么问题?您的“挣扎”是以什么形式出现的?这段代码运行时是否没有错误,如果是,是否会生成GIF输出?您遇到的具体问题是什么?质量是什么意思?GIF图像最多只能处理256色。如果没有一些巧妙的抖动和巧妙的调色板生成技术(我不确定Java是否会这样),他们可能最终会使用一些默认的256色调色板,其中只有一小部分颜色与您的图像相关,而且很自然,这看起来会令人反感。你为什么不使用PNG?