8 Replies Latest reply: Jul 1, 2013 9:16 PM by 2fd7fb26-c040-401b-97e1-a09567632110 RSS

    Convert Multi Page Tiff Image to String

    943540
      I have string of Encoded Multi Page (Base64) Tiff Image.
      I want to convert Multi Page Encoded (Base64) Tiff Image to stringImage1 and stringImage2.
      I want to do this convert without I/O operation on disk (on the fly).
      Please suggest using JAI.

      Thanks,
      Sam

      Edited by: 940537 on Jun 13, 2012 1:51 PM
        • 1. Re: Convert Multi Page Tiff Image to String
          EJP
          What's a stringImage?
          • 2. Re: Convert Multi Page Tiff Image to String
            943540
            I want to convert multi page Tiff image having 2 images to encoded base 64 string so that I can pass that string to the calling function.
            stringImage1 = String for Image 1
            stringImage2 = String for Image 2
            • 3. Re: Convert Multi Page Tiff Image to String
              gimbal2
              So basically your question actually consists of two parts.

              - how do you get the individual pages from a multi-page tiff file?
              - how do you convert a pile of bytes (which in this case is an image) into a base64 encoded string?

              The latter has absolutely nothing to do with JAI, so that leaves tackling the first. This is some very old code I pulled from a very old project that was used to extract the individual pages and save each one as a separate PNG file. I didn't write it myself but I have seen it work.
                RenderedImage[] images = null;
                SeekableStream ss = null;
              
                try{
                  ss = new FileSeekableStream(file); // file is a File object pointing to the tiff file to load
                  ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", ss, null);
                  int numPages = decoder.getNumPages();
              
                  images = new RenderedImage[numPages];
              
                  for (int i = 0; i < decoder.getNumPages(); i++) {
                    images[i] = decoder.decodeAsRenderedImage(i);
                  }
              
                  // at this point you will want to do something with the invididual pages.
              
                } catch(Throwable t){
                  // log exception, rethrow it, whatever
                } finally {
              
                  if (ss != null) {
                    try { ss.close(); } catch (Exception e) { }
                  }
                }
              That would give you all the individual pages as RenderedImage objects in the images array which is easily used in combination with ImageIO. Perhaps you will want to use ImageDecoder.decodeAsRaster() in stead to have easier access to the pixel data. Get comfortable with the API documentation and example code you can find using Google is basically the last thing I have to say about it.
              • 4. Re: Convert Multi Page Tiff Image to String
                943540
                Thanks a lot, With the code you posted I am able to get the 2 images as RenderedImage.
                Can you please suggest how to convert the RenderedImage (images) to String/byte[] ) without storing image to disk.
                • 5. Re: Convert Multi Page Tiff Image to String
                  gimbal2
                  940537 wrote:
                  Can you please suggest how to convert the RenderedImage (images) to String/byte[] ) without storing image to disk.
                  Quote:
                  Perhaps you will want to use ImageDecoder.decodeAsRaster() in stead to have easier access to the pixel data.
                  I repeat: time to do some reading.
                  • 6. Re: Convert Multi Page Tiff Image to String
                    sabre150
                    Based on Problem 4 Base64 for decoding on Windows for encoded image of Linux the OP has a basic misunderstanding of what Strings should be used for since he seems to be trying to place binary data (the Tiff image) in a String without Base64 encoding it and then only later to Base64 encoding it. This corrupts the image.
                    • 7. Re: Convert Multi Page Tiff Image to String
                      943540
                      private String getStringForMultiPageTIFF(RenderedImage image)throws IOException {
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                ImageEncoder encoder = null;
                                TIFFEncodeParam tiffEncodeparam = new TIFFEncodeParam();
                                byte[] imageByteArray = new byte[0];
                                
                                encoder = ImageCodec.createImageEncoder(TIFF_IMAGE, baos, tiffEncodeparam);
                                encoder.encode(image);
                                imageByteArray = baos.toByteArray();          
                                return Base64.encodeBase64String(imageByteArray);
                           }
                      • 8. Re: Convert Multi Page Tiff Image to String
                        2fd7fb26-c040-401b-97e1-a09567632110

                        there are two procedures, one is get individual pages from multi-page pages, the other is convert tiff image to string vb.net. the latter would be much easier considering the codes provided up here.