java有关图片缩放的实现

java对图片的处理有自己的API,但功能和效率并不一定很好,以下是一段代码
private boolean CreateThumbnail(String fromdir, String imgfile,
String todir, String sysimgfile,int width,int height) {
                        double rw = 0.0;
double rh = 0.0;
File F = new File(fromdir,imgfile);
if (!F.isFile())
throw new Exception(F
+ " is not image file error in CreateThumbnail!");


File ThF = new File(todir,sysimgfile);

BufferedImage Bi = ImageIO.read(F);
Image Itemp = Bi.getScaledInstance(width, height, Bi.SCALE_SMOOTH);

rh = (double)height /Bi.getHeight();
rw = (double)width / Bi.getWidth();
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(rw, rh), null);
Itemp = op.filter(Bi, null);

try {
ImageIO.write((BufferedImage) Itemp, "png", ThF);
} catch (Exception ex) {
throw new Exception(" ImageIo.write error in CreatThum.: "
+ ex.getMessage());
}
return (true);
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
这段代码可以完成图片缩放的操作。

除java自身的api外,有一些其它的图片缩放处理办法,jmagick我认为是效率最好的,它要和magickimage配合使用,先安装magickimage,再安装jmagick就行了。
对magickimage的安装,我感觉在linux下方便一点,在windows下有点麻烦,在windows下要下载一个windows版本的源码再编译,我开始下载了一个linux的版本,半天没编译过。
jmagick的安装我认为比magickimage更麻烦,特别是在windows下,我开始下载了一些已编译好的dll,但不能正常运行,后来直接下其源码编译才好使,在源码中的win32目录下,有一个Makefile.VC++,是可以命令行编译的,前提是要先安装vc6,在这个文件中要修改以下几个地方:JNIINC,MAGICKINC,MAGICKLIB,我是把要编译的源码直接考到makefile.vc++目录来进行的,否则还有一些设置要改,另外在编译时,8个源码文件中会出现找不到其头文件的错误,我直接把这包含头文件的代码行注释掉就OK了,这个问题在linux上也存在,编译完之后把得到的Dll考到system32下就可以用了。
不过有点很郁闷的是,我在一台服务器上可以正常安装并运行,但到另外一台服务器上确不能正常安装运行,运行时会出现Can't load IA 32-bit .so on a IA 32-bit platform的错误提示,到今没有找到解决办法,不过这二台服务器的配置有点不同,第一台是2.3的内核,这台可以运行,另一台是2.6的内核,且用的是一台普通PC,这台不能运行。
另外还有一个很恐怖的问题,我发现当调用的magickimage出问题了,会引起tomcat也一起死掉,这个问题在windows我测试过必然会出现,在linux下还没测试。比如说以下代码:
BufferedImage image = ImageIO.read(new File(file));
MagickImage image = new MagickImage(info);
return image.getDimension().width;
当要取的图片文件不存在,否不是需要的格式的文件时,就会出现这种情况。

还有一个开源的java图片处理imagej,但我发现它好象不支持png的背景透明,所以没有用它。估计应该是可以的,但我没有仔细地研究过,所以也不好多说。

以上这些图片处理都有一个问题,那就是有支持Gif动画的缩放。不过有一个专门的gif处理的第三方库,叫gif4j,不过它不是开源的,但它的库好象可以使用,至少我目前一直在用,这也是一个java开发的。
 

Installing ImageMagick with JPEG, TIFF and PNG Support

Posted at December 4, 2006 03:58 AM

I really like ImageMagick, as I think I've mentioned before. It produces much better images than GD does and still isn't a resource hog, which puts it on my Must Have list. I use ImageMagick on several of my own sites via PHP so it's on all of my servers. This means people I host get the advantage of having it available too.

The only problem is that the default installation of ImageMagick will usually only support converting .gif images. Many times this is because of what the server itself supports natively. So every time I get a new server I have to remember what to install and how to install ImageMagick so that it'll support more image formats. I just had to do that (again) and suspect that others run into the same issue all too often, so following is a quick and dirty tutorial how to do it.

Note: This is for a *nix system (RedHat Enterprise to be more exact) but should work with almost any installation, excepting a Windows server.

First, if it's a new server that's never had ImageMagick installed before, you're likely going to need some additional files. Otherwise it simply won't work.

Second, I'm assuming you have Root access to the server via SSH.

Third, if you've previously tried to make or install ImageMagick, you're going to want to undo that. If you don't, you'll run into problems. Trust me on this one. So if you've already tried to install ImageMagick before finding these instructions navigate to the Source directory where you downloaded IM and type in: make uninstall

