如何提高g.drawImage()方法的性能来调整图像大小 [英] How to improve the performance of g.drawImage() method for resizing images

查看:942
本文介绍了如何提高g.drawImage()方法的性能来调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,用户可以在相册中上传图片,但自然上传的图片需要调整大小,因此也有拇指可用,所显示的图片也适合页面(例如800x600)。
我调整大小的方式是这样的:

I have an application where users are able to upload pictures in albums but naturally the uploaded images need to be resized so there are also thumbs available and the shown pictures also fit in the page (eg. 800x600). The way I do the resize is like this:

Image scaledImage = img.getScaledInstance((int)width, (int)height, Image.SCALE_SMOOTH);
BufferedImage imageBuff = new BufferedImage((int)width, (int)height, BufferedImage.TYPE_INT_RGB);
Graphics g = imageBuff.createGraphics();
g.drawImage(scaledImage, 0, 0, new Color(0,0,0), null);
g.dispose();

它运作正常。我唯一的问题是 g.drawImage()方法似乎非常缓慢,我无法想象用户要耐心等待上传20张图片20 * 10秒~3分钟。事实上,在我的电脑上,为一张照片拍摄3张不同的照片大约需要40秒。

And it works okayish. My only problem is that the g.drawImage() method seems to be awfully slow, and I just cannot imagine the user to be patient enought to wait for an upload of 20 pictures 20*10 secs ~ 3 minutes. In fact, on my computer it takes almost 40 secs for making the 3 different resizes for a single picture.

这还不够好,我正在寻找更快的速度解。我想知道是否有人可以通过调用shell脚本,命令来告诉我一个更好的Java脚本,命令,无论你知道什么,它必须更快,其他一切都无关紧要。

That's not good enough, and I'm looking for a faster solution. I'm wondering if somebody could tell me about a better one in Java OR by calling a shell script, command, whatever hack you know, it has to be quicker, everything else does not matter this time.

推荐答案

您可以使用 ImageMagick 创建缩略图

convert -define jpeg:size=500x180  hatching_orig.jpg  -auto-orient \
        -thumbnail 250x90   -unsharp 0x.5  thumbnail.gif

要从Java中使用它,你可以尝试 JMagick ,它为ImageMagick提供Java(JNI)接口。或者您可以直接使用 Runtime.exec ProcessBuilder 调用ImageMagick命令。

To use it from Java you can try JMagick which provides a Java (JNI) interface to ImageMagick. Or you can simply invoke the ImageMagick commands directly using Runtime.exec or ProcessBuilder.

这篇关于如何提高g.drawImage()方法的性能来调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