aspose file tools*
The moose likes Java in General and the fly likes Image Handling in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Image Handling in Java" Watch "Image Handling in Java" New topic
Author

Image Handling in Java

Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Greetings!!

Can we downscale the image in Java without changing the dimensions of the image?

By using BufferedImage, Graphics2D, RenderingHints I can downsize the image with the dimensions I am giving. But if I keep the dimensions same, whatever image I am getting after calling drawimage() [dimensions are same], Is that image different from the original image?

I hope I have rightly put my question

Regards
Andrey Kozhanov
Ranch Hand

Joined: Mar 12, 2010
Posts: 79
We can. Use Graphics2D.drawRenderedImage function.
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Thanks Andrey :-)

I did not get the chance to look at the method suggested by you. I am very new to Graphics2D. I tried to use drawImage method of Graphics2D.

My requirement is I need to scaledown (or compress/reduce the size(KB) of the image) the image to some size (say 50000 bytes for my application) keeping the dimensions same.

Code I have written till now is


the way I am calling the above method is

(I have hard coded the dimensions currently because my original image is 800*600)

Now I finding on this code is:-
Image is getting downscaled to some size (dimensions same). I tried with few images. Getting different size for different images.
For example 1) My original image was around 38000 bytes and the new image after invoking the above code is around 6000 bytes.
2) My original image was around 64000 bytes and the new image after invoking the above code is around 53000 bytes.

My question is - Can not I control the size of image (keeping the dimesnions same). I want to dowmscale the image (keeping the dimensions same) to 50000 bytes if the size of the original image is more than 50000 bytes.

Please suggest some solution

Andrew flexdeveloper
Greenhorn

Joined: Aug 04, 2011
Posts: 7
See this site java2s.com/Tutorial/Java/0280__SWT/SetControlSize.htm and roseindia.net/java/example/java/awt/ImageSize.shtml

I am not sure it will solve your problem or not , but it will give you some hint for the solving your problem


Flex Development | Flex Developer in India
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 170

a) I don't see where you are printing the AFTER size, and b) if you are keeping the original image dimensions, why would you expect the size to be any different?
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Carey Brown wrote:a) I don't see where you are printing the AFTER size, and b) if you are keeping the original image dimensions, why would you expect the size to be any different?


Hi, This is not a complete code. This is just a part of my complete program.
By size I mean the size of image in bytes. We can compress or downscale the image even by keeping the dimensions same.

Thanks anyways :-)
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Andrew flexdeveloper wrote:See this site java2s.com/Tutorial/Java/0280__SWT/SetControlSize.htm and roseindia.net/java/example/java/awt/ImageSize.shtml

I am not sure it will solve your problem or not , but it will give you some hint for the solving your problem


Thanks. I will go through these links and will let you know if find something usefull.

Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 170

Brij Garg wrote:
Carey Brown wrote:a) I don't see where you are printing the AFTER size, and b) if you are keeping the original image dimensions, why would you expect the size to be any different?


Hi, This is not a complete code. This is just a part of my complete program.
By size I mean the size of image in bytes. We can compress or downscale the image even by keeping the dimensions same.

Thanks anyways :-)


A BufferedImage is not compressed, if it were to be saved as a JPG file the file itself would then be compressed, but that's not what you're measuring.
"Downscaling" usually infers re-sampling to a smaller dimension which would not keep the dimensions the same.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Image Handling in Java
 
Similar Threads
problem in checking the dimensions of an image.
resizing background on JPanel with window
How to use AWT/Swing w/o window?
Resize an image while maintaing its aspect ratio..
JFrame resize