在 Java 中,在创建使用 TIFFFields 配置的关联 TIFFDirectory 后,如何使用 TIFFImageWriter 将相同的字节数组写入 tiff 文件
更多信息
压缩:T4_COMPRESSION_T4_1D
在https://github.com/freeswitch/spandsp/blob/master/src/t4_t6_decode.c
压缩传真图像的每行:
/* 将游程转换为行的位图 */
/* White/black/white... runs, always starting with white.
That means the first run could be zero length. */
for (x = 0, fudge = 0; x < s->a_cursor; x++, fudge ^= 0xFF)
{
i = s->cur_runs[x];
if ((int) i >= s->pixels)
{
s->pixel_stream = (s->pixel_stream << s->pixels) |
(msbmask[s->pixels] & fudge);
for (i += (8 - s->pixels); i >= 8; i -= 8)
{
s->pixels = 8;
s->row_buf[row_pos++] = (uint8_t) s->pixel_stream;
s->pixel_stream = fudge;
}
}
s->pixel_stream = (s->pixel_stream << i) | (msbmask[i] & fudge);
s->pixels -= i;
}
s->image_length++;
每个 row_buf 字节都会累积在一页的 image_buffer 中, 在 t4_rx.c write_tiff_image(t4_rx_state_t *s) 中,条带 通过以下方式写入文件:
/* Let libtiff do the compression */
TIFFWriteEncodedStrip(t->tiff_file, 0, t->image_buffer, t->image_size)
问:要在java中执行此操作,包含页面的字节数组如何 像素/位传递给 TiffImageWriter 对象?
我不完全确定我理解你的问题,但你的字节数组似乎包含打包成字节的 1 位像素信息。
您可以基于这些字节创建 BufferedImage,然后使用 ImageIO 将其写入 TIFF。
首先,根据数据制作 BufferedImage:
DataBuffer buffer = new DataBufferByte(pixels, pixels.length);
WritableRaster raster =
Raster.createPackedRaster(buffer, imageWidth, imageHeight, 1, null);
byte[] colors = { 0x00, (byte) 0xff }; // 0=black, 1=white
ColorModel colorModel =
new IndexColorModel(1, colors.length, colors, colors, colors);
BufferedImage image =
new BufferedImage(colorModel, raster, false, new Properties());
ImageIO TIFF 元数据规范描述了如何指定 TIFF 压缩:
ImageWriter tiffWriter =
ImageIO.getImageWritersByFormatName("TIFF").next();
ImageWriteParam param = tiffWriter.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("CCITT T.4");
IIOImage iioImage = new IIOImage(image, null, null);
try (ImageOutputStream out = ImageIO.createImageOutputStream(
new BufferedOutputStream(
Files.newOutputStream(filename)))) {
tiffWriter.setOutput(out);
tiffWriter.write(null, iioImage, param);
} finally {
tiffWriter.dispose();
}
完整方法如下:
public void writeTIFF(byte[] pixels,
int imageWidth,
int imageHeight,
Path filename)
throws IOException {
DataBuffer buffer = new DataBufferByte(pixels, pixels.length);
WritableRaster raster =
Raster.createPackedRaster(buffer, imageWidth, imageHeight, 1, null);
byte[] colors = { 0x00, (byte) 0xff }; // 0=black, 1=white
ColorModel colorModel =
new IndexColorModel(1, colors.length, colors, colors, colors);
BufferedImage image =
new BufferedImage(colorModel, raster, false, new Properties());
ImageWriter tiffWriter =
ImageIO.getImageWritersByFormatName("TIFF").next();
ImageWriteParam param = tiffWriter.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("CCITT T.4");
IIOImage iioImage = new IIOImage(image, null, null);
try (ImageOutputStream out = ImageIO.createImageOutputStream(
new BufferedOutputStream(
Files.newOutputStream(filename)))) {
tiffWriter.setOutput(out);
tiffWriter.write(null, iioImage, param);
} finally {
tiffWriter.dispose();
}
}