Java的圖像處理代碼 java的圖像處理代碼是什么

關(guān)于java圖像處理

Java圖像處理技巧四則

你所需要的網(wǎng)站建設(shè)服務(wù),我們均能行業(yè)靠前的水平為你提供.標(biāo)準(zhǔn)是產(chǎn)品質(zhì)量的保證,主要從事網(wǎng)站制作、成都網(wǎng)站設(shè)計企業(yè)網(wǎng)站建設(shè)、移動網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、品牌網(wǎng)站建設(shè)、網(wǎng)頁制作、做網(wǎng)站、建網(wǎng)站。成都創(chuàng)新互聯(lián)擁有實力堅強的技術(shù)研發(fā)團隊及素養(yǎng)的視覺設(shè)計專才。

下面代碼中用到的sourceImage是一個已經(jīng)存在的Image對象

圖像剪切

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

//import java.awt.*;

//import java.awt.image.*;

Image croppedImage;

ImageFilter cropFilter;

CropFilter =new CropImageFilter(25,30,75,75); //四個參數(shù)分別為圖像起點坐標(biāo)和寬高,即CropImageFilter(int x,int y,int width,int height),詳細情況請參考API

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

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

圖像縮放

對于一個已經(jīng)存在的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實現(xiàn)了圖像觀察者接口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;

}

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)+(gray16)+(gray8)+gray;

}

}

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

色彩變換

根據(jù)上面的原理,我們也可以實現(xiàn)色彩變換,這樣的效果就很多了。下面是一個反轉(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中的三個方法,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) {

}

怎么用java代碼模擬一張圖片

用java代碼模擬一張圖片可以這樣操作:1.創(chuàng)建BufferedImage類

2.根據(jù)BufferedImage類得到一個Graphics2D對象

3.根據(jù)Graphics2D對象進行邏輯操作

4.處理繪圖

5.將繪制好的圖片寫入到圖片

java數(shù)字圖像處理常用算法

前些時候做畢業(yè)設(shè)計 用java做的數(shù)字圖像處理方面的東西 這方面的資料ms比較少 發(fā)點東西上來大家共享一下 主要就是些算法 有自己寫的 有人家的 還有改人家的 有的算法寫的不好 大家不要見笑

一 讀取bmp圖片數(shù)據(jù)

//? 獲取待檢測圖像? 數(shù)據(jù)保存在數(shù)組 nData[] nB[]? nG[]? nR[]中

