當前位置:首頁 » 操作系統 » 圖片變灰源碼

圖片變灰源碼

發布時間: 2023-05-26 01:52:44

java程序將彩色png圖片轉為黑白的

幫你搜了一段網上流行的代碼:

灰度變換
下面的程序使用三種方法對一個彩色圖像進行灰度變換,變換的效果都不一樣。一般而言,灰度變換的演算法是將象素的三個顏色分量使用R*0.3+G*0.59+B*0.11得到灰度值,然後將之賦值給紅綠藍,這樣顏色取得的效果就是灰度的。另一種就是取紅綠藍三色中的最大值作為灰度值。java核心包也有一種演算法,但是沒有看源代碼,不知道具體演算法是什麼樣的,效果和上述不同。

/* GrayFilter.java*/
/*@author:cherami */
/*email:[email protected]*/
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:[email protected]*/
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;
}
}

㈡ 類似騰訊空間好友相片點擊放大,可以旋轉、關閉、查看上、下一張圖片,圖片在放大的時候背景變灰源代碼

網路一下:LightBox

熱點內容
scratch少兒編程課程 發布:2025-04-16 17:11:44 瀏覽:642
榮耀x10從哪裡設置密碼 發布:2025-04-16 17:11:43 瀏覽:368
java從入門到精通視頻 發布:2025-04-16 17:11:43 瀏覽:88
php微信介面教程 發布:2025-04-16 17:07:30 瀏覽:310
android實現陰影 發布:2025-04-16 16:50:08 瀏覽:794
粉筆直播課緩存 發布:2025-04-16 16:31:21 瀏覽:346
機頂盒都有什麼配置 發布:2025-04-16 16:24:37 瀏覽:213
編寫手游反編譯都需要學習什麼 發布:2025-04-16 16:19:36 瀏覽:818
proteus編譯文件位置 發布:2025-04-16 16:18:44 瀏覽:367
土壓縮的本質 發布:2025-04-16 16:13:21 瀏覽:594