• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to decrease image weight?

 
Adriano Bellavita
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm making a servlet in J2EE, and I need to take an image from WS and return it to a mobile client.

So my problem is the image size, and I need to reduce it.

I use JAI to manipulate the images, so I'm working with RederedOp.

Images type can be:

JPG,JPEG,PNG,PNM,BMP,TIFF.

I obtain a scaled instance of an image in this way:


IPImage is one of my classes, in which there is an instance of RenderedOp.

So, how can I reduce the image weight?
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By "weight", do you actually mean "width"? If so, I'd think that you'd set "expectedW" to something smaller than "image.getWidth()" - maybe "image.getWidth() / 2.0". You'll probably want to shrink the height by the same ratio in order to maintain image proportions.
 
Adriano Bellavita
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My English is terrible and I have posted a wrong example.

I want to reduce the weight, so if I have an image ok 150Kb I want to obtain the same image with a decreased weight (i.e. 80Kb).

The code show the way I obtain my image.

I'm sorry...
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you want to maintain image size, yet decrease its file size? That can work only by decreasing image quality, and not all the file formats you mention support that. In fact, I think JPEG is the only one that allows you to control image quality (generally by specifying a percentage value), and thus indirectly image size.
 
Adriano Bellavita
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the only way to reduce image weight is to reduce its size?

If I have an opportunity to reduce the weight I must try!!!

If JPEG is the only format that allows to control image quality without affect image size, I can convert all images I take to this format.

Which is the way to reduce image quality using JPEG?
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure how to do that using JAI, but it's easy using ImageIO: http://www.universalwebservices.net/web-programming-resources/java/adjust-jpeg-image-compression-quality-when-saving-images-in-java. You'll want to set the compression quality parameter to values smaller than 1.0; 0.75 will probably reduce the file size to half, yet still maintain decent quality.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for "beginning". Moving thread.
 
Adriano Bellavita
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to the mod.

I don't know why, cause I have not wrote all the code, but I must work with RenderedOp class.

When I return the image to my client, I use this code:


So I return a ByteArrayOutputStream, and I can choose a file type (as TIFF or JPEG).

Before this step I must obtain a less-weight image.

The example posted by Lester Burnham (ty in advance) is useful if File class are used.

In my case I must work with rendered images...

Who can help me???
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ImageIO isn't just for files; it can write to any OutputStream - so it can write the RenderedImage to a ByteArrayOutputStream while doing the JPEG conversion and compression, all in one call to ImageIO.write!
 
Adriano Bellavita
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm honest to you...

I don't know how to do it...

I only know I have an instance of RenderedOp...
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a little searching you would have found that Lester's example should be able to help you. All you need to do is replace one line:
changes into After that code example you can get a byte[] from encoderOutputStream.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!