<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java圖像處理技巧四則

    下面代碼中用到的sourceImage是一個(gè)已經(jīng)存在的Image對(duì)象
    圖像剪切

      對(duì)于一個(gè)已經(jīng)存在的Image對(duì)象,要得到它的一個(gè)局部圖像,可以使用下面的步驟:

    //import java.awt.*;
    //import java.awt.image.*;

    Image croppedImage;
    ImageFilter cropFilter;
    CropFilter =new CropImageFilter(25,30,75,75); //四個(gè)參數(shù)分別為圖像起點(diǎn)坐標(biāo)和寬高,即CropImageFilter(int x,int y,int width,int height),詳細(xì)情況請(qǐng)參考API
    CroppedImage= Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(sourceImage.getSource(),cropFilter));


      如果是在Component的子類中使用,可以將上面的Toolkit.getDefaultToolkit().去掉。FilteredImageSource是一個(gè)ImageProducer對(duì)象。

    圖像縮放

      對(duì)于一個(gè)已經(jīng)存在的Image對(duì)象,得到它的一個(gè)縮放的Image對(duì)象可以使用Image的getScaledInstance方法:

    Image scaledImage=sourceImage. getScaledInstance(100,100, Image.SCALE_DEFAULT); //得到一個(gè)100X100的圖像
    Image doubledImage=sourceImage. getScaledInstance(sourceImage.getWidth(this)*2,sourceImage.getHeight(this)*2, Image.SCALE_DEFAULT); //得到一個(gè)放大兩倍的圖像,這個(gè)程序一般在一個(gè)swing的組件中使用,而類Jcomponent實(shí)現(xiàn)了圖像觀察者接口ImageObserver,所有可以使用this。
    //其它情況請(qǐng)參考API



    灰度變換

      下面的程序使用三種方法對(duì)一個(gè)彩色圖像進(jìn)行灰度變換,變換的效果都不一樣。一般而言,灰度變換的算法是將象素的三個(gè)顏色分量使用R*0.3+G*0.59+B*0.11得到灰度值,然后將之賦值給紅綠藍(lán),這樣顏色取得的效果就是灰度的。另一種就是取紅綠藍(lán)三色中的最大值作為灰度值。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;
    }

    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()。

    色彩變換

      根據(jù)上面的原理,我們也可以實(shí)現(xiàn)色彩變換,這樣的效果就很多了。下面是一個(gè)反轉(zhuǎn)變換的例子:

    /* 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中的三個(gè)方法,getRed、getGreen、getBlue。
      下面是上面的效果的一個(gè)總的演示程序。

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

    posted on 2008-04-23 13:45 Jarod.cn.LuLuLife 閱讀(131) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導(dǎo)航

    統(tǒng)計(jì)

    公告

    我的知識(shí)Blog!

    常用鏈接

    留言簿(3)

    隨筆檔案

    文章檔案

    Image

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: a级黄色毛片免费播放视频| 日本成年免费网站| 亚洲AV无码久久精品蜜桃| 91老湿机福利免费体验| 亚洲精品第一国产综合亚AV| 亚洲综合另类小说色区色噜噜| 久久99热精品免费观看动漫| 亚洲第一区二区快射影院| 国产亚洲?V无码?V男人的天堂| 69视频在线是免费观看| 丰满亚洲大尺度无码无码专线 | 久草视频在线免费| 亚洲av综合日韩| 亚洲福利在线观看| 日本大片在线看黄a∨免费| 拍拍拍无挡免费视频网站| 亚洲日韩一区二区三区| 亚洲国产精品一区二区成人片国内| 手机在线免费视频| 免费国产午夜高清在线视频 | 国产AV无码专区亚洲AV琪琪| 亚洲AV无码久久精品色欲| 全部免费国产潢色一级| 久久A级毛片免费观看| 全黄A免费一级毛片| 亚洲制服丝袜第一页| 国产AV无码专区亚洲AV毛网站| 免费无码又爽又刺激毛片| 久久久精品免费视频| 免费人成网站永久| 亚洲人成网站18禁止久久影院 | 毛片免费观看的视频在线| 久久九九全国免费| 色屁屁www影院免费观看视频| 色婷五月综激情亚洲综合| 亚洲va在线va天堂va888www| 免费jjzz在线播放国产| 成人免费毛片视频| 4455永久在线观免费看| 久久久久久久99精品免费| 成人网站免费大全日韩国产 |