登录社区:用户名: 密码: 忘记密码 网页功能:加入收藏 设为首页 网站搜索  

文档

下载

图书

论坛

安全

源码

硬件

游戏
首页 信息 空间 VB VC Delphi Java Flash 补丁 控件 安全 黑客 电子书 笔记本 手机 MP3 杀毒 QQ群 产品库 分类信息 编程网站
  立华软件园 - 安全技术中心 - 技术文档 - JAVA 技术文章 | 相关下载 | 电子图书 | 攻防录像 | 安全网站 | 在线论坛 | QQ群组 | 搜索   
 安全技术技术文档
  · 安全配制
  · 工具介绍
  · 黑客教学
  · 防火墙
  · 漏洞分析
  · 破解专题
  · 黑客编程
  · 入侵检测
 安全技术工具下载
  · 扫描工具
  · 攻击程序
  · 后门木马
  · 拒绝服务
  · 口令破解
  · 代理程序
  · 防火墙
  · 加密解密
  · 入侵检测
  · 攻防演示
 安全技术论坛
  · 安全配制
  · 工具介绍
  · 防火墙
  · 黑客入侵
  · 漏洞检测
  · 破解方法
 其他安全技术资源
  · 攻防演示动画
  · 电子图书
  · QQ群组讨论区
  · 其他网站资源
最新招聘信息

Java图像处理技巧四则-剪切,缩放,灰度变换,彩色变换
发表日期:2004-07-30作者:[转贴] 出处:  

Java图像处理技巧四则

作者:Cherami

email:cherami@163.net

 

本人的另外一些作品请查看:http://www.smiling.com.cn/group/homepage.ecgi?group_id=23141

 

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

图像剪切

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

//import java.awt.*;

//import java.awt.image.*;

Image croppedImage;

ImageFilter cropFilter;

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

CroppedImage= Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(sourceImage.getSource(),cropFilter));

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

//FilteredImageSource是一个ImageProducer对象。

图像缩放

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

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

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

//其它情况请参考API

 

灰度变换

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

/* GrayFilter.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.image.*;

 

public class GrayFilter extends RGBImageFilter {

       int modelStyle;

       public GrayFilter() {

              modelStyle=GrayModel.CS_MAX;

              canFilterIndexColorModel=true;

       }

       public GrayFilter(int style) {

              modelStyle=style;

              canFilterIndexColorModel=true;

       }

       public void setColorModel(ColorModel cm) {

              if (modelStyle==GrayModel.CS_MAX) {

                     substituteColorModel(cm,new GrayModel(cm));

              }

              else if (modelStyle==GrayModel.CS_FLOAT) {

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

              }

       }

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

              return pixel;

       }

}

 

/* GrayModel.java*/

/*@author:cherami */

/*email:cherami@163.net*/

 

import java.awt.image.*;

 

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(sourceModel.getPixelSize());

              this.sourceModel=sourceModel;

              modelStyle=0;

       }

       public GrayModel(ColorModel sourceModel,int style) {

              super(sourceModel.getPixelSize());

              this.sourceModel=sourceModel;

              modelStyle=style;           

       }

       public void setGrayStyle(int style) {

              modelStyle=style;

       }

       protected int getGrayLevel(int pixel) {

              if (modelStyle==CS_MAX) {

                     return Math.max(sourceModel.getRed(pixel),Math.max(sourceModel.getGreen(pixel),sourceModel.getBlue(pixel)));

              }

              else if (modelStyle==CS_FLOAT){

                     return (int)(sourceModel.getRed(pixel)*0.3+sourceModel.getGreen(pixel)*0.59+sourceModel.getBlue(pixel)*0.11);

              }

              else {

                     return 0;

              }

       }

       public int getAlpha(int pixel) {

              return sourceModel.getAlpha(pixel);

       }

       public int getRed(int pixel) {

              return getGrayLevel(pixel);

       }

       public int getGreen(int pixel) {

              return getGrayLevel(pixel);

       }

       public int getBlue(int pixel) {

              return getGrayLevel(pixel);

       }

       public int getRGB(int pixel) {

              int gray=getGrayLevel(pixel);

              return (getAlpha(pixel)<<24)+(gray<<16)+(gray<<8)+gray;

       }

}