public? void getBMPImage(String source) throws Exception {?????????? ???????? clearNData();??????????????????????? //清除數(shù)據(jù)保存區(qū)???????? FileInputStream fs = null;?????????????? try {??????????? fs = new FileInputStream(source);??????????? int bfLen = ;??????????? byte bf[] = new byte[bfLen];??????????? fs read(bf bfLen); // 讀取 字節(jié)BMP文件頭??????????? int biLen = ;??????????? byte bi[] = new byte[biLen];??????????? fs read(bi biLen); // 讀取 字節(jié)BMP信息頭

// 源圖寬度??????????? nWidth = (((int) bi[ ] xff) )??????????????????? | (((int) bi[ ] xff) )??????????????????? | (((int) bi[ ] xff) ) | (int) bi[ ] xff;

// 源圖高度??????????? nHeight = (((int) bi[ ] xff) )??????????????????? | (((int) bi[ ] xff) )??????????????????? | (((int) bi[ ] xff) ) | (int) bi[ ] xff;

// 位數(shù)??????????? nBitCount = (((int) bi[ ] xff) ) | (int) bi[ ] xff;

// 源圖大小??????????? int nSizeImage = (((int) bi[ ] xff) )??????????????????? | (((int) bi[ ] xff) )??????????????????? | (((int) bi[ ] xff) ) | (int) bi[ ] xff;

// 對 位BMP進行解析??????????? if (nBitCount == ){??????????????? int nPad = (nSizeImage / nHeight) nWidth * ;??????????????? nData = new int[nHeight * nWidth];??????????????? nB=new int[nHeight * nWidth];??????????????? nR=new int[nHeight * nWidth];??????????????? nG=new int[nHeight * nWidth];??????????????? byte bRGB[] = new byte[(nWidth + nPad) * * nHeight];??????????????? fs read(bRGB (nWidth + nPad) * * nHeight);??????????????? int nIndex = ;??????????????? for (int j = ; j nHeight; j++){??????????????????? for (int i = ; i nWidth; i++) {??????????????????????? nData[nWidth * (nHeight j ) + i] = ( xff) ??????????????????????????????? | (((int) bRGB[nIndex + ] xff) )???????????????????????????????? | (((int) bRGB[nIndex + ] xff) )??????????????????????????????? | (int) bRGB[nIndex] xff;????????????????????? ??????????????????????? nB[nWidth * (nHeight j ) + i]=(int) bRGB[nIndex] xff;??????????????????????? nG[nWidth * (nHeight j ) + i]=(int) bRGB[nIndex+ ] xff;??????????????????????? nR[nWidth * (nHeight j ) + i]=(int) bRGB[nIndex+ ] xff;??????????????????????? nIndex += ;??????????????????? }??????????????????? nIndex += nPad;??????????????? }?//?????????????? Toolkit kit = Toolkit getDefaultToolkit();?//?????????????? image = kit createImage(new MemoryImageSource(nWidth nHeight ?//?????????????????????? nData nWidth));

/*?????????????? //調(diào)試數(shù)據(jù)的讀取

FileWriter fw = new FileWriter( C:\\Documents and Settings\\Administrator\\My Documents\\nDataRaw txt );//創(chuàng)建新文件??????????????? PrintWriter out = new PrintWriter(fw);??????????????? for(int j= ;jnHeight;j++){??????????????? ?for(int i= ;inWidth;i++){??????????????? ??out print(( * +nData[nWidth * (nHeight j ) + i])+ _ ??????????????? ????+nR[nWidth * (nHeight j ) + i]+ _ ??????????????? ????+nG[nWidth * (nHeight j ) + i]+ _ ??????????????? ????+nB[nWidth * (nHeight j ) + i]+ );??????????????? ????????????????? ?}??????????????? ?out println( );??????????????? }??????????????? out close();*/????????? ??????????? }??????? }??????? catch (Exception e) {??????????? e printStackTrace();??????????? throw new Exception(e);??????? } ??????? finally {??????????? if (fs != null) {??????????????? fs close();??????????? }??????? }???? //?? return image;??? }

二?由r g b 獲取灰度數(shù)組

public? int[] getBrightnessData(int rData[] int gData[] int bData[]){??? ???? ?int brightnessData[]=new int[rData length];??? ?if(rData length!=gData length || rData length!=bData length??? ???|| bData length!=gData length){??? ??return brightnessData;??? ?}??? ?else {??? ??for(int i= ;ibData length;i++){??? ???double temp= *rData[i]+ *gData[i]+ *bData[i];??? ???brightnessData[i]=(int)(temp)+((temp (int)(temp)) ? : );??? ??}??? ??return brightnessData;??? ?}??? ? ??? }?

三 直方圖均衡化

