简体   繁体   English

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

[英]Convert Binary Text To Tiff Image/PDF in JAVA using JAI

I've a different requirement.我有不同的要求。 I'm receiving TIFF images as a binary text.我接收 TIFF 图像作为二进制文本。 I don't know if I can call that Binary Text.我不知道我是否可以称之为二进制文本。 The text contains non-ascii characters like shown below文本包含非 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•¼

So, I tired to read this text using imageIO using the below code, But it throws error.因此,我厌倦了使用下面的代码使用 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);

The Error message I get is我得到的错误信息是

Exception in thread "main" java.lang.IllegalArgumentException: image == null!

Now surfing through the posts, I identified that not all TIF can be read using ImageIO.现在浏览帖子,我发现并非所有 TIF 都可以使用 ImageIO 读取。 So I used a code online that basically converts the TIFF into a PDF.所以我使用了一个在线代码,基本上将TIFF转换为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");

}

I get the error as Bad endianness tag (not 0x4949 or 0x4d4d) .我收到错误为Bad endianness tag (not 0x4949 or 0x4d4d) This error comes in this line TiffImage.getNumberOfPages(ra);此错误出现在这一行TiffImage.getNumberOfPages(ra); when I try to read the pages in Tiff.当我尝试阅读 Tiff 中的页面时。 I verified using Mirth tool to create a binary text of a tiff and the tiff is valid.我使用 Mirth 工具验证了创建 tiff 的二进制文本,并且 tiff 是有效的。 I'm running out of options on how to fix this issue.我没有办法解决这个问题了。 ANy help is much appreciated.任何帮助深表感谢。

This issue is resolved.此问题已解决。 The issue happened because the Binary text is not properly generated and it created the issue.问题的发生是因为二进制文本未正确生成并造成了问题。

A request is made to the client to send the data in the Base64 encoded format.向客户端发出请求,以 Base64 编码格式发送数据。 And it works fine now.现在它工作正常。 There is a problem that happens with the binary character set is that all not characters are properly written to the file.二进制字符集有一个问题是所有非字符都正确写入文件。 That's the reason why any of the program languages cannot able to convert it properly.这就是为什么任何程序语言都无法正确转换它的原因。

When we receive the data as base64 message then it is just direct file writer of mirth that will solve the issue.当我们收到作为 base64 消息的数据时,只有直接文件写入器才能解决问题。

Appreciate all your efforts to answer the problem.感谢您为回答问题所做的所有努力。 It is your suggestions that gave an idea.是你的建议给了一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM