gpt4 book ai didi

java - 如何在IIOMetadata中嵌入ICC_Profile(Adobe98)并使用ImageIO写入PNG?

转载 作者:行者123 更新时间:2023-12-01 13:40:27 27 4
gpt4 key购买 nike

我尝试使用ImageIOBufferedImage写入png文件,但每次我将ICC_Profile添加到IIOMetadata 生成的 png 图像没有嵌入 ICC_Profile,并且在生成的 png 文件上运行 ImageMagick 识别会产生错误。

我需要的是添加 ICC_Profile 的工作代码。我承认我对 ImageIO 有点新手。我的想法是,我只需将配置文件添加到标题中,无论用于渲染图像的任何内容都应该找到配置文件并在关联的色彩空间中显示图像(如果支持)。我是否错过了应该对这张图片做的其他事情?

另外 - 我想澄清的是,我只想使用 ImageIO 而不是 JAI 的 ColorConvertOp,因为 JAI 已经有 7 年没有开发了,在我看来,最好的方法是使用标准 JDK 类。这是可能的和/或其他人能够实现它吗?

我的代码:

Iterator i = ImageIO.getImageWritersByFormatName("PNG");
if (i.hasNext()) {
ImageWriter imageWriter = (ImageWriter) i.next();
ImageWriteParam param = imageWriter.getDefaultWriteParam();
ImageTypeSpecifier its = new ImageTypeSpecifier(bi.getColorModel(), bi.getSampleModel());
IIOMetadata iomd = imageWriter.getDefaultImageMetadata(its, param);
String formatName = "javax_imageio_png_1.0";
Node node = iomd.getAsTree(formatName);

//add resolution
IIOMetadataNode phys = new IIOMetadataNode("pHYs");
phys.setAttribute("unitSpecifier", "unknown");
phys.setAttribute("pixelsPerUnitXAxis", "300");
phys.setAttribute("pixelsPerUnitYAxis", "300");

//add ICC PROFILE
IIOMetadataNode iccp = new IIOMetadataNode("iCCP");
//ICC_Profile pro = Util.getAdobeRGBICCProfile();
iccp.setUserObject(parameters.getOriginalICC());
iccp.setAttribute("profileName", "AdobeRGB1998");
iccp.setAttribute("compressionMethod", "deflate");

node.appendChild(phys);
node.appendChild(iccp);

try {
iomd.setFromTree(formatName, node);
} catch (IIOInvalidTreeException e) {
System.out.println("IIOInvalidTreeException Occurred setting resolution on PNG EXIF Header.");
e.printStackTrace(System.out);
}

IIOImage iioimage = new IIOImage(bi, null, iomd);
try {
imageWriter.setOutput(new FileImageOutputStream(new File(fileName)));
imageWriter.write(iioimage);
} catch (IOException e) {
System.out.println("Error Occurred setting resolution on PNG.");
e.printStackTrace(System.out);
}
}

Image Magick 识别输出:

Image: /adobe.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 399x508+0+0
Resolution: 300x300
Print size: 1.33x1.69333
Units: Undefined
Type: TrueColorAlpha
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
alpha: 1-bit
Channel statistics:
Red:
min: 0 (0)
max: 255 (1)
mean: 34.4178 (0.134972)
standard deviation: 82.6482 (0.324111)
kurtosis: 2.12664
skewness: 2.01838
Green:
min: 0 (0)
max: 252 (0.988235)
mean: 27.8842 (0.10935)
standard deviation: 71.8833 (0.281895)
kurtosis: 4.11874
skewness: 2.41137
Blue:
min: 0 (0)
max: 254 (0.996078)
mean: 28.1182 (0.110267)
standard deviation: 73.4184 (0.287915)
kurtosis: 4.25472
skewness: 2.44667
Alpha:
min: 0 (0)
max: 255 (1)
mean: 38.5321 (0.151106)
standard deviation: 91.3288 (0.358152)
kurtosis: 1.79587
skewness: -1.9483
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 76.722 (0.300871)
standard deviation: 80.2016 (0.314516)
kurtosis: 4.15993
skewness: 2.43672
Alpha: none #00000000
Rendering intent: Perceptual
Gamma: 0.454545
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)
Background color: white
Border color: srgba(223,223,223,1)
Matte color: grey74
Transparent color: none
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 399x508+0+0
Dispose: Undefined
Iterations: 0
Compression: Zip
Orientation: Undefined
Properties:
date:create: 2013-12-30T10:23:05-06:00
date:modify: 2013-12-30T10:23:05-06:00
png:IHDR.bit-depth-orig: 8
png:IHDR.bit_depth: 8
png:IHDR.color-type-orig: 6
png:IHDR.color_type: 6 (RGBA)
png:IHDR.interlace_method: 0 (Not interlaced)
png:IHDR.width,height: 399, 508
png:pHYs: x_res=300, y_res=300, units=0
png:sRGB: intent=0 (Perceptual Intent)
signature: f03e01954623f646c9b594d0b2234ca444bb5c8282e923c8af520e08753fed0d
Artifacts:
filename: /adobe.png
verbose: true
Tainted: False
Filesize: 68.3KB
Number pixels: 203K
Pixels per second: 20.27MB
User time: 0.000u
Elapsed time: 0:01.009
Version: ImageMagick 6.8.6-6 2013-10-12 Q16 http://www.imagemagick.org
identify: iCCP: Data error in compressed datastream `/adobe.png' @ warning/png.c /MagickPNGWarningHandler/1830.
identify: Profile size field missing from iCCP chunk `/adobe.png' @ warning/png.c/MagickPNGWarningHandler/1830.

