Reduce image resolution in Java

I need to use a java program to reduce the size of the image (not the width and height)

I need to reduce the size from 1MB to about 50kb – 100kb Of course, this resolution will be reduced, but it doesn't matter

Solution

According to this blog post: http://i-proving.com/2006/07/06/java-advanced-imaging/ You can use Java advanced imaging library to do what you want The following sample code should give you a good starting point This will adjust the height and width of the image, as well as the image quality Once your image has the desired file size, you can zoom it to the desired pixel height and width when displaying the image

// read in the original image from an input stream
SeekableStream s = SeekableStream.wrapInputStream(
  inputStream,true);
RenderedOp image = JAI.create("stream",s);
((OpImage)image.getRendering()).setTileCache(null);

// Now resize the image

float scale = newWidth / image.getWidth();

RenderedOp resizedImage = JAI.create("SubsampleAverage",image,scale,qualityHints);


// lastly,write the newly-resized image to an
// output stream,in a specific encoding

JAI.create("encode",resizedImage,outputStream,"PNG",null);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>