如果你有自己的算法或者想取得特殊的效果,你可以修改类GrayModel的方法getGrayLevel()

 

色彩变换

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

/* ReverseColorModel.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.image.*;

 

public class ReverseColorModel extends ColorModel {

       ColorModel sourceModel;

       public ReverseColorModel(ColorModel sourceModel) {

              super(sourceModel.getPixelSize());

              this.sourceModel=sourceModel;

       }

       public int getAlpha(int pixel) {

              return sourceModel.getAlpha(pixel);

       }

       public int getRed(int pixel) {

              return ~sourceModel.getRed(pixel);

       }

       public int getGreen(int pixel) {

              return ~sourceModel.getGreen(pixel);

       }

       public int getBlue(int pixel) {

              return ~sourceModel.getBlue(pixel);

       }

       public int getRGB(int pixel) {

              return (getAlpha(pixel)<<24)+(getRed(pixel)<<16)+(getGreen(pixel)<<8)+getBlue(pixel);

       }

}

/* ReverseColorModel.java*/

/*@author:cherami */

/*email:cherami@163.net*/

 

import java.awt.image.*;

 

public class ReverseFilter extends RGBImageFilter {

       public ReverseFilter() {

              canFilterIndexColorModel=true;

       }

       public void setColorModel(ColorModel cm) {

                     substituteColorModel(cm,new ReverseColorModel(cm));

       }

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

              return pixel;

       }

}

要想取得自己的效果,需要修改ReverseColorModel.java中的三个方法,getRedgetGreengetBlue

下面是上面的效果的一个总的演示程序。

/*GrayImage.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.*;

import java.awt.image.*;

import javax.swing.*;

import java.awt.color.*;

 

public class GrayImage extends JFrame{

       Image source,gray,gray3,clip,bigimg;

       BufferedImage bimg,gray2;

       GrayFilter filter,filter2;

       ImageIcon ii;

       ImageFilter cropFilter;

       int iw,ih;

       public GrayImage() {

              ii=new ImageIcon("images/11.gif");

              source=ii.getImage();

              iw=source.getWidth(this);

              ih=source.getHeight(this);

              filter=new GrayFilter();

              filter2=new GrayFilter(GrayModel.CS_FLOAT);

              gray=createImage(new FilteredImageSource(source.getSource(),filter));

              gray3=createImage(new FilteredImageSource(source.getSource(),filter2));

              cropFilter=new CropImageFilter(5,5,iw-5,ih-5);

              clip=createImage(new FilteredImageSource(source.getSource(),cropFilter));

              bigimg=source.getScaledInstance(iw*2,ih*2,Image.SCALE_DEFAULT);

              MediaTracker mt=new MediaTracker(this);

              mt.addImage(gray,0);

              try {

                     mt.waitForAll();

              } catch (Exception e) {

              }

       }

       public void paint(Graphics g) {

              Graphics2D g2=(Graphics2D)g;

              bimg=new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);

        Graphics2D srcG = bimg.createGraphics();

        RenderingHints rhs = g2.getRenderingHints();

        srcG.setRenderingHints(rhs);

        srcG.drawImage(source, 0, 0, null);

              ColorSpace graySpace=ColorSpace.getInstance(ColorSpace.CS_GRAY);

              ColorConvertOp op=new ColorConvertOp(graySpace,rhs);

              gray2=new BufferedImage(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);

       }

       public void update(Graphics g) {

              paint(g);

       }

       public static void main(String args[]) {

              GrayImage m=new GrayImage();

              m.setSize(400,400);

              m.setVisible(true);

       }

}

 


 

我来说两句】 【发送给朋友】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 Java图像处理技巧四则-剪切,缩放,灰度变换,彩色变换

 ■ [欢迎对本文发表评论]
用  户:  匿名发出:
您要为您所发的言论的后果负责,故请各位遵纪守法并注意语言文明。

最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放 / 友情链接  
Copyright ©2001-2006 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.00215