Okay, now we're ready to start.

The additional files you'll need are the libs for jpeg, png, tiff and lzw compression. You can check if they're already installed, or you can just install them. On a *nix system the header files should be stored in /usr/include and/or /usr/local/include. The files you'll be looking for are named:

tiff.h
png.h
jpeglib.h

If you have 'em all already you can skip some of the downloads below, though you may also choose to re-install them to make sure your libs are resonably up to date. The files you'll need are uploaded to my server here. They are:

ImageMagick-6.2.3-0.tar.gz (Though you should also be able to use the most recent version from ImageMagick's site)
jpegsrc.v6b.tar.gz
libpng-1.0.6.tar.gz
libtiff-lzw-compression-kit-1.1.tar.gz
tiff-v3.5.5.tar.gz

If you download these to your computer and then upload them to your server via FTP make sure you do so in Binary mode. ASCII transfers will break the files.

Or, if you would prefer to download them directly to your server, SSH in and go to the directory where you keep stuff you've downloaded (/usr/local/src for me) and run the fullowing:

wget http://www.randycullom.com/ImageMagick/ImageMagick-6.2.3-0.tar.gz
wget http://www.randycullom.com/ImageMagick/jpegsrc.v6b.tar.gz
wget http://www.randycullom.com/ImageMagick/libpng-1.0.6.tar.gz
wget http://www.randycullom.com/ImageMagick/libtiff-lzw-compression-kit-1.1.tar.gz
wget http://www.randycullom.com/ImageMagick/tiff-v3.5.5.tar.gz

Next we need to unzip and uncompress each of the files. So:

tar -zxvf ImageMagick-6.2.3-0.tar.gz
tar -zxvf jpegsrc.v6b.tar.gz
tar -zxvf libpng-1.0.6.tar.gz
tar -zxvf libtiff-lzw-compression-kit-1.1.tar.gz
tar -zxvf tiff-v3.5.5.tar.gz

Now let's start installing stuff.

Type:
cd libpng-1.0.6/scripts

Now do an "ls" to look for the makefile that matches your operating system. On all of my systems this is going to be one called makefile.linux, but choose the one that matches your system. If you don't see a file included for your operating system --and it's not a *nix system-- your best bet is to try to makefile.gcc file.

Let's copy our makefile to a location we can use it. So in my case I would type:
cp makefile.linux ../makefile

And install libpng by typing:
cd ..
make
make install

Now let's move on to jpeg support. Type:
cd ..
cd jpeg-6b
./configure --enable-shared
make
make install

Next up is lzw compression support. Type:
cd ..
cd libtiff-lzw-compression-kit

We need to make a small edit to the Makefile to make our lives easier. Normally you would do this either with nano, pico or vi. If you use nano or pico make sure you always use the -w option. I use nano, like:
nano -w Makefile

Change the line that reads:
TIFF_SRC_DIR = /tmp/libtiff

so that it reads:
TIFF_SRC_DIR = ../tiff-v3.5.5

Exit your editor, saving the changes. Then type:
make install
cd ..
cd tiff-v3.5.5
./configure

Answer yes to the TIFF Configuration Parameters. When it's done type:
make
make install

Finally, let's install ImageMagick. Type:
cd ..
cd ImageMagick-6.2.3
./configure --enable-lzw=yes --enable-shared=yes --disable-static --without-perl
make
make install

That's it! You should now have ImageMagick installed, with support for .gif, .jpg/jpeg, .png and .tiff file types. You can test it right from your SSH command line. ImageMagick comes with a test image that will produce an error if it experiences a problem. To test things out type the following:
/usr/local/bin/convert logo: logo.gif
/usr/local/bin/convert logo: logo.jpg
/usr/local/bin/convert logo: logo.png
/usr/local/bin/convert logo: logo.tiff

You shouldn't see any errors. And when you do an ls you should see your four new files listed.

[Added for Nick's question in the comments below.]
The currently available original source(s) of the image library files can be found at:

The Independent JPEG Group
The LibPNG Reference Library
The LibTIFF/TIFF Library
and of course...
The ImageMagick site for the IM source files

I keep them all here because it's easier than directing people to multiple places so that one ends up with a final application that works. Plus I use 'em myself when I need to install IM on a new server. But by all means feel free to grab the updated versions from the above places, as long as you don't mind the possibly of having to work through a few niggling issues to get it all working properly.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值