使用不同的压缩方式创建多页 TIFF
Mutipage TIFF creation with different compressions
我想将 2 张 jpeg 图像编码为 3 页的 TIFF 文件。规格如下
- 图像 1 的质量为 35 的 JPEG 压缩
- 图像 1 的 CCITT 压缩图像
- 图像 2 的 CCITT 压缩图像
我能够为以上 3 种类型生成单独的 tiff 文件。但是当我尝试将它们组合在一起时,我必须进行一次压缩
param(`params.setCompression(TIFFEncodeParam.COMPRESSION_PACKBITS);`)
扩大尺寸。
如何为不同的页面设置不同的压缩率?
在尝试使用 JAI 之后。我切换到 icafe
BufferedImage[] images = new BufferedImage[3];
images[0]=compressedFront;
images[1]=frontBinaryImage;
images[2]=backBinaryImage;
ImageParam.ImageParamBuilder builder = ImageParam.getBuilder();
TIFFOptions tiffOptions = new TIFFOptions();
tiffOptions.setTiffCompression(Compression.JPG);
tiffOptions.setJPEGQuality(35);
ImageParam[] param = new ImageParam[3];
param[0] = builder.colorType(ImageColorType.GRAY_SCALE).imageOptions(tiffOptions).build();
tiffOptions = new TIFFOptions(tiffOptions); // Copy constructor
tiffOptions.setTiffCompression(Compression.CCITTFAX4);
param[1] = builder.colorType(ImageColorType.BILEVEL).imageOptions(tiffOptions).build();
tiffOptions = new TIFFOptions(tiffOptions);
tiffOptions.setTiffCompression(Compression.CCITTFAX4);
param[2] = builder.colorType(ImageColorType.BILEVEL).imageOptions(tiffOptions).build();
TIFFTweaker.writeMultipageTIFF(rout, param, images);
rout.close();
fout.close();
使用标准 ImageIO API(使用 JAI ImageIO 或其他 TIFF 插件),您应该可以这样做:
public static void main(String[] args) throws IOException {
List<BufferedImage> images = Arrays.asList(
new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB),
new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_BINARY),
new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_BINARY)
);
List<String> compression = Arrays.asList("JPEG", "CCITT T.4", "CCITT T.4");
try (ImageOutputStream outputStream = ImageIO.createImageOutputStream(new File(args[0]))) {
ImageWriter tiffWriter = ImageIO.getImageWritersByFormatName("TIFF").next(); // Assumes TIFF plugin installed
tiffWriter.setOutput(outputStream);
if (!images.isEmpty()) {
tiffWriter.prepareWriteSequence(null); // Use default stream metadata
for (int i = 0; i < images.size(); i++) {
// Set up explicit compression for each image
ImageWriteParam param = tiffWriter.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
String compressionType = compression.get(i);
param.setCompressionType(compressionType);
if ("JPEG".equals(compressionType)) {
param.setCompressionQuality(.35f);
}
tiffWriter.writeToSequence(new IIOImage(images.get(i), null, null), param); // Ignoring thumbnail and metadata for now
}
tiffWriter.endWriteSequence();
}
}
}
以上代码已使用 TwelveMonkeys TIFFImageWriter
进行测试,但它应该同样适用于 JAI ImageIO 插件。
我想将 2 张 jpeg 图像编码为 3 页的 TIFF 文件。规格如下
- 图像 1 的质量为 35 的 JPEG 压缩
- 图像 1 的 CCITT 压缩图像
- 图像 2 的 CCITT 压缩图像
我能够为以上 3 种类型生成单独的 tiff 文件。但是当我尝试将它们组合在一起时,我必须进行一次压缩
param(`params.setCompression(TIFFEncodeParam.COMPRESSION_PACKBITS);`)
扩大尺寸。
如何为不同的页面设置不同的压缩率?
在尝试使用 JAI 之后。我切换到 icafe
BufferedImage[] images = new BufferedImage[3];
images[0]=compressedFront;
images[1]=frontBinaryImage;
images[2]=backBinaryImage;
ImageParam.ImageParamBuilder builder = ImageParam.getBuilder();
TIFFOptions tiffOptions = new TIFFOptions();
tiffOptions.setTiffCompression(Compression.JPG);
tiffOptions.setJPEGQuality(35);
ImageParam[] param = new ImageParam[3];
param[0] = builder.colorType(ImageColorType.GRAY_SCALE).imageOptions(tiffOptions).build();
tiffOptions = new TIFFOptions(tiffOptions); // Copy constructor
tiffOptions.setTiffCompression(Compression.CCITTFAX4);
param[1] = builder.colorType(ImageColorType.BILEVEL).imageOptions(tiffOptions).build();
tiffOptions = new TIFFOptions(tiffOptions);
tiffOptions.setTiffCompression(Compression.CCITTFAX4);
param[2] = builder.colorType(ImageColorType.BILEVEL).imageOptions(tiffOptions).build();
TIFFTweaker.writeMultipageTIFF(rout, param, images);
rout.close();
fout.close();
使用标准 ImageIO API(使用 JAI ImageIO 或其他 TIFF 插件),您应该可以这样做:
public static void main(String[] args) throws IOException {
List<BufferedImage> images = Arrays.asList(
new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB),
new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_BINARY),
new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_BINARY)
);
List<String> compression = Arrays.asList("JPEG", "CCITT T.4", "CCITT T.4");
try (ImageOutputStream outputStream = ImageIO.createImageOutputStream(new File(args[0]))) {
ImageWriter tiffWriter = ImageIO.getImageWritersByFormatName("TIFF").next(); // Assumes TIFF plugin installed
tiffWriter.setOutput(outputStream);
if (!images.isEmpty()) {
tiffWriter.prepareWriteSequence(null); // Use default stream metadata
for (int i = 0; i < images.size(); i++) {
// Set up explicit compression for each image
ImageWriteParam param = tiffWriter.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
String compressionType = compression.get(i);
param.setCompressionType(compressionType);
if ("JPEG".equals(compressionType)) {
param.setCompressionQuality(.35f);
}
tiffWriter.writeToSequence(new IIOImage(images.get(i), null, null), param); // Ignoring thumbnail and metadata for now
}
tiffWriter.endWriteSequence();
}
}
}
以上代码已使用 TwelveMonkeys TIFFImageWriter
进行测试,但它应该同样适用于 JAI ImageIO 插件。