任何帮助将不胜感激。谢谢。

最佳答案

我问的原因是我不认为您的 ICC 配置文件是 deflate 压缩的 byte[]。你说“这不是问题”,但我真的认为这是问题。 :-)

如果我不对 ICC 配置文件应用 deflate 压缩,我会从 ExifTool 收到类似的警告,就像从识别中收到的警告一样。

Warning                         : Error inflating iCCP
ICC Profile : (Binary data 526 bytes, use -b option to extract)

如果我对 ICC 配置文件正确应用 deflate 压缩,ICC 配置文件将被正确识别为“Adobe RGB 1998”。

Profile CMM Type                : ADBE
Profile Version : 2.1.0
Profile Class : Display Device Profile
Color Space Data : RGB
[...]
CMM Flags : Not Embedded, Independent
Rendering Intent : Perceptual
Connection Space Illuminant : 0.9642 1 0.82491
Profile Creator : ADBE
Profile ID : 0
Profile Copyright : Copyright 2000 Adobe Systems Incorporated
Profile Description : Adobe RGB (1998)

以下代码对我有用:

public class PNGICCProfileIssue {
public static void main(String[] args) throws IOException {
File input = new File(args[0]);
BufferedImage image = ImageIO.read(input);

Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("PNG");
if (!writers.hasNext()) {
return;
}

ImageWriter writer = writers.next();
writer.setOutput(ImageIO.createImageOutputStream(new File(input.getParent(), input.getName().replace('.', '_') + "_icc.png")));

IIOMetadata imageMetadata = writer.getDefaultImageMetadata(ImageTypeSpecifier.createFromRenderedImage(image), writer.getDefaultWriteParam());

//add ICC PROFILE
IIOMetadataNode iccp = new IIOMetadataNode("iCCP");
ICC_ColorSpace colorSpace = (ICC_ColorSpace) ColorSpaces.getColorSpace(ColorSpaces.CS_ADOBE_RGB_1998);
iccp.setUserObject(getAsDeflatedBytes(colorSpace));
iccp.setAttribute("profileName", "AdobeRGB1998");
iccp.setAttribute("compressionMethod", "deflate");

Node nativeTree = imageMetadata.getAsTree(imageMetadata.getNativeMetadataFormatName());
nativeTree.appendChild(iccp);
imageMetadata.mergeTree(imageMetadata.getNativeMetadataFormatName(), nativeTree);

writer.write(new IIOImage(image, null, imageMetadata));
}

private static byte[] getAsDeflatedBytes(ICC_ColorSpace colorSpace) throws IOException {
byte[] data = colorSpace.getProfile().getData();

ByteArrayOutputStream deflated = new ByteArrayOutputStream();
DeflaterOutputStream deflater = new DeflaterOutputStream(deflated);
deflater.write(data);
deflater.flush();
deflater.close();

return deflated.toByteArray();
}

我认为这个要求应该记录在元数据文档中,因为我必须告诉你,这有点令人困惑。

祝你好运!

关于java - 如何在IIOMetadata中嵌入ICC_Profile(Adobe98)并使用ImageIO写入PNG?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20843873/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com