Java图像处理技巧四则

2016-02-19 20:06 29 1 收藏

关注图老师设计创意栏目可以让大家能更好的了解电脑,知道有关于电脑的更多有趣教程,今天给大家分享Java图像处理技巧四则教程,希望对大家能有一点小小的帮助。

【 tulaoshi.com - 编程语言 】

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

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

!-- frame contents --!-- /frame contents --

//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对象可以使用Image的getScaledInstance方法:
  

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.59+B*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
  else if (modelStyle==GrayModel
  }
  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;
  }

!-- frame contents --!-- /frame contents --

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);
  }

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

public int getRGB(int pixel) {
  int gray=getGrayLevel(pixel);
  return (getAlpha(pixel)24)+(gray16)+(gray8)+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);
  }

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

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;
  }
  }

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

/*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);
  }
  }



来源:https://www.tulaoshi.com/n/20160219/1622737.html

延伸阅读
标签: 聊天软件
  1. 传送文件快快快 假如你想给你的朋友发送文件,首先在本地磁盘中找到要发送的文件,将其拖动到QQ中你朋友的昵称上悬停片刻,此时QQ会自动打开与你朋友的聊天窗口,并向他提交发送文件请求。 2. 认清群中成员的真实面目 很多时候我们并没有加群中某些成员为好友,如果这个家伙经常修改自己的昵称,要想辨别他到...
标签: 聊天软件
  一、 QQ中防范木马的新方法 大家知道,目前黑客工具实在是太多了,木马就是其中之一,特别是针对QQ的木马就更是数不胜数了。那么有什么办法避免木马记录我们输入的密码呢?这里推荐一个小方法,可以使你的QQ密码安全许多!具体方法是:假如你的QQ密码是5009,在输入时最后不直接输入该密码,这样直接输入会被直接记录下来。你可...
标签: Delphi
  配置数据引擎(BDE、SQL Link)的简单方法 在数据库程序分发时,需要携带数据引擎(BDE、SQL Link),并且在客户端安装完程序后还需要配置数据引擎,如用户名(username)、密码(Password)等等。如果手工配置的话,工作量比较大,这时,我们可利用InstallShield For Delphi轻松实现配置。在用InstallShield For Delphi制作安装程序时,...
标签: 电脑入门
开总结会的时候了,用PowerPoint编辑一个PPT演示文稿是必不可少的。但如果因PowerPoint不美观在众人面前出现,是不是觉得糗大了.如何才能做出更清晰漂亮的PPT文档,为报告或演示锦上添花,是很多朋友都关心的问题。下面我们就一起来分享一些Powerpoint文档的美化小技巧。 技巧一:为图像添加边框 在PowerPoint中为图像添加边框最简便的方法...
标签: windows 操作系统
Windows XP操作系统中,有一些功能我们可能用不着,但他占用了我们的系统资源,同时有些功能可能因为设置不当,也可能造成系统资源的浪费,本文介绍了四个小技巧,对你提高Windows XP操作系统运行速度有一定的帮助。 一、修改预读设置以提高速度 Win XP使用预读设置这一新技术,以提高系统速度。如果你拥有PIII 800, 512M内...

经验教程

201

收藏

13
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部