Java图像处理

本文介绍如何使用Java进行图像处理,包括图像剪切、缩放、灰度变换及色彩变换等操作,并提供了具体代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面代码中用到的sourceImage是一个已经存在的Image对象

  图像剪切

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

//importJava.awt.*;
//importjava.awt.image.*;
ImagecroppedImage;
ImageFiltercropFilter;
CropFilter
=newCropImageFilter(25,30,75,75);//四个参数分别为图像起点坐标和宽高,即CropImageFilter(intx,inty,intwidth,intheight),具体情况请参考API
CroppedImage=Toolkit.getDefaultToolkit().createImage(newFilteredImageSource(sourceImage.getSource(),cropFilter));
//假如是在Component的子类中使用,可以将上面的Toolkit.getDefaultToolkit().去掉。
//FilteredImageSource是一个ImageProdUCer对象。

  图像缩放

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

ImagescaledImage
=sourceImage.getScaledInstance(100,100,Image.SCALE_DEFAULT);//得到一个100X100的图像
ImagedoubledImage=sourceImage.getScaledInstance(sourceImage.getWidth(this)*2,sourceImage.getHeight(this)*2,Image.SCALE_DEFAULT);//得到一个放大两倍的图像,这个程序一般在一个swing的组件中使用,而类Jcomponent实现了图像观察者接口ImageObserver,所有可以使用this。
//其它情况请参考API。

  灰度变换

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

/*GrayFilter.java*/
/*@author:cherami*/
/*email:cherami@163.net*/
importjava.awt.image.*;

publicclassGrayFilterextendsRGBImageFilter{
intmodelStyle;
publicGrayFilter(){
modelStyle
=GrayModel.CS_MAX;
canFilterIndexColorModel
=true;
}
publicGrayFilter(intstyle){
modelStyle
=style;
canFilterIndexColorModel
=true;
}
publicvoidsetColorModel(ColorModelcm){
if(modelStyle==GrayModel.CS_MAX){
substituteColorModel(cm,
newGrayModel(cm));
}
elseif(modelStyle==GrayModel.CS_FLOAT){
substituteColorModel(cm,
newGrayModel(cm,modelStyle));
}
}
publicintfilterRGB(intx,inty,intpixel){
returnpixel;
}
}

/*GrayModel.java*/
/*@author:cherami*/
/*email:cherami@163.net*/

importjava.awt.image.*;

publicclassGrayModelextendsColorModel{
publicstaticfinalintCS_MAX=0;
publicstaticfinalintCS_FLOAT=1;
ColorModelsourceModel;
intmodelStyle;
publicGrayModel(ColorModelsourceModel){
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
modelStyle
=0;
}
publicGrayModel(ColorModelsourceModel,intstyle){
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
modelStyle
=style;
}
publicvoidsetGrayStyle(intstyle){
modelStyle
=style;
}
protectedintgetGrayLevel(intpixel){
if(modelStyle==CS_MAX){
returnMath.max(sourceModel.getRed(pixel),Math.max(sourceModel.getGreen(pixel),sourceModel.getBlue(pixel)));
}
elseif(modelStyle==CS_FLOAT){
return(int)(sourceModel.getRed(pixel)*0.3+sourceModel.getGreen(pixel)*0.59+sourceModel.getBlue(pixel)*0.11);
}
else{
return0;
}
}
publicintgetAlpha(intpixel){
returnsourceModel.getAlpha(pixel);
}
publicintgetRed(intpixel){
returngetGrayLevel(pixel);
}
publicintgetGreen(intpixel){
returngetGrayLevel(pixel);
}
publicintgetBlue(intpixel){
returngetGrayLevel(pixel);
}
publicintgetRGB(intpixel){
intgray=getGrayLevel(pixel);
return(getAlpha(pixel)<<24)+(gray<<16)+(gray<<8)+gray;
}
}

  假如你有自己的算法或者想取得非凡的效果,你可以修改类GrayModel的方法getGrayLevel()。
  色彩变换

  根据上面的原理,我们也可以实现色彩变换,这样的效果就很多了。下面是一个反转变换的例子:

