我在看ImageConverter类,试图找出如何将BufferedImage转换为8位颜色,但我不知道如何做到这一点。我还在互联网上搜索,找不到简单的答案,他们都在谈论8位灰度图像。我只是想将图像的颜色转换为8位......没有别的东西,不会调整大小。有没有人介意告诉我如何做到这一点。如何将BufferedImage转换为8位?
8
A
回答
2
从文章"Transparent gifs in Java" at G-Man's Uber Software Engineering Blog这段代码工作得很好:
public static void main(String[] args) throws Exception {
BufferedImage src = convertRGBAToIndexed(ImageIO.read(new File("/src.jpg")));
ImageIO.write(src, "gif", new File("/dest.gif"));
}
public static BufferedImage convertRGBAToIndexed(BufferedImage src) {
BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
Graphics g = dest.getGraphics();
g.setColor(new Color(231, 20, 189));
// fill with a hideous color and make it transparent
g.fillRect(0, 0, dest.getWidth(), dest.getHeight());
dest = makeTransparent(dest, 0, 0);
dest.createGraphics().drawImage(src, 0, 0, null);
return dest;
}
public static BufferedImage makeTransparent(BufferedImage image, int x, int y) {
ColorModel cm = image.getColorModel();
if (!(cm instanceof IndexColorModel))
return image; // sorry...
IndexColorModel icm = (IndexColorModel) cm;
WritableRaster raster = image.getRaster();
int pixel = raster.getSample(x, y, 0); // pixel is offset in ICM's palette
int size = icm.getMapSize();
byte[] reds = new byte[size];
byte[] greens = new byte[size];
byte[] blues = new byte[size];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
IndexColorModel icm2 = new IndexColorModel(8, size, reds, greens, blues, pixel);
return new BufferedImage(icm2, raster, image.isAlphaPremultiplied(), null);
}
+0
工作很好,只需要删除透明部分。 –
1
4
您可以使用JAI(Java高级图像处理)这个官方的Sun(现在的Oracle)图像库来做到这一点。
ColorQuantizerDescriptor显示了您可以应用的量化过程的选择。
+0
你有一些示例代码? http://stackoverflow.com/questions/15312921/how-to-use-colorquantizerdescriptor –
相关问题
- 1. 如何将BufferedImage RGBA转换为BufferedImage RGB?
- 2. 如何将Graphics2D转换为BufferedImage?
- 3. 如何将BufferedImage转换为javaxt.io.Image?
- 4. 如何将BufferedImage转换为AVI?
- 5. 如何将JLabel.getIcon()转换为BufferedImage
- 6. 将UIImage转换为8位
- 7. 将BufferedImage转换为aws ... rekognition.model.Image
- 8. 将ImageIcon转换为BufferedImage
- 9. 将ByteArrayOutputStream转换为BufferedImage
- 10. 将BufferedImage转换为OpenCv Mat
- 11. 将com.lowagie.text.Image转换为BufferedImage
- 12. PHP,将UTF-8转换为ASCII 8位
- 13. 如何将BufferedImage(RGB)转换为BufferedImage(ARGB或RGBA)?
- 14. 将8位int转换为32位
- 15. DICOM将16位转换为8位
- 16. 如何将8位字节转换为6位字符?
- 17. 如何将24位RGB转换为8位RGB
- 18. VHDL如何将32位变量转换为4 x 8位std_logic_vector?
- 19. 如何将8位OpenCV IplImage *转换为32位IplImage *?
- 20. 如何将VHDL中的8位转换为16位?
- 21. 如何将BufferedImage转换成图像?
- 22. 将JPEG转换为8位BMP
- 23. 将RGB565转换为灰度(8位)
- 24. 将8位颜色转换为RGB值
- 25. 如何从BufferedImage创建8位PNG?
- 26. 将8位无符号PCM转换为8位带符号PCM
- 27. 转换为8位灰度
- 28. 将Image对象转换为BufferedImage对象
- 29. 将BufferedImage转换为OpenCV中的Mat
- 30. 将Graphics2D转换为图像或BufferedImage
你可能必须选择一个量化算法,即。将图像的颜色范围转换为256个入口调色板。 – PhiLho
将图像的颜色转换为8位是什么意思?我想你想减少色彩空间?你想要执行彩色量化吗? – Behe
你们能指点我一个图书馆/ API的某种?我不知道你在说什么。这也听起来像我可以创建我自己的调色板,如果可能的话,这将是更好的事件。 –