Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAI-将TIFF转换为PNG结果,图像右侧有黑线_Java_Png_Tiff_Jai - Fatal编程技术网

Java JAI-将TIFF转换为PNG结果,图像右侧有黑线

Java JAI-将TIFF转换为PNG结果,图像右侧有黑线,java,png,tiff,jai,Java,Png,Tiff,Jai,以下是我在SO online上找到的两个示例,我正在尝试使用以下代码将从REST端点接收到的TIFF图像转换为PNG图像: public void TiffToPng(byte[] tiffBytes, ByteArrayOutputStream output) throws IOException { // ALL GOOD, no black line shows try (OutputStream fos = new FileOutputStream("C:\\m

以下是我在SO online上找到的两个示例,我正在尝试使用以下代码将从REST端点接收到的TIFF图像转换为PNG图像:

public void TiffToPng(byte[] tiffBytes, ByteArrayOutputStream output) throws IOException {
    // ALL GOOD, no black line shows
    try (OutputStream fos = new FileOutputStream("C:\\mytiff.tiff")) {
        fos.write(tiffBytes);
        fos.flush();
    }

    SeekableStream stream = new ByteArraySeekableStream(tiffBytes);
    ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", stream, null);
    
    try {
        RenderedImage renderedImage = decoder.decodeAsRenderedImage(0);
        PNGEncodeParam pngEncodeParam = PNGEncodeParam.getDefaultEncodeParam(renderedImage);
        pngEncodeParam.setBitDepth(1);

        ImageEncoder encoder = ImageCodec.createImageEncoder("png", output, pngEncodeParam);
        encoder.encode(renderedImage);
        
        // PROBLEM! image has black line
        byte[] pngBytes = output.toByteArray(); 
        try (OutputStream fos = new FileOutputStream("C:\\mypng.png")) {
            fos.write(pngBytes);
            fos.flush();
        }
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    } finally {
        stream.close();
    }
}
问题是转换后的图像在右侧有一条黑线

如果我将上面的TIFF字节持久化到TIFF文件中,则没有黑线

如果我将上面的PNG字节保存到PNG文件中,右边会显示一条黑线,它告诉我转换过程是罪魁祸首


您可以尝试使用比
jai
更新的库:
jai-imageio-core
1.4.0

然后,代码使用中间
缓冲区图像

BufferedImage tif = ImageIO.read(new File("C:\\mytiff.tiff"));
ImageIO.write(tif, "png", new File("C:\\mypng.png"));
我认为您使用的是二进制图像(黑色/白色),如果图像的宽度不是8的倍数,则可能会出现此问题