java 如何决定使用哪种 BufferedImage 图像类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32414617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:10:01  来源:igfitidea点击:

How to decide which BufferedImage image type to use?

javabufferedimage

提问by user3437460

Java BufferedImage class has a long list of class variables known as the image type which can be used as an argument for the BufferedImage constructor.

Java BufferedImage 类有一个很长的类变量列表,称为图像类型,可以用作 BufferedImage 构造函数的参数。

However, Java docs did a minimal explanation what these image types are used for and how would it affect the BufferedImage to be created.

然而,Java 文档对这些图像类型的用途以及它如何影响要创建的 BufferedImage 做了一个最小的解释。

My question is:

我的问题是:

  1. How would an image type affect the BufferedImage to be created? Does it control the number of bits used to store various colors (Red,Green,Blue) and its transparency?

  2. Which image type should we use if we just want to create

    • an opaque image
    • a transparent image
    • a translucent image
  1. 图像类型将如何影响要创建的 BufferedImage?它是否控制用于存储各种颜色(红色、绿色、蓝色)及其透明度的位数?

  2. 如果我们只想创建,我们应该使用哪种图像类型

    • 不透明的图像
    • 透明图像
    • 半透明图像


I read the description in the Java Doc many times, but just couldn't figure out how should we use it. For example, this one:

我多次阅读 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.

TYPE_INT_BGR

表示具有 8 位 RGB 颜色分量的图像,对应于 Windows 或 Solaris 样式的 BGR 颜色模型,其中蓝色、绿色和红色颜色打包为整数像素。没有阿尔法。该图像具有 DirectColorModel。当具有非不透明 alpha 的数据存储在这种类型的图像中时,必须将颜色数据调整为非预乘形式并丢弃 alpha,如 AlphaComposite 文档中所述。

回答by Hyman

Unless you have specific requirements (for example saving memory or saving computations or a specific native pixel format) just go with the default TYPE_INT_ARGBwhich has 8 bits per channel, 3 channels + alpha.

除非您有特定要求(例如节省内存或节省计算或特定的原生像素格式),否则请使用TYPE_INT_ARGB每个通道 8 位、3 个通道 + alpha的默认值。

Skipping the alpha channel when working with 8 bits per channel won't affect the total memory occupied by the image since every pixel will be packed in an intin any case so 8 bits will be discarded.

在使用每个通道 8 位时跳过 alpha 通道不会影响图像占用的总内存,因为int在任何情况下每个像素都将被打包,因此 8 位将被丢弃。

Basically you have:

基本上你有:

  • TYPE_INT_ARGB, 4 bytes per pixel with alpha channel
  • TYPE_INT_ARGB_PRE, 4 bytes per pixel, same as before but colors are already multiplied by the alpha of the pixel to save computations
  • TYPE_INT_RGB, 4 bytes per pixel without alpha channel
  • TYPE_USHORT_555_RGBand TYPE_USHORT_565_RGB, 2 bytes per pixel, much less colors, don't need to use it unless you have memory constraints
  • TYPE_INT_ARGB, 每像素 4 字节,带 alpha 通道
  • TYPE_INT_ARGB_PRE, 每像素 4 个字节,与之前相同,但颜色已经乘以像素的 alpha 以节省计算
  • TYPE_INT_RGB, 每像素 4 字节,无 alpha 通道
  • TYPE_USHORT_555_RGBTYPE_USHORT_565_RGB,每像素 2 个字节,更少的颜色,除非您有内存限制,否则不需要使用它

Then there are all the same kind of formats with swapped channels (eg. BGRinstead that RGB). You should choose the one native of your platform so that less conversion should be done.

然后有所有相同类型的格式与交换频道(例如,BGR而不是那个RGB)。您应该选择您平台的原生版本,以便减少转换。