java图像处理的方法

项目需要调整图片服务器的解决方案,这次调整使用fastdfs存储小文件,也就是图片,因为现在还没有fastdfs的版的客服端,所以理所当然把处理图片放到了java这块,使用java来处理图片并且上传到fastdfs上以下是小编为大家搜索整理的java图像处理的方法,欢迎阅读!更多精彩内容请及时关注我们应届毕业生考试网!

java图像处理的方法

  图像剪切

对于一个已经存在的Image对象,要得到它的一个局部图像,可以使用下面的步骤:

//import .*;

//import e.*;

Image croppedImage;

ImageFilter cropFilter;

CropFilter =new CropImageFilter(25,30,75,75);//四个参数分别为图像起点坐标和宽高,即CropImageFilter(int x,int y,int width,int height),详细情况请参考API

CroppedImage= efaultToolkit()teImage(new FilteredImageSource(ource(),cropFilter));

//如果是在Component的子类中使用,可以将上面的efaultToolkit().去掉。

//FilteredImageSource是一个ImageProducer对象。

  图像缩放

对于一个已经存在的Image对象,得到它的一个缩放的Image对象可以使用Image的getScaledInstance方法:

Image scaledImage=sourceImage. getScaledInstance(100,100, E_DEFAULT);//得到一个100X100的'图像

Image doubledImage=sourceImage. getScaledInstance(idth(this)*2,eight(this)*2, E_DEFAULT);//得到一个放大两倍的图像,这个程序一般在一个swing的组件中使用,而类Jcomponent实现了图像观察者接口ImageObserver,所有可以使用this。

//其它情况请参考API。

  灰度变换

下面的程序使用三种方法对一个彩色图像进行灰度变换,变换的效果都不一样。一般而言,灰度变换的算法是将象素的三个颜色分量使用 R*0.3+G*0.59+B*0.11得到灰度值,然后将之赋值给红绿蓝,这样颜色取得的效果就是灰度的。另一种就是取红绿蓝三色中的最大值作为灰度值。java核心包也有一种算法,但是没有看源代码,不知道具体算法是什么样的,效果和上述不同。

/* */

/*@author:cherami */

/*/

import e.*;

public class GrayFilter extends RGBImageFilter {

int modelStyle;

public GrayFilter() {

modelStyle=_MAX;

canFilterIndexColorModel=true;

}

public GrayFilter(int style) {

modelStyle=style;

canFilterIndexColorModel=true;

}

public void setColorModel(ColorModel cm) {

if (modelStyle==_MAX) {

substituteColorModel(cm,new GrayModel(cm));

}

else if (modelStyle==_FLOAT) {

substituteColorModel(cm,new GrayModel(cm,modelStyle));

}

public int filterRGB(int x,int y,int pixel) {

return pixel;

}

}

/* */

/*@author:cherami */

/*/

import e.*;

public class GrayModel extends ColorModel {

public static final int CS_MAX=0;

public static final int CS_FLOAT=1;

ColorModel sourceModel;

int modelStyle;

public GrayModel(ColorModel sourceModel) {

super(ixelSize());

ceModel=sourceModel;

modelStyle=0;

}