public int [] equilibrateGray(int[] PixelsGray int width int height) ??? {??????? ????????? int gray;???????? int length=PixelsGray length;???????? int FrequenceGray[]=new int[length]; ???????? int SumGray[]=new int[ ]; ???????? int ImageDestination[]=new int[length];???????? for(int i = ; i length ;i++) ??????? { ??????? ?? gray=PixelsGray[i]; ????????????? FrequenceGray[gray]++; ??????? } ????????? //??? 灰度均衡化 ???????? SumGray[ ]=FrequenceGray[ ]; ???????? for(int i= ;i ;i++){?????????????? SumGray[i]=SumGray[i ]+FrequenceGray[i];? ???????? }???????? for(int i= ;i ;i++) {?????????????? SumGray[i]=(int)(SumGray[i]* /length);? ???????? }???????? for(int i= ;iheight;i++) ???????? { ?????????????? for(int j= ;jwidth;j++) ????????????? { ????????????????? int k=i*width+j; ????????????????? ImageDestination[k]= xFF | ((SumGray[PixelsGray[k]] ??????????????????????????? ) | (SumGray[PixelsGray[k]] ) | SumGray[PixelsGray[k]]); ?????????????? } ????????? }???????? return ImageDestination;????? }?

四 laplace 階濾波 增強邊緣 圖像銳化

public int[] laplace DFileter(int []data int width int height){??? ??? ?int filterData[]=new int[data length];??? ?int min= ;??? ?int max= ;??? ?for(int i= ;iheight;i++){??? ??for(int j= ;jwidth;j++){??? ???if(i== || i==height || j== || j==width )??? ?????????? filterData[i*width+j]=data[i*width+j];??? ???else??? ????filterData[i*width+j]= *data[i*width+j] data[i*width+j ] data[i*width+j+ ]??? ???????????????????????? data[(i )*width+j] data[(i )*width+j ] data[(i )*width+j+ ]??? ???????????????????????? data[(i+ )*width+j] data[(i+ )*width+j ] data[(i+ )*width+j+ ];??? ?????? ???if(filterData[i*width+j]min)??? ????min=filterData[i*width+j];??? ???if(filterData[i*width+j]max)??? ????max=filterData[i*width+j];??? ??}????? ?}//??? ?System out println( max: +max);//??? ?System out println( min: +min);??? ???? ?for(int i= ;iwidth*height;i++){??? ??filterData[i]=(filterData[i] min)* /(max min);??? ?}??? ?return filterData;??? }?

五 laplace 階增強濾波 增強邊緣 增強系數(shù)delt

public int[] laplaceHigh DFileter(int []data int width int height double delt){??? ???? ?int filterData[]=new int[data length];??? ?int min= ;??? ?int max= ;??? ?for(int i= ;iheight;i++){??? ??for(int j= ;jwidth;j++){??? ???if(i== || i==height || j== || j==width )??? ?????????? filterData[i*width+j]=(int)(( +delt)*data[i*width+j]);??? ???else??? ????filterData[i*width+j]=(int)(( +delt)*data[i*width+j] data[i*width+j ]) data[i*width+j+ ]??? ???????????????????????? data[(i )*width+j] data[(i )*width+j ] data[(i )*width+j+ ]??? ???????????????????????? data[(i+ )*width+j] data[(i+ )*width+j ] data[(i+ )*width+j+ ];??? ?????? ???if(filterData[i*width+j]min)??? ????min=filterData[i*width+j];??? ???if(filterData[i*width+j]max)??? ????max=filterData[i*width+j];??? ??}????? ?}??? ?for(int i= ;iwidth*height;i++){??? ??filterData[i]=(filterData[i] min)* /(max min);??? ?}??? ?return filterData;??? }? 六 局部閾值處理 值化

//?? 局部閾值處理 值化 niblack s?? method??? /*原理 ?? ???????? T(x y)=m(x y)?? +?? k*s(x y)?? ???????? 取一個寬度為w的矩形框 (x y)為這個框的中心 ???????? 統(tǒng)計框內(nèi)數(shù)據(jù) T(x y)為閾值 m(x y)為均值 s(x y)為均方差 k為參數(shù)(推薦 )計算出t再對(x y)進行切割 / ?? ???????? 這個算法的優(yōu)點是???? 速度快 效果好 ?? ???????? 缺點是???? niblack s?? method會產(chǎn)生一定的噪聲 ? ???? */??? ??? public int[] localThresholdProcess(int []data int width int height int w int h double coefficients double gate){??? ?int[] processData=new int[data length];??? ?for(int i= ;idata length;i++){??? ??processData[i]= ;??? ?}??? ???? ?if(data length!=width*height)??? ??return processData;??? ???? ?int wNum=width/w;??? ?int hNum=height/h;??? ?int delt[]=new int[w*h];??? ???? ?//System out println( w; +w+ ? h: +h+ ? wNum: +wNum+ hNum: +hNum);??? ???? ?for(int j= ;jhNum;j++){??? ??for(int i= ;iwNum;i++){??? ?//for(int j= ;j ;j++){??? ?//?for(int i= ;i ;i++){???? ??? for(int n= ;nh;n++)??? ?????????? for(int k= ;kw;k++){??? ????? ????? delt[n*w+k]=data[(j*h+n)*width+i*w+k];??? ????? ????? //System out print( delt[ +(n*w+k)+ ]: +delt[n*w+k]+ );??? ?????????? }??? ??? //System out println();??? ??? /*??? ??? for(int n= ;nh;n++)??? ?????????? for(int k= ;kw;k++){??? ????? ????? System out print( data[ +((j*h+n)*width+i*w+k)+ ]: +data[(j*h+n)*width+i*w+k]+ );??? ?????????? }??? ??? System out println();??? ??? */??? ??? delt=thresholdProcess(delt w h coefficients gate);??? ??? for(int n= ;nh;n++)??? ?????????? for(int k= ;kw;k++){??? ????? ????? processData[(j*h+n)*width+i*w+k]=delt[n*w+k];??? ????? ???? // System out print( delt[ +(n*w+k)+ ]: +delt[n*w+k]+ );??? ?????????? }??? ??? //System out println();??? ??? /*??? ??? for(int n= ;nh;n++)??? ?????????? for(int k= ;kw;k++){??? ????? ????? System out print( processData[ +((j*h+n)*width+i*w+k)+ ]: +processData[(j*h+n)*width+i*w+k]+ );??? ?????????? }??? ??? System out println();??? ??? */??? ??} ??? ?}??? ???? ?return processData;??? }?

七 全局閾值處理 值化

public int[] thresholdProcess(int []data int width int height double coefficients double gate){??? ?int [] processData=new int[data length];??? ?if(data length!=width*height)??? ??return processData;??? ?else{??? ??double sum= ;??? ??double average= ;??? ??double variance= ;??? ??double threshold;??? ????? ??if( gate!= ){??? ???threshold=gate;???? ??}??? ??else{??? ??????? for(int i= ;iwidth*height;i++){??? ???? ?? sum+=data[i];??? ??????? }??? ??????? average=sum/(width*height);??? ????? ??????? for(int i= ;iwidth*height;i++){??? ???? ???? variance+=(data[i] average)*(data[i] average);??? ??????? }??? ??????? variance=Math sqrt(variance);??? ??????? threshold=average coefficients*variance;??? ??}??? ????? ???? for(int i= ;iwidth*height;i++){??? ???? ?if(data[i]threshold)??? ???? ??? processData[i]= ;??? ???? ?else ??? ???? ?????? processData[i]= ;??? ???? }??? ????? ???? return processData;?????? }??? }?

八 ?垂直邊緣檢測 sobel算子

public int[] verticleEdgeCheck(int []data int width int height int sobelCoefficients) throws Exception{??? ?int filterData[]=new int[data length];??? ?int min= ;??? ?int max= ;??? ?if(data length!=width*height)??? ??return filterData;??? ???? ?try{??? ???? ?? for(int i= ;iheight;i++){??? ?? for(int j= ;jwidth;j++){??? ??? if(i== || i== || i==height || i==height ??? ?????? ||j== || j== || j==width || j==width ){??? ?????????? filterData[i*width+j]=data[i*width+j];??? ???? }??? ???? else{??? ????? double average;??? ??????? //中心的九個像素點??? ???????? //average=data[i*width+j] Math sqrt( )*data[i*width+j ]+Math sqrt( )*data[i*width+j+ ]??? ????? average=data[i*width+j] sobelCoefficients*data[i*width+j ]+sobelCoefficients*data[i*width+j+ ]?????? ??? ????????????? data[(i )*width+j ]+data[(i )*width+j+ ]??? ???????????????? data[(i+ )*width+j ]+data[(i+ )*width+j+ ];??? ???????? filterData[i*width+j]=(int)(average);??? ???? }?? ??? ??? ??? if(filterData[i*width+j]min)???? ????min=filterData[i*width+j];???? ??? if(filterData[i*width+j]max)???? ????max=filterData[i*width+j];??? ??? }??? ??? }??? ?? for(int i= ;iwidth*height;i++){????? ??filterData[i]=(filterData[i] min)* /(max min);????? ?? }??? ???? ?}??? ?catch (Exception e) ??? ?{??????????? e printStackTrace();??????????? throw new Exception(e);??????? }? ??? ???? ?return filterData;??? }?

九 ?圖像平滑 * 掩模處理(平均處理) 降低噪聲

lishixinzhi/Article/program/Java/hx/201311/26286

用java編寫一個圖像處理,光線補償 、

寫了很多篇關(guān)于圖像處理的文章,沒有一篇介紹Java 2D的圖像處理API,文章討論和提及的

API都是基于JDK6的,首先來看Java中如何組織一個圖像對象BufferedImage的,如圖:

一個BufferedImage的像素數(shù)據(jù)儲存在Raster中,ColorModel里面儲存顏色空間,類型等

信息,當(dāng)前Java只支持一下三種圖像格式- JPG,PNG,GIF,如何向讓Java支持其它格式,首

先要 完成Java中的圖像讀寫接口,然后打成jar,加上啟動參數(shù)- Xbootclasspath/p

newimageformatIO.jar即可。

Java中如何讀寫一個圖像文件,使用ImageIO對象即可。讀圖像文件的代碼如下:

File file = new File("D:\\test\\blue_flower.jpg");

BufferedImage image = ImageIO.read(file);

寫圖像文件的代碼如下:

File outputfile = new File("saved.png");

ImageIO.write(bufferedImage, "png",outputfile);

從BufferedImage對象中讀取像素數(shù)據(jù)的代碼如下:

1 int type= image.getType();2 if ( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )3 ? ? ?return (int [])image.getRaster().getDataElements(x, y, width, height, pixels );4 else5 ? ? return image.getRGB( x, y, width, height, pixels, 0, width );

首先獲取圖像類型,如果不是32位的INT型數(shù)據(jù),直接讀寫RGB值即可,否則需要從Raster

對象中讀取。

往BufferedImage對象中寫入像素數(shù)據(jù)同樣遵守上面的規(guī)則。代碼如下:

1 int type= image.getType();2 if ( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )3 ?? image.getRaster().setDataElements(x, y, width, height, pixels );4 else5 ? ?image.setRGB(x, y, width, height, pixels, 0, width );

讀取圖像可能因為圖像文件比較大,需要一定時間的等待才可以,Java Advance Image

Processor API提供了MediaTracker對象來跟蹤圖像的加載,同步其它操作,使用方法如下:

MediaTracker tracker = new MediaTracker(this); //初始化對象 om/roucheng/tracker.addImage(image_01, 1); // 加入要跟蹤的BufferedImage對象image_001tracker.waitForID(1, 10000) // 等待10秒,讓iamge_01圖像加載

從一個32位int型數(shù)據(jù)cARGB中讀取圖像RGB顏色值的代碼如下:

1 int alpha = (cARGB 24) 0xff; //透明度通道 g/2 int red = (cARGB 16) 0xff;3 int green = (cARGB 8) 0xff;4 int blue = cARGB 0xff;

將RGB顏色值寫入成一個INT型數(shù)據(jù)cRGB的代碼如下:

cRGB = (alpha 24) | (red 16) | (green 8) | blue;

創(chuàng)建一個BufferedImage對象的代碼如下:

BufferedImage image = newBufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);

一個完整的源代碼Demo如下:

1 package com.gloomyfish.swing; ?2 ??3 import java.awt.BorderLayout; ?4 import java.awt.Dimension; ?5 import java.awt.Graphics; ?6 import java.awt.Graphics2D; ?7 import java.awt.RenderingHints; ?8 import java.awt.image.BufferedImage; ?9 import java.io.File; 10 import java.io.IOException; 11 ?12 import javax.imageio.ImageIO; 13 import javax.swing.JComponent; 14 import javax.swing.JFrame; 15 ?16 public class PlasmaDemo extends JComponent {

17 ? ?18 ? ? /** ?19 ?? ? *

20 ? ? ?*/ ? 21 ? ? private static final long serialVersionUID = -2236160343614397287L;

22 ? ? private BufferedImage image = null;

23 ? ? private int size = 256; 24 ? ? ? ?25 ? ? public PlasmaDemo() {

26 ? ? ? ? super();

27 ? ? ? ? this.setOpaque(false);

28 ?? ?}

29 ? ? ? ?30 ? ? protected void paintComponent(Graphics g) {

31 ? ? ? ? Graphics2D g2 = (Graphics2D)g;

32 ?? ? ? ?g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

33 ? ? ? ? g2.drawImage(getImage(), 5, 5, image.getWidth(), image.getHeight(), null);

34 ?? ?}

35 ? ? ? ?36 ? ? private BufferedImage getImage() {

37 ? ? ? ? if(image == null) {

38 ? ? ? ? ? ? image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);

39 ? ? ? ? ? ? int[] rgbData = new int[size*size];

40 ?? ? ? ? ? ?generateNoiseImage(rgbData);

41 ? ? ? ? ? ? setRGB(image, 0, 0, size, size, rgbData); 42 ? ? ? ? ? ? File outFile = new File("plasma.jpg"); 43 ? ? ? ? ? ? try { 44 ? ? ? ? ? ? ? ? ImageIO.write(image, "jpg", outFile); 45 ? ? ? ? ? ? } catch (IOException e) { 46 ?? ? ? ? ? ? ? ?e.printStackTrace(); 47 ?? ? ? ? ? ?} 48 ?? ? ? ?}

49 ? ? ? ? return image;

50 ?? ?}

51 ? ? ? ?52 ? ? public void generateNoiseImage(int[] rgbData) {

53 ? ? ? ? int index = 0;

54 ? ? ? ? int a = 255;

55 ? ? ? ? int r = 0;

56 ? ? ? ? int g = 0;

57 ? ? ? ? int b = 0;

58 ? 59 ? ? ? ? for(int row=0; rowsize; row++) {

60 ? ? ? ? ? ? for(int col=0; colsize; col++) {

61 ? ? ? ? ? ? ? ? // set random color value for each pixel ? 62 ? ? ? ? ? ? ? ? r = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0)));

63 ? ? ? ? ? ? ? ? g = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0)));

64 ? ? ? ? ? ? ? ? b = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0)));

65 ? ? ? ? ? ? ? ? ? ?66 ? ? ? ? ? ? ? ? rgbData[index] = ((clamp(a) 0xff) 24) |

67 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ((clamp(r) 0xff) 16) ?|

68 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ((clamp(g) 0xff) 8) ? |

69 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ((clamp(b) 0xff));

70 ? ? ? ? ? ? ? ? index++;

71 ?? ? ? ? ? ?}

72 ?? ? ? ?}

73 ? ? ? ? ? ?74 ?? ?}

