这是我以前用过的,实现简单的缩放。
/**
* 图象缩放
* @param image 原始图象
* @param width 缩放后的宽度
* @param height 缩放后的高度
* @return
*/
public static Image createScaleImage(Image image,int width,int height){
ImageFilter replicate = new ReplicateScaleFilter(width, height);
ImageProducer prod = new FilteredImageSource(image.getSource(),replicate);
return Toolkit.getDefaultToolkit().createImage(prod);
} /**
* 缩放图象
* @param image 原始图象
* @param proportion 缩放比例
* @return
*/
public static Image createScaleImage(Image image,float proportion){
int w=(int)(image.getWidth(null)*proportion);
int h=(int)(image.getHeight(null)*proportion);
return createScaleImage(image,w,h);
}如果要考虑效率和功能,可能要用JAI了.....
/**
* 图象缩放
* @param image 原始图象
* @param width 缩放后的宽度
* @param height 缩放后的高度
* @return
*/
public static Image createScaleImage(Image image,int width,int height){
ImageFilter replicate = new ReplicateScaleFilter(width, height);
ImageProducer prod = new FilteredImageSource(image.getSource(),replicate);
return Toolkit.getDefaultToolkit().createImage(prod);
} /**
* 缩放图象
* @param image 原始图象
* @param proportion 缩放比例
* @return
*/
public static Image createScaleImage(Image image,float proportion){
int w=(int)(image.getWidth(null)*proportion);
int h=(int)(image.getHeight(null)*proportion);
return createScaleImage(image,w,h);
}如果要考虑效率和功能,可能要用JAI了.....
解决方案 »
- 如何打乱数组
- 对象名.class什么意思啊?
- 关于BUTTON焦点的一个问题
- 定义一个整型集合类integerSet。这种类型的对象可以存储10个20~80之间的整数,即它的内部有一个整型数组存储数据。编程:(1)判断两个integerSet类对象s1和s2是否相等。(2)输出两个集合对象的交集。(3)输出两个集合对象的并集
- 请教一个servlet的问题
- 如何应用正则表达式分解表达式中的标记
- 关于SUPER的简单问题
- 用Java怎么实现文件搜索,实现核心是什么?
- uses or overrides a deprected api是什么意思呀
- 请问这个方法怎么写啊?
- 关于画矩形橡皮筋框的问题
- 各位,请大家看看下面这段代码,请大家看看错在那里!!!
比如把文件读进内存以jpg的方式,总部会凭空就出来张图片资源
板桥里人 jdon.com图片上传到服务器后,会根据情况将图片缩小成一个图标,我们可以利用java强大的图形处理功能,对上传的图片进行缩放处理.下面的程序使用jdk1.4中最新的ImageIO对图片进行读写.使用AffineTransform对图片进行缩放.import java.io.File;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.image.AffineTransformOp;
import javax.imageio.ImageIO;
import java.awt.geom.AffineTransform;
public class UploadImg{/**
* @param fromdir 图片的原始目录
* @param todir 处理后的图片存放目录
* @param imgfile 原始图片
* @param sysimgfile 处理后的图片文件名前缀
*
*/
public boolean CreateThumbnail() throws Exception
{
//ext是图片的格式 gif JPG 或png
String ext="";
double Ratio=0.0;
File F = new File(fromdir,imgfile);
if (!F.isFile())
{
throw new Exception(F+" is not image file error in CreateThumbnail!");
} //首先判断上传的图片是gif还是JPG ImageIO只能将gif转换为png
if (isJpg(imgfile))
{
ext="jpg";
}else{
ext="png";
} File ThF = new File(todir,sysimgfile+"."+ext); BufferedImage Bi = ImageIO.read(F);
//假设图片宽 高 最大为120 120
Image Itemp = Bi.getScaledInstance (120,120,Bi.SCALE_SMOOTH); if ((Bi.getHeight()>120) || (Bi.getWidth()>120))
{
if (Bi.getHeight()>Bi.getWidth())
{
Ratio = 120.0/Bi.getHeight();
}else{
Ratio = 120.0/Bi.getWidth();
}
} AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null);
Itemp = op.filter(Bi, null); try {
ImageIO.write((BufferedImage)Itemp, ext, ThF);
}catch (Exception ex) {
throw new Exception(" ImageIo.write error in CreatThum.: "+ex.getMessage());
}
return (true);
}
}
该程序由于使用到Java 的AWT,虽然没有显示,但是在linux系统下,程序需要X11 windows的支持.需要安装 XFree86
或XFree86-Xvfb ,加入 export DISPLAY=hostdomain:0.0 为这个问题,我也头疼半天,查询网上类似我这样抱怨很多,既然Java是一次编写,到处运行,好象有点名不副实.当然也可以使用PJA VNC 或ACME Laboratories 但由于该程序使用到jdk1.4最新的类,估计其他替代产品没有出来,只好作罢.
java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable
http://java.sun.com/products/jimi/