0

I have a base64 string like below

String base64string ="Suhnagsksbsgshsbsbshsushvshs.....etc;

From this I need to find a image type because I will get tiff format images also, so I cannot display tif format on chrome browser so I need to find out which format it is and then if it's a tiff I need to convert it to jpg then display at front end.

For this,

  1. I'm storing base64 decoded string as a jpg file in temp directory.

  2. I want to read the file and get original image format from the same

  3. if it's tiff format I need to convert it to jpg.

Currently I'm facing error at second point

Find the below code which I have tried but not able to get the image format.

            byte[] decoded = op.createSignature(encryptionKey,sign[1]); 
            
            
            String tempfolder = System.getProperty("java.io.tmpdir");
            System.out.println("tempfolder :"+tempfolder);
            
            String tmpfilepath = tempfolder+""+new Date().getTime()+".jpeg";
            System.out.println("tmpfilepath :"+tmpfilepath);
            File myfile = new File(tmpfilepath);        
            FileOutputStream stream = new FileOutputStream(myfile);
            stream.write(decoded);
            stream.close();     
            try {
                
                ImageInputStream iis= ImageIO.createImageInputStream(myfile);
                Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
                while(iter.hasNext()){
                    ImageReader reader = iter.next();               //throw new RuntimeException("no readers found");
                    String formatName = reader.getFormatName();
                    System.out.println("image format :"+formatName);
                }           
                iis.close();
                
            }

From this code, iterator doesn't finding any data from image and stored image size is 12kb and not able to open manually aslo as showing cannot read this file error( I think it will not open)

If I throw exception it is showing as nosuchelementexception

I have tried to read sample original image file by passing file path to imageinputstream I have got original file format but from base64 byte array not able to get the same.

Please suggest me to change anything in this code and I don't understand why it's not able to get image format from base64 byte array.

I'm using Java 1.7.

jps
  • 20,041
  • 15
  • 75
  • 79
  • 2
    you can also recognize many image/file formats by looking at the first few characters of the Base64 string. `Su...`seems to be tiff, `/9j..`is typical for jpg, `iVBOR...`for png, see [here](https://stackoverflow.com/questions/62329321/how-can-i-check-a-base64-string-is-a-filewhat-type-or-not/62330081#62330081) – jps Feb 13 '21 at 13:25
  • Thank you, but I don't know exactly how many file formats will come from a webservice which I'm consuming, but Su.. is for tif and /9 is for jpg and iVB if for png is really helpfull, but why I'm not getting original file format from above code.am I missing anything. Thank you for the early comment btw – RajniKanth Rao Feb 13 '21 at 16:29

0 Answers0