gpt4 book ai didi

java - ImageIO写入特定的tiff

转载 作者:行者123 更新时间:2023-12-02 09:58:26 26 4
gpt4 key购买 nike

我尝试转换(使用 ImageIO -> https://github.com/haraldk/TwelveMonkeys ) 将图像转换为特定的 tiff,就像 imagemagick 那样。我有一个输入图像,想要编写一个特定的 tiff,如下所示:

PLANAR_CONFIGURATION = 1
SAMPLES_PER_PIXEL = 1
BITS_PER_SAMPLE = 1
Y_RESOLUTION = 196
X_RESOLUTION = 204
IMAGE_WIDTH = 1728

知道如何渲染输入流吗?目前图像只是转换为 tiff。

BufferedImage image = ImageIO.read(inputstream)
ImageIO.write( image, "tiff", outputstream );

最佳答案

正如@fmw42所说,让你自己做的图像为1位。 TIFFImageWriter 插件将按原样写入传递的图像。幸运的是,这并不难做到。

这是将图像转换为二进制的简单(但不是很复杂)的方法:

private static BufferedImage toBinary(BufferedImage original) {
if (original.getType() == BufferedImage.TYPE_BYTE_BINARY) {
return original;
}

// Quick and unsophisticated way to convert to B/W binary, using default dither and threshold (fixed, 50% I think)
BufferedImage image = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g = image.createGraphics();
try {
g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g.setComposite(AlphaComposite.Src);
g.drawImage(original, 0, 0, null);
}
finally {
g.dispose();
}

return image;
}

我将把它作为练习,使用自适应阈值、误差扩散抖动等编写更高级的解决方案。

现在您可以使用以下代码,就快完成了:

public static void main(String[] args) throws IOException {
BufferedImage original = ImageIO.read(new File(args[0]));
ImageIO.write(toBinary(original), "TIFF", new File("out.tif"));
}

不幸的是,这不会设置 X 和 Y 分辨率标签。如果您也需要它,则必须更深入地研究 ImageIO API,并弄清楚如何使用元数据来控制输出。请注意,只有元数据中的某些值可以通过这种方式设置。其他值将根据传入的图像数据计算得出,有些值可能由编写者填充为默认值。

您可以使用以下代码(toBinary方法与上面相同):

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

BufferedImage image = toBinary(original);

ImageWriter writer = ImageIO.getImageWritersByFormatName("TIFF").next();

try (ImageOutputStream stream = ImageIO.createImageOutputStream(new File("out.tif"))) {
// You may use the param to control compression
ImageWriteParam param = writer.getDefaultWriteParam();

IIOMetadata metadata = writer.getDefaultImageMetadata(ImageTypeSpecifier.createFromRenderedImage(image), param);
Node root = metadata.getAsTree("com_sun_media_imageio_plugins_tiff_image_1.0"); // "javax_imageio_tiff_image_1.0" will work in later versions
Node ifd = root.getFirstChild();
// Add X and Y resolution tags
ifd.appendChild(createResTag("282", "XResolution", "204/1"));
ifd.appendChild(createResTag("283", "YResolution", "196/1"));

// Merge changes back to metadata
metadata.mergeTree("com_sun_media_imageio_plugins_tiff_image_1.0", root);

// Write full image, with metadata
writer.setOutput(stream);
writer.write(null, new IIOImage(image, null, metadata), param);
}
finally {
writer.dispose();
}
}

private static IIOMetadataNode createResTag(String tagNumber, String tagName, String tagValue) {
IIOMetadataNode res = new IIOMetadataNode("TIFFField");
res.setAttribute("number", tagNumber);
res.setAttribute("name", tagName); // Tag name is optional

IIOMetadataNode value = new IIOMetadataNode("TIFFRational");
value.setAttribute("value", tagValue);

IIOMetadataNode rationals = new IIOMetadataNode("TIFFRationals");
rationals.appendChild(value);
res.appendChild(rationals);

return res;
}

PS:TwelveMonkeys TIFF 插件目前不写入 PlanarConfiguration: 1,因为这是默认值,没有办法强制它。但这并不重要,因为在这种情况下,所有兼容的 TIFF 软件都必须使用默认值。

关于java - ImageIO写入特定的tiff,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55814194/

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