Mik*_*ell 3 java javax.imageio
我正在使用一些第三方软件将图像转换为PDF,我注意到文件大小有些膨胀。经过一番挖掘后,我确认未保留图像的颜色模型。黑白(1位)图像已转换为RGB颜色模型。
遍历库可显示一些颜色模型检测:
switch (awtColorSpace.getType()) {
case ColorSpace.TYPE_RGB:
return PDDeviceRGB.INSTANCE;
case ColorSpace.TYPE_GRAY:
return PDDeviceGray.INSTANCE;
case ColorSpace.TYPE_CMYK:
return PDDeviceCMYK.INSTANCE;
default:
throw new UnsupportedOperationException("color space not implemented: "
+ awtColorSpace.getType());
}
Run Code Online (Sandbox Code Playgroud)
这些图像总是以RGB的形式返回。我决定编写一些测试,它们似乎证实了这一点:
package com.acme;
import org.junit.Test;
import javax.imageio.ImageIO;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import static org.junit.Assert.*;
public class ImageColorDetectionTest {
@Test
public void colorImage() throws Exception {
// Colorspace: sRGB, Depth: 8-bit, Channel depth: Red: 8-bit Green: 8-bit Blue: 8-bit
BufferedImage image = readImage("/color.png");
assertEquals(ColorSpace.TYPE_RGB, image.getColorModel().getColorSpace().getType());
}
@Test
public void greyscaleImage() throws Exception {
// Colorspace: Gray, Depth: 8-bit, Channel depth: Gray: 8-bit
BufferedImage image = readImage("/greyscale.png");
assertEquals(ColorSpace.TYPE_GRAY, image.getColorModel().getColorSpace().getType());
}
@Test
public void blackAndWhiteImage() throws Exception {
// Colorspace: Gray, Depth: 8/1-bit, Channel depth: Gray: 1-bit
BufferedImage image = readImage("/bw.png");
assertEquals(ColorSpace.TYPE_GRAY, image.getColorModel().getColorSpace().getType());
}
protected BufferedImage readImage(String path) throws IOException {
try (InputStream content = this.getClass().getResourceAsStream(path)) {
return ImageIO.read(content);
}
}
}
Run Code Online (Sandbox Code Playgroud)
blackAndWhiteImage测试始终失败。色彩空间类型为5(RGB)。这是JDK中的错误,还是我在这里遗漏了一些基本知识?
测试图像:
色彩空间:sRGB,深度:8位,通道深度:红色:8位绿色:8位蓝色:8位

