I'm writing a Web application that let user upload images in several formats (eg png,jpg,bmp). After the image has been uploaded the system should convert it to "png" and scale it to a predefined resolution.
To convert the image I use the handy method:
javax.imageio.ImageIO.write(im, type, baos);
Here's where the problem start. The first argument of Image javax.imageio.ImageIO.write is a RenderedImage . The Java Doc states that the only known implementation is BufferedImage .
I try to find a way to convert a java.awt.Image to a BufferedImage, but it doesn't seems possible. However, it is possible to draw an image on a BufferedImage .
The problem is that creating a new BufferedImage each time is very memory expensive. I can start creating a pool of BufferedImage but I'm looking for clever/news ideas.
Try Thumbnailator library: http://code.google.com/p/thumbnailator/
Example code:
Thumbnails.of("large-picture.jpg")
.scale(1.0)
.outputFormat("png")
.toOutputStream(os);
Library offers also much more like scaling, sizing, cropping, rotating, watermarking etc.
There's a bunch of Java libraries and SO posts about this. Did you check them out?
您可以一遍又一遍地重复使用一个缓冲图像,在使用ImageIO.write方法时使用子图像适当调整大小
You could look at the implementation of BufferedImage and implement your own (A file-based implementation perhaps?). These classes are generally pretty straight-forward to read, often 50-70% comments and clearly written.
They give out the sources, why not make use of 'em?
启动netpbm或imagemagick等本机转换器。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.