使用 Java 创建多页 Tiff

Create Multi-Page Tiff with Java

我有兴趣拍摄一张 tif 图像并向其添加一个图层,其中包含带有 Java 的文本,如果可能的话,最好使用十二只猴子图像库。

我可以调整 here 中的代码以将文本添加到 tif 或创建一个只有文本的相同大小的新 tif,但不能将它们另存为多页 tif。例如:

import javax.imageio.*;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

public class ImageUtil {

public static void main(String[] args) throws Exception {

    BufferedImage src = ImageIO.read(new File("/path/to/main.tif"));
    BufferedImage text = createTextLayer(src);
    BufferedImage[] images = new BufferedImage[]{src, text};
    createMultiPage(images);

}

private static BufferedImage createTextLayer(BufferedImage src) {
    int w = src.getWidth();
    int h = src.getHeight();
    BufferedImage img = new BufferedImage(
            w, h, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = img.createGraphics();
    g2d.drawImage(img, 0, 0, null);

    g2d.setPaint(Color.red);
    g2d.setFont(new Font("Serif", Font.BOLD, 200));
    String s = "Hello, world!";
    FontMetrics fm = g2d.getFontMetrics();
    int x = img.getWidth() - fm.stringWidth(s) - 5;
    int y = fm.getHeight() * 5;
    g2d.drawString(s, x, y);
    g2d.dispose();
    return img;
}

private static void createMultiPage(BufferedImage[] images) throws IOException {

    File tempFile = new File("/new/file/path.tif");
    //I also tried passing in stream var below to the try, but also receive java.lang.UnsupportedOperationException: Unsupported write variant!
    //OutputStream stream = new FileOutputStream(tempFile);

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

    try (ImageOutputStream output = ImageIO.createImageOutputStream(tempFile)) {
        writer.setOutput(output);

        ImageWriteParam params = writer.getDefaultWriteParam();
        params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        params.setCompressionType("None");

        //error here: java.lang.UnsupportedOperationException: Unsupported write variant!
        writer.prepareWriteSequence(null);

        for (int i = 0; i < images.length; i++){
            writer.writeToSequence(new IIOImage(images[i], null, null), params);
        }

        // We're done
        writer.endWriteSequence();
    }

}
}

专家:

<dependency>
    <groupId>com.twelvemonkeys.imageio</groupId>
    <artifactId>imageio-tiff</artifactId>
    <version>3.2.1</version>
</dependency>

如何从图像和生成的文本图像创建多页 tif?

我能够为 jpg 获取以下代码 运行,但 jpg 没有图层。

public static void testWriteSequence() throws IOException {
    BufferedImage[] images = new BufferedImage[] {
            new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB),
            new BufferedImage(110, 100, BufferedImage.TYPE_INT_RGB),
            new BufferedImage(120, 100, BufferedImage.TYPE_INT_RGB),
            new BufferedImage(130, 100, BufferedImage.TYPE_INT_RGB)
    };

    Color[] colors = {Color.BLUE, Color.GREEN, Color.RED, Color.ORANGE};

    for (int i = 0; i < images.length; i++) {
        BufferedImage image = images[i];
        Graphics2D g2d = image.createGraphics();
        try {
            g2d.setColor(colors[i]);
            g2d.fillRect(0, 0, 100, 100);
        }
        finally {
            g2d.dispose();
        }
    }

    //ImageWriter writer = createImageWriter();
    ImageWriter writer = ImageIO.getImageWritersByFormatName("JPEG").next();

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    try (ImageOutputStream output = ImageIO.createImageOutputStream(buffer)) {
        writer.setOutput(output);

        ImageWriteParam params = writer.getDefaultWriteParam();
        params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);

        writer.prepareWriteSequence(null);

        params.setCompressionType("JPEG");
        writer.writeToSequence(new IIOImage(images[0], null, null), params);

        params.setCompressionType("JPEG");
        writer.writeToSequence(new IIOImage(images[1], null, null), params);

        params.setCompressionType("JPEG");
        writer.writeToSequence(new IIOImage(images[2], null, null), params);

        params.setCompressionType("JPEG");
        writer.writeToSequence(new IIOImage(images[3], null, null), params);

        writer.endWriteSequence();

        File tempFile = new File("/path/to/new/file.jpg");
        OutputStream out = new FileOutputStream(tempFile);

        buffer.writeTo(out);
    }
}

谢谢。

您可以使用标准 ImageIO API 编写多页图像(以支持它的格式,如 TIFF)。现在 Java ImageIO 附带了一个捆绑的 TIFF 插件,从 Java 9 开始,下面应该可以正常工作,没有额外的依赖。对于 Java 8 及更早版本,您仍然需要一个 TIFF 插件,如前面提到的 JAI 或 TwelveMonkeys。

例如,请参阅 TwelveMonkeys ImageIO 项目的测试用例中的 TIFFImageWriterTest.testWriteSequence 方法,了解如何执行此操作的示例。

重要部分:

BufferedImage[] images = ...
OutputStream stream = ... // May also use File here, as output

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

try (ImageOutputStream output = ImageIO.createImageOutputStream(stream)) {
    writer.setOutput(output);

    ImageWriteParam params = writer.getDefaultWriteParam();
    params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);

    // Compression: None, PackBits, ZLib, Deflate, LZW, JPEG and CCITT variants allowed
    // (different plugins may use a different set of compression type names)
    params.setCompressionType("Deflate");

    writer.prepareWriteSequence(null);

    for (BufferedImage image : images) {
        writer.writeToSequence(new IIOImage(image, null, null), params);
    }

    // We're done
    writer.endWriteSequence();
}

writer.dispose();