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()。
没有相关教程