Results 1 to 6 of 6
Thread: Scaling a JPEG
- 03-20-2009, 11:05 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 35
- Rep Power
- 0
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
- 03-21-2009, 12:02 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
There is code in org.jdesktop.swingx.graphics.GraphicsUtilities to do this. (if you don't want to reinvent the wheel.)
- 03-21-2009, 02:56 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 35
- Rep Power
- 0
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
- 03-21-2009, 03:06 AM #4
Member
- Join Date
- Jan 2009
- Posts
- 35
- Rep Power
- 0
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
- 03-21-2009, 04:55 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
I'm glad you got it sorted.OK--I figured it out.
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:The trick was...
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(); //...
- 03-21-2009, 08:47 PM #6
Similar Threads
-
Save image as JPEG
By aartheesrini in forum Java AppletsReplies: 1Last Post: 03-16-2009, 05:56 AM -
how to display uploaded jpeg in jsp(its urgent)
By shaktish in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 02-17-2009, 02:52 PM -
extracting text from jpeg
By Nicholas Jordan in forum Advanced JavaReplies: 0Last Post: 10-05-2008, 11:40 PM -
Scaling-ache and mouse dragging
By willemjav in forum Java AppletsReplies: 19Last Post: 07-19-2008, 12:17 AM -
scaling images
By willemjav in forum Java AppletsReplies: 7Last Post: 06-19-2008, 09:54 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks