使用JAI在JAVA中将二进制文本转换为Tiff图像/PDF

3

我有一个不同的需求。我收到的是作为二进制文本的TIFF图像。我不知道是否可以称之为二进制文本。该文本包含像下面所示的非ASCII字符。

0ÎÀi7°®èý¯Â£ôîÀk1 ü"»£ð‚£Ê£ðü»£ö¿
ŒGÓº?¬hÄr€kðŠîÂ
ŒG*Àkð
¸z «ÿ*ëÿ¢^˾6‚¢êZÒáÿì)eì"‚("¿ÿ€jPšÄ0?<À@Ã\=>P€ª ê¨Eý5?J†¤=oöÃ|(0Ã6ª™P†!*¯Ä0ÿ*¢uÝ¡0Š­jþ &&—ÿ
+§¾È°Ã¡-s§‚2“³˜©Î{é¾pªXp%&ì;PËæ™4ºfŒ˜Îÿ Éû½)¨ŽV“þp¦IÇG˜bþñÿÿi•¼

因此,我试图使用以下代码使用ImageIO读取此文本,但它会抛出错误。

String str = "Binary Mentioned Above";
byte[] b = str.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(b);
BufferedImage bImageFromConvert = ImageIO.read(in);

TIFFEncodeParam params = new TIFFEncodeParam();
File myNewTIFF_File =  new File("C:\\Projects\\test\\combined.tif");  
ImageIO.write(bImageFromConvert, "TIFF", myNewTIFF_File);

我收到的错误消息是:
Exception in thread "main" java.lang.IllegalArgumentException: image == null!

浏览帖子后,我发现并非所有的TIF文件都能用ImageIO读取。因此,我使用了一个在线代码,基本上是将TIF转换为PDF。

public static String ImageToPDF(byte[] bytes, String pathFile) {
            String fileName= pathFile + ".pdf";
            Document document = null;

                document = new Document();

            try {
                FileOutputStream fos = new FileOutputStream(fileName);
                PdfWriter writer = PdfWriter.getInstance(document, fos);

                writer.open();
                document.open();

                // Array of bytes we have read from the Binary file
                RandomAccessFileOrArray ra = new RandomAccessFileOrArray(bytes);
                System.out.println("ra ---- "+ra);

                // Get the number of pages the the binary file have inside
                int numberOfPages = TiffImage.getNumberOfPages(ra);
                System.out.println("numberOfPages ------------ "+numberOfPages);

                // Loop through numberOfPages and add them on the document 
                // one by one
                for(int page = 1; page <= numberOfPages; page ++){
                    Image image = TiffImage.getTiffImage(new RandomAccessFileOrArray(bytes),page);
                    image.scaleAbsolute(500, 500);
                    document.add(image);
                }                   

                document.close();
                writer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return fileName;
             }

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


        File imgFront = new File("C:\\Projects\\newtest.txt");
        byte[] fileContent =  Files.readAllBytes(imgFront.toPath());
        //fileContent = File

        ImageToPDF(fileContent,"C:\\Projects\\pdfWithImage");

}

我得到了一个错误,错误为“Bad endianness tag (not 0x4949 or 0x4d4d)”。这个错误出现在这一行代码中:“TiffImage.getNumberOfPages(ra);”当我尝试读取Tiff中的页面时。我使用Mirth工具验证了一个tiff文件的二进制文本并且确认该tiff文件是有效的。我已经无法解决这个问题了。任何帮助都将不胜感激。

你的代码哪一行会抛出 IllegalArgumentException 异常? - Freiheit
对于第二个代码块,我认为你应该将代码缩小到仅读取/写入TIFF,并避免PDF转换,除非PDF转换对于解决读取TIFF的问题至关重要。例如,您能否将“Image”对象单独编写为原始TIFF格式? - Freiheit
@Freiheit 针对第一条评论 --BufferedImage bImageFromConvert = ImageIO.read(in); 这行代码抛出了错误。 - Vibin Guevara
@VibinGuvera 你几乎可以肯定需要指定一个字符集给str.getBytes()。如果它使用UTF-8编码,这种方法通常是在非Windows系统上的默认值,那么它肯定不会给你正确的byte[]。 - agermano
嗨@agermano!很高兴在这里见到你。我不建议一开始就将那些二进制字节放入“String”中。如果您只是将它们保留为字节数组,您就不必担心字符集问题。(实际上,TIFF数据的“字符集”是“C”。) - daveloyall
1个回答

0

这个问题已经解决了。问题是由于二进制文本未正确生成而导致的。

请求客户以Base64编码格式发送数据,现在它可以正常工作了。二进制字符集存在的问题是所有非字符都无法正确写入文件。这就是为什么任何程序语言都不能正确转换它的原因。

当我们将数据作为base64消息接收时,只需使用mirth的直接文件写入器即可解决该问题。

感谢您的努力回答问题。正是您的建议给了我们灵感。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接