75 ? ? ? ?76 ? ? private int clamp(int rgb) {

77 ? ? ? ? if(rgb 255)

78 ? ? ? ? ? ? return 255;

79 ? ? ? ? if(rgb 0)

80 ? ? ? ? ? ? return 0;

81 ? ? ? ? return rgb;

82 ?? ?}

83 ? ?84 ? ? public void setRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {

85 ? ? ? ? int type = image.getType();

86 ? ? ? ? if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )

87 ?? ? ? ? ? ?image.getRaster().setDataElements( x, y, width, height, pixels );

88 ? ? ? ? else ? 89 ? ? ? ? ? ? image.setRGB( x, y, width, height, pixels, 0, width );

90 ?? ?}

91 ? ? ? ?92 ? ? public static void main(String[] args) {

93 ? ? ? ? JFrame frame = new JFrame("Noise Art Panel");

94 ?? ? ? ?frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

95 ? ? ? ? frame.getContentPane().setLayout(new BorderLayout()); m/roucheng/ 98 ? ? ? ? frame.getContentPane().add(new PlasmaDemo(), BorderLayout.CENTER);

99 ? ? ? ? frame.setPreferredSize(new Dimension(400 + 25,450)); ?

100 ?? ? ? ?frame.pack(); ?

101 ? ? ? ? frame.setVisible(true); ?

102 ?? ?} ?

103 } ?

網(wǎng)頁標(biāo)題:Java的圖像處理代碼 java的圖像處理代碼是什么
當(dāng)前鏈接:http://muchs.cn/article4/dosesoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、網(wǎng)站設(shè)計公司動態(tài)網(wǎng)站、移動網(wǎng)站建設(shè)、外貿(mào)建站網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)