gpt4 book ai didi

java - T6 压缩 Tiff 图像

转载 作者:行者123 更新时间:2023-12-02 08:46:38 25 4
gpt4 key购买 nike

我正在尝试读取JPG格式的图像文件,将文本写入图像,并将其保存为单条压缩 TIFF 图像格式的文件。我使用 Apache Commons 库来进行压缩和编写输出图像文件。我不知道为什么输出图像被绘制成分成两部分,然后先绘制第二部分。如果有人可以帮助我解决这个问题或指出我在这里做错了什么。谢谢..

这是下面代码中的示例输入和输出图像...

sample image

FileOutputStream      fos  = null;
BufferedOutputStream os = null;
ByteArrayOutputStream baos = null;

try {

String inputPath = "D:\\Test\\input.jpg";
File inputFile = new File(inputPath);

BufferedImage inputImage = ImageIO.read(inputFile);

int width = inputImage.getWidth();
int height = inputImage.getHeight();

BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);

Font font = new Font("Arial", Font.BOLD, 40 );
java.awt.Graphics2D g2 = outputImage.createGraphics();
g2.setFont( font );
g2.setColor ( Color.BLACK );
g2.drawImage ( inputImage, 0, 0, width, height, Color.WHITE, null);
g2.drawString("TEST TEXT", 20, 40);
g2.dispose();

PixelDensity resolution = PixelDensity.createFromPixelsPerInch ( 200, 200);
double resolutionX = resolution.horizontalDensityInches();
double resolutionY = resolution.verticalDensityInches();
RationalNumber rationalNumX = RationalNumber.valueOf ( resolutionX );
RationalNumber rationalNumY = RationalNumber.valueOf ( resolutionY );

TiffOutputSet outputSet = new TiffOutputSet(ByteOrder.LITTLE_ENDIAN);
TiffOutputDirectory directory = outputSet.addRootDirectory();

baos = new ByteArrayOutputStream();
ImageIO.write(outputImage, "tif", baos);
byte[] bytes = baos.toByteArray();

byte[] compressedBytes = T4AndT6Compression.compressT6(bytes, width, height);

TiffImageData.Data data = new TiffImageData.Data(0, compressedBytes.length, compressedBytes);

TiffElement.DataElement[] dataElements = new TiffElement.DataElement[]{ data };

TiffImageData tiffImageData = new TiffImageData.Strips(dataElements, height);

directory.add ( TiffTagConstants.TIFF_TAG_NEW_SUBFILE_TYPE, 1 );
directory.add ( TiffTagConstants.TIFF_TAG_PHOTOMETRIC_INTERPRETATION, (short) 1 );
directory.add ( TiffTagConstants.TIFF_TAG_COMPRESSION, (short) 4 );
directory.add ( TiffTagConstants.TIFF_TAG_BITS_PER_SAMPLE, (short) 1 );
directory.add ( TiffTagConstants.TIFF_TAG_RESOLUTION_UNIT, (short) 2 );
directory.add ( TiffTagConstants.TIFF_TAG_ROWS_PER_STRIP, height );
directory.add ( TiffTagConstants.TIFF_TAG_IMAGE_WIDTH, width );
directory.add ( TiffTagConstants.TIFF_TAG_IMAGE_LENGTH, height );
directory.add ( TiffTagConstants.TIFF_TAG_XRESOLUTION, rationalNumX );
directory.add ( TiffTagConstants.TIFF_TAG_YRESOLUTION, rationalNumY );

directory.setTiffImageData( tiffImageData );

String outputPath = "D:\\Test\\output.tif";
File outputFile = new File(outputPath);

fos = new FileOutputStream(outputFile);
os = new BufferedOutputStream(fos);

new TiffImageWriterLossy().write( os, outputSet );


} catch (IOException ex) {

Logger.getLogger(JavaApplication7.class.getName()).log(Level.SEVERE, null, ex);

} catch (ImageWriteException ex) {

Logger.getLogger(JavaApplication7.class.getName()).log(Level.SEVERE, null, ex);

} finally {

if ( baos != null ) {
try {
baos.flush();
baos.close();
} catch ( IOException ex ) { }
}
if ( os != null ) {
try {
os.flush();
os.close();
} catch ( IOException ex ) { }
}
if ( fos != null ) {
try {
fos.flush();
fos.close();
} catch ( IOException ex ) { }
}

}

最佳答案

您的代码的问题在于,您首先使用 ImageIO 将 BufferedImage 存储为 TIFF 文件,然后将整个文件与 header 一起压缩为图像的像素数据您传递给 Commons Imaging 的图像:

baos = new ByteArrayOutputStream(); 
ImageIO.write(outputImage, "tif", baos);
byte[] bytes = baos.toByteArray(); // <-- This is not pixel data, but a complete TIFF file

byte[] compressedBytes = T4AndT6Compression.compressT6(bytes, width, height);

这实际上与您期望的图像相似的原因是 ImageIO 写入未压缩的图像数据。图像之前的垃圾线和偏移量是 TIFF 标题和显示为像素的标签...

相反,您可能打算做类似的事情:

// Cast is safe here, as you know outputImage is TYPE_BYTE_BINARY
byte[] bytes = ((DataBufferByte) outputImage.getRaster().getDataBuffer()).getData();
byte[] compressedBytes = T4AndT6Compression.compressT6(bytes, width, height);

这只会压缩像素,您的图像应该没问题。

<小时/>

您还可以仅使用 ImageIO 完成所有操作,并通过执行以下操作来避免对公共(public)成像的额外依赖:

try (ImageOutputStream stream = ImageIO.createImageOutputStream(outputFile)) {
ImageTypeSpecifier imageTypeSpecifier = ImageTypeSpecifier.createFromRenderedImage(outputImage);
ImageWriter writer = ImageIO.getImageWriters(imageTypeSpecifier, "TIFF").next();
writer.setOutput(stream);

ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("CCITT T.6");

IIOMetadata metadata = writer.getDefaultImageMetadata(imageTypeSpecifier, param);
// TODO: Set 200 DPI, default is likely 72, and perhaps subfile type if needed,
// other tags will be set correctly for you

writer.write(null, new IIOImage(outputImage, null, metadata), param);
}

关于java - T6 压缩 Tiff 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61041199/

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