/*ReverseColorModel.java*/
/*@author:cherami*/
/*email:cherami@163.net*/
importjava.awt.image.*;

publicclassReverseColorModelextendsColorModel{
ColorModelsourceModel;
publicReverseColorModel(ColorModelsourceModel){
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
}
publicintgetAlpha(intpixel){
returnsourceModel.getAlpha(pixel);
}
publicintgetRed(intpixel){
return~sourceModel.getRed(pixel);
}
publicintgetGreen(intpixel){
return~sourceModel.getGreen(pixel);
}
publicintgetBlue(intpixel){
return~sourceModel.getBlue(pixel);
}
publicintgetRGB(intpixel){
return(getAlpha(pixel)<<24)+(getRed(pixel)<<16)+(getGreen(pixel)<<8)+getBlue(pixel);
}
}
/*ReverseColorModel.java*/
/*@author:cherami*/
/*email:cherami@163.net*/

importjava.awt.image.*;

publicclassReverseFilterextendsRGBImageFilter{
publicReverseFilter(){
canFilterIndexColorModel
=true;
}
publicvoidsetColorModel(ColorModelcm){
substituteColorModel(cm,
newReverseColorModel(cm));
}
publicintfilterRGB(intx,inty,intpixel){
returnpixel;
}
}

  要想取得自己的效果,需要修改ReverseColorModel.java中的三个方法,getRed、getGreen、getBlue。
下面是上面的效果的一个总的演示程序。

/*GrayImage.java*/
/*@author:cherami*/
/*email:cherami@163.net*/
importjava.awt.*;
importjava.awt.image.*;
importjavax.swing.*;
importjava.awt.color.*;

publicclassGrayImageextendsJFrame{
Imagesource,gray,gray3,clip,bigimg;
BufferedImagebimg,gray2;
GrayFilterfilter,filter2;
ImageIconii;
ImageFiltercropFilter;
intiw,ih;
publicGrayImage(){
ii
=newImageIcon("images/11.gif");
source
=ii.getImage();
iw
=source.getWidth(this);
ih
=source.getHeight(this);
filter
=newGrayFilter();
filter2
=newGrayFilter(GrayModel.CS_FLOAT);
gray
=createImage(newFilteredImageSource(source.getSource(),filter));
gray3
=createImage(newFilteredImageSource(source.getSource(),filter2));
cropFilter
=newCropImageFilter(5,5,iw-5,ih-5);
clip
=createImage(newFilteredImageSource(source.getSource(),cropFilter));
bigimg
=source.getScaledInstance(iw*2,ih*2,Image.SCALE_DEFAULT);
MediaTrackermt
=newMediaTracker(this);
mt.addImage(gray,
0);
try{
mt.waitForAll();
}
catch(Exceptione){
}
}
publicvoidpaint(Graphicsg){
Graphics2Dg2
=(Graphics2D)g;
bimg
=newBufferedImage(iw,ih,BufferedImage.TYPE_INT_RGB);
Graphics2DsrcG
=bimg.createGraphics();
RenderingHintsrhs
=g2.getRenderingHints();
srcG.setRenderingHints(rhs);
srcG.drawImage(source,
0,0,null);
ColorSpacegraySpace
=ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOpop
=newColorConvertOp(graySpace,rhs);
gray2
=newBufferedImage(iw,ih,BufferedImage.TYPE_INT_RGB);
op.filter(bimg,gray2);
g2.drawImage(source,
40,40,this);
g2.drawImage(gray,
80,40,this);
g2.drawImage(gray2,
120,40,this);
g2.drawImage(gray3,
160,40,this);
g2.drawImage(clip,
40,80,this);
g2.drawImage(bigimg,
80,80,this);
}
publicvoidupdate(Graphicsg){
paint(g);
}
publicstaticvoidmain(Stringargs[]){
GrayImagem
=newGrayImage();
m.setSize(
400,400);
m.setVisible(
true);
}
}

转自:http://www.knowsky.com/363901.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值