Announcement Announcement Module
Collapse
No announcement yet.
How to convert InputStream (image) to Thumbnail image. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to convert InputStream (image) to Thumbnail image.

    Hi, I want to convert an InputStream type of image into a Thumbnail image.
    I tried to use Imgscalr, but I don't how to use it with InputStram.

    Here is my code which is not compiled at the line of Imgscalr.

    Code:
    public ResponseEntity<byte[]> showImage(@PathVariable String  processDefinitionKey, Model model) throws Exception
    {
            String categoryId = processDefinitionKey;
            logger.debug("=======showImage() - categoryId: "+categoryId);
            List<CategoryImages> ciList = (List<CategoryImages>) categoryImageService.findImagesByCategoryId(Integer.parseInt(categoryId), 0, categoryImageService.count()) ;
               if (ciList.isEmpty()) {
                   logger.debug("========cList is Empty");
                   return null;
               }
               CategoryImages ci = ciList.get(0);
               int imageLength = ci.getImageData().length; 
               try {
                    InputStream image = new ByteArrayInputStream(ci.getImageData());
                    // imgscalr  Java Image Scaling Library 
                    BufferedImage thumbnail =
                            Scalr.resize(image, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_WIDTH,
                                        150, 100, Scalr.OP_ANTIALIAS);
                    
                    byte[] imageBytes = IOUtils.toByteArray(image);
                    logger.debug(" =======imageBytes length: "+imageBytes.length);
    
                    HttpHeaders headers = new HttpHeaders();
                    headers.setContentType(MediaType.IMAGE_JPEG);
                    headers.setContentLength(imageBytes.length);
    
                    return new ResponseEntity<byte[]>(imageBytes, headers, HttpStatus.OK);
                } catch (Exception e) { 
                    throw new RuntimeException(e);
                }
    }
    The error is
    no suitable method found for resize(java.io.InputStream,org.imgscalr.Scalr.Meth od,org.imgscalr.Scalr.Mode,int,int,java.awt.image. ConvolveOp)
    method org.imgscalr.Scalr.resize(java.awt.image.BufferedI mage,org.imgscalr.Scalr.Method,org.imgscalr.Scalr. Mode,int,int,java.awt.image.BufferedImageOp...) is not applicable
    (actual argument java.io.InputStream cannot be converted to java.awt.image.BufferedImage by method invocation conversion)
    method org.imgscalr.Scalr.resize(java.awt.image.BufferedI mage,org.imgscalr.Scalr.Mode,int,int,java.awt.imag e.BufferedImageOp...) is not applicable
    (actual argument java.io.InputStream cannot be converted to java.awt.image.BufferedImage by method invocation conversion)
    method org.imgscalr.Scalr.resize(java.awt.image.BufferedI mage,org.imgscalr.Scalr.Method,int,int,java.awt.im age.BufferedImageOp...) is not applicable
    (actual argument java.io.InputStream cannot be converted to java.awt.image.BufferedImage by method invocation conversion)
    method org.imgscalr.Scalr.resize(java.awt.image.BufferedI mage,int,int,java.awt.image.BufferedImageOp...) is not applicable
    (actual argument java.io.InputStream cannot be converted to java.awt.image.BufferedImage by method invocation conversion)
    method org.imgscalr.Scalr.resize(java.awt.image.BufferedI mage,org.imgscalr.Scalr.Method,org.imgscalr.Scalr. Mode,int,java.awt.image.BufferedImageOp...) is not applicable
    (actual argument java.io.InputStream cannot be converted to java.awt.image.BufferedImage by method invocation conversion)
    method org.imgscalr.Scalr.resize(java.awt.image.BufferedI mage,org.imgscalr.Scalr.Mode,int,java.awt.image.Bu fferedImageOp...) is not applicab..
    Any suggestion is very appreciated.
    THANKS
    Sam

  • #2
    Sam,

    imgscalr consumes and produces BufferedImage instances; instances of these type represent an image that has already been decoded from its binary/compressed representation on-disk to its full pixel values in memory (I am ignoring native caching of the image data and what not)

    So the problem is you want to take a raw byte stream (represented by the InputStream) and some how decode that binary information into a BufferedImage first before you pass it to imgscalr to do any work on it.

    Fortunately there are a few convenience methods on the ImageIO class that can do exactly that for you.

    First try the read(InputStream) method and if that doesn't work for you or throws an exception about not being able to find a suitable decoder, you'll need to get the correct ImageReader yourself.

    Basically you have a raw stream of bytes and you need to find the right ImageReader capable of reading that stream of bytes as a particular (known) image-type in order to get it to decode it into a BufferedImage.

    Once you have the BufferedImage you can use imgscalr to your heart's content

    Comment


    • #3
      Thank you very much for the suggestion.
      I found a method from ImageIO, and now fully compiled.
      But when I am running it, it thrown an exception about "src cannot be null", shown below:

      Code:
      InputStream image = new ByteArrayInputStream(ci.getImageData());
                      if (image != null) {
                          // imgscalr – Java Image Scaling Library 
                          BufferedImage thumbnail =
                                  Scalr.resize(ImageIO.read(image), Scalr.Method.SPEED, Scalr.Mode.FIT_TO_WIDTH,
                                              150, 100, Scalr.OP_ANTIALIAS);
      Thanks a lot
      Sam

      Comment


      • #4
        That's because ImageIO cannot understand how to decode your stream into a buffered image and you are passing a null to imgscalr as a result.

        You will need to google something like "ImageIO read from InputStream" to see how to do that, but once you have the BufferedImage, you are good to go.

        Comment


        • #5
          I am still stuck. Google is not my friend at present.
          Sam

          Comment


          • #6
            A lot of google result pointed to the following solution that is what I exactly using:

            InputStream inputStream;
            ........
            BufferedImage bufferedImage=javax.imageio.ImageIO.read(inputStre am);
            But it is failed in my case.

            Sam

            Comment


            • #7
              I don't know why the following code failed to convert BufferedImage to ByteArrayOutputStream:

              public static byte[] toByteArray(BufferedImage bufImage, String fName) throws IOException {
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              ImageIO.write(bufImage, fName, baos);
              byte[] bytesOut = baos.toByteArray();
              logger.debug("===bufImage: "+bufImage.toString());
              logger.debug("===bytesOut: "+bytesOut.length);
              return bytesOut;
              }
              the length of bytesOut is 0 after finished ImageIO.write(..).

              Comment


              • #8
                sorry I am still searching for the answer.

                Comment


                • #9
                  does it work for you?

                  Comment

                  Working...
                  X