Results 1 to 6 of 6

Thread: Scaling a JPEG

  1. #1
    ScottVal is offline Member
    Join Date
    Jan 2009
    Posts
    35
    Rep Power
    0

    Default Scaling a JPEG

    Hello-
    I'm working on a class which will scale-down an image taken from a JPEG file.

    I can read the image data into a BufferedImage just fine.

    I notice there is an Image method called Image.getScaledInstance(), so I thought, cool, I can use that to scale the image.

    However, the method returns an "Image," which is the superclass of BufferedImage, so I can't do anything with the "Image."

    The method I want to use to write the BufferedImage back to disk (ImageWriter.write) takes an IIOImage, which I can construct from a BufferedImage. But the fact that I now have an "Image" means that I can't use the constructor.

    Thoughts?....
    -scott

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    There is code in org.jdesktop.swingx.graphics.GraphicsUtilities to do this. (if you don't want to reinvent the wheel.)

  3. #3
    ScottVal is offline Member
    Join Date
    Jan 2009
    Posts
    35
    Rep Power
    0

    Default

    Copying (or using) some one else's code is fine, but I'm wondering why there is this method, which seemingly can't really be used:

    In abstract class java.awt.Image:
    Image getScaledInstance(int width, int height, int hints)

    BufferedImage extends Image.
    So, you can use method getScaledInstance on a BufferedImage, but you can't do anything with the result. The result is an Image, not a BufferedImage. I used the debugger to determine that the resulting object is actually of class sun.awt.image.ToolkitImage.

    -scott

  4. #4
    ScottVal is offline Member
    Join Date
    Jan 2009
    Posts
    35
    Rep Power
    0

    Default

    OK--I figured it out.
    The trick was to create a BufferedImage of the down-scaled size, then create a graphics context from that BufferedImage, and use drawImage to draw the "Image" object in the graphics context. -scott

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    OK--I figured it out.
    I'm glad you got it sorted.

    The trick was...
    Yes, exactly. In the code I linked to the (1) create scaled down BI (2) get Graphics context (3) draw into the context steps are illustrated. For example in the createThumbnalFast() method:

    Java Code:
    //...
    BufferedImage temp = createCompatibleImage(image, width, height);
    Graphics2D g2 = temp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(image, 0, 0, temp.getWidth(), temp.getHeight(), null);
    g2.dispose();
    //...

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    I would have just used an AffineTransform between BufferedImages.

Similar Threads

  1. Save image as JPEG
    By aartheesrini in forum Java Applets
    Replies: 1
    Last Post: 03-16-2009, 06:56 AM
  2. how to display uploaded jpeg in jsp(its urgent)
    By shaktish in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 02-17-2009, 03:52 PM
  3. extracting text from jpeg
    By Nicholas Jordan in forum Advanced Java
    Replies: 0
    Last Post: 10-06-2008, 12:40 AM
  4. Scaling-ache and mouse dragging
    By willemjav in forum Java Applets
    Replies: 19
    Last Post: 07-19-2008, 01:17 AM
  5. scaling images
    By willemjav in forum Java Applets
    Replies: 7
    Last Post: 06-19-2008, 10:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •