Java BufferedImage 类有一长串称为图像类型的类变量,可用作 BufferedImage 构造函数的参数。
但是,Java 文档对这些图像类型的用途以及它如何影响要创建的 BufferedImage 做了最简单的解释。
我的问题是:
图像类型如何影响要创建的 BufferedImage?它是否控制用于存储各种颜色(红色、绿色、蓝色)的位数及其透明度?
如果我们只想创建,我们应该使用哪种图像类型
- 一张不透明的图片
- 一张透明图片
- 半透明图片
我看了很多遍Java Doc中的描述,就是想不通应该怎么用。例如,这个:
TYPE_INT_BGR
Represents an image with 8-bit RGB color components, corresponding to a Windows- or Solaris- style BGR color model, with the colors Blue, Green, and Red packed into integer pixels. There is no alpha. The image has a DirectColorModel. When data with non-opaque alpha is stored in an image of this type, the color data must be adjusted to a non-premultiplied form and the alpha discarded, as described in the AlphaComposite documentation.
最佳答案
除非您有特定要求(例如节省内存或节省计算或特定的 native 像素格式),否则请使用默认的 TYPE_INT_ARGB
,它每个 channel 8 位,3 个 channel + alpha。
在每个 channel 使用 8 位时跳过 alpha channel 不会影响图像占用的总内存,因为在任何情况下每个像素都将打包在 int
中,因此 8 位将被丢弃.
基本上你有:
TYPE_INT_ARGB
,每个像素 4 个字节,带 alpha channelTYPE_INT_ARGB_PRE
,每个像素 4 个字节,与以前相同,但颜色已经乘以像素的 alpha 以节省计算TYPE_INT_RGB
,每像素 4 字节,无 alpha channelTYPE_USHORT_555_RGB
和TYPE_USHORT_565_RGB
,每个像素 2 个字节,颜色少得多,不需要使用它,除非你有内存限制
然后所有相同类型的格式都带有交换 channel (例如 BGR
而不是 RGB
)。您应该选择适合您平台的一种,这样可以减少转换。
关于java - 如何决定使用哪种 BufferedImage 图像类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32414617/