Imagemagick识别:
magick identify -verbose bw.png
Image: bw.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: PseudoClass
Geometry: 329x247+0+0
Units: Undefined
Type: Bilevel
Base type: Palette
Endianess: Undefined
Colorspace: Gray
Depth: 8/1-bit
Channel depth:
Gray: 1-bit
Channel statistics:
Pixels: 81263
Gray:
min: 0 (0)
max: 255 (1)
mean: 110.66 (0.433961)
standard deviation: 126.384 (0.495623)
kurtosis: -1.92901
skewness: 0.266484
entropy: 0.98738
Colors: 2
Histogram:
45998: ( 0, 0, 0) #000000 gray(0)
35265: (255,255,255) #FFFFFF gray(255)
Colormap entries: 2
Colormap:
0: ( 0, 0, 0,255) #000000FF graya(0,1)
1: (255,255,255,255) #FFFFFFFF graya(255,1)
Rendering intent: Undefined
Gamma: 0.45455
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Matte color: grey74
Background color: white
Border color: srgb(223,223,223)
Transparent color: none
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 329x247+0+0
Dispose: Undefined
Iterations: 0
Compression: Zip
Orientation: Undefined
Properties:
date:create: 2017-06-22T09:33:09-05:00
date:modify: 2017-06-22T09:33:09-05:00
png:bKGD: chunk was found (see Background color, above)
png:cHRM: chunk was found (see Chromaticity, above)
png:gAMA: gamma=0.45455 (See Gamma, above)
png:IHDR.bit-depth-orig: 1
png:IHDR.bit_depth: 1
png:IHDR.color-type-orig: 0
png:IHDR.color_type: 0 (Grayscale)
png:IHDR.interlace_method: 0 (Not interlaced)
png:IHDR.width,height: 329, 247
png:text: 2 tEXt/zTXt/iTXt chunks were found
png:tIME: 2017-06-21T10:12:36Z
signature: 689d59f57ef9b4d58011f92e26f937d9d58cf1ca1ccbcaad6bad7bdd0552fcfa
Artifacts:
verbose: true
Tainted: False
Filesize: 3.69KB
Number pixels: 81.3K
User time: 0.000u
Elapsed time: 0:01.000
Version: ImageMagick 7.0.5-4 Q16 x86_64 2017-03-25 http://www.imagemagick.org
Run Code Online (Sandbox Code Playgroud)
我看到您已经有了一个可以接受的答案,但是无论如何我都会尝试解释一下。.这不是错误。但是我同意,有时候这是违反直觉的。
测试使用图像的已解码内存表示形式的色彩空间,并将其与编码文件中的预期色彩空间进行比较。解码文件时(ImageIO.read在您的示例中使用),该ImageReader插件通常会将图像转换为内存中表示形式,可以快速在屏幕上绘制。这可能与存储在磁盘上时最节省空间的表示形式完全不同。
例如,IndexColorModel即使PNG文件不包含任何PLTE块,每个样本使用少于8位的灰度图像也通常会转换为。而且,IndexColorModel即使它仅包含灰度值,也始终使用sRGB色彩空间(RGB类型)。这对于显示的像素无关紧要,无论如何,像素都是黑白的,但是对于您的测试却很重要。
使用ImageIOAPI 可以获取文件中实际编码的色彩空间:
try (ImageInputStream content = ImageIO.createImageInputStream(this.getClass().getResourceAsStream(path))) {
ImageReader reader = ImageIO.getImageReaders(input).next(); // Assumes PNGImageReader is always there
reader.setInput(input);
IIOMetadata metadata = reader.getImageMetadata(0);
Node nativeTree = metadata.getAsTree(metadata.getNativeMetadataFormatName());
Node standardTree = metadata.getAsTree(IIOMetadataFormatImpl.standardMetadataFormatName);
// ... Get color space information as needed using DOM traversal
}
Run Code Online (Sandbox Code Playgroud)
我跳过读取实际值,因为它变得很冗长,但这很简单。所有值均为Strings。有关IIOMetadata详细信息,请参见类文档。
该bw.png文件的元数据包含以下两种不同的输出表示形式。
本机元数据:
<javax_imageio_png_1.0>
<IHDR width="329" height="247" bitDepth="1" colorType="Grayscale" compressionMethod="deflate" filterMethod="adaptive" interlaceMethod="none"/>
<bKGD>
<bKGD_Grayscale gray="1"/>
</bKGD>
<cHRM whitePointX="31270" whitePointY="32900" redX="64000" redY="33000" greenX="30000" greenY="60000" blueX="15000" blueY="6000"/>
<gAMA value="45455"/>
<tIME year="2017" month="6" day="21" hour="10" minute="12" second="36"/>
</javax_imageio_png_1.0>
Run Code Online (Sandbox Code Playgroud)
标准的“与插件无关的”元数据(跳过无关的值):
<javax_imageio_1.0>
<Chroma>
<ColorSpaceType name="GRAY"/>
<NumChannels value="1"/>
<Gamma value="0.45455"/>
<BlackIsZero value="TRUE"/>
<BackgroundColor red="1" green="1" blue="1"/>
</Chroma>
<Compression ... />
<Data>
<PlanarConfiguration value="PixelInterleaved"/>
<SampleFormat value="UnsignedIntegral"/>
<BitsPerSample value="1"/>
</Data>
<Dimension ... />
<Document ... />
<Transparency>
<Alpha value="none"/>
</Transparency>
</javax_imageio_1.0>
Run Code Online (Sandbox Code Playgroud)
如果您的实际图像是TIFF或其他多种格式,则最好通过ColorSpaceType从Chroma节点获取名称来使用标准元数据格式。
| 归档时间: |
|
| 查看次数: |
788 次 |
| 最近记录: |