3

I've a different requirement. I'm receiving TIFF images as a binary text. I don't know if I can call that Binary Text. The text contains non-ascii characters like shown below

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.

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. So I used a code online that basically converts the TIFF into a 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) . This error comes in this line TiffImage.getNumberOfPages(ra); when I try to read the pages in Tiff. I verified using Mirth tool to create a binary text of a tiff and the tiff is valid. I'm running out of options on how to fix this issue. ANy help is much appreciated.

Vibin Guevara
  • 778
  • 10
  • 27
  • What line of your code throws the `llegalArgumentException`? – Freiheit Nov 06 '19 at 15:16
  • For the second block of code I think you should narrow that code down to just reading/writing the TIFF and avoid the PDF conversion *unless* that PDF conversion is key to solving the problem of reading the TIFF. For example can you write out the `Image` object as a raw TIFF on its own? – Freiheit Nov 06 '19 at 15:18
  • @Freiheit For the first comment -- BufferedImage bImageFromConvert = ImageIO.read(in); This line throws the error – Vibin Guevara Nov 07 '19 at 05:00
  • @VibinGuvera You will almost assuredly need to specify a charset to str.getBytes(). There's no way that's going to give you the correct byte[] if it uses UTF-8 encoding, which is typically the default on non-windows systems. – agermano Nov 07 '19 at 15:22
  • hi @agermano! Fancy seeing you here. I wouldn't recommend putting those binary bytes into a `String` in the first place. If you just leave them as an array of bytes, you won't have to worry about character sets. (And indeed, the "character set" for TIFF data is "C".) – daveloyall Jan 03 '20 at 22:55

1 Answers1

0

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. 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.

Appreciate all your efforts to answer the problem. It is your suggestions that gave an idea.

Vibin Guevara
  • 778
  • 10
  • 27