1. ビットマップグレースケール
//获取灰度图片
public static Bitmap getHuiDu(Bitmap bitMap, Context context) {
int width = bitMap.getWidth();//Width 图片宽度
int height = bitMap.getHeight();// Height 图片高度
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Config.RGB_565);
// bmpGrayscale 设置灰度之后的bitmap对象
//设置灰度
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bitMap, 0, 0, paint);
c.save();
c.restore();
);
return bmpGrayscale;
}
2. ビットマップの二値化、ここでは ARGB_8888 が使用されます
public static Bitmap convertToBMW(Bitmap bmp) {
int width = bmp.getWidth(); // 获取位图的宽
int height = bmp.getHeight(); // 获取位图的高
int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组
// 设定二值化的域值,默认值为100
int tmp = 150;
bmp.getPixels(pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int grey = pixels[width * i + j];
// 分离三原色
alpha = ((grey & 0xFF000000) >> 24);
int red = ((grey & 0x00FF0000) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF);
int i1 = 0xFFFF;
if (red > tmp) {
red = 255;
} else {
red = 0;
}
if (blue > tmp) {
blue = 255;
} else {
blue = 0;
}
if (green > tmp) {
green = 255;
} else {
green = 0;
}
pixels[width * i + j] = alpha << 24 | red << 16 | green << 8
| blue;
if (pixels[width * i + j] == -1) {
pixels[width * i + j] = -1;
} else {
pixels[width * i + j] = -16777216;
}
}
}
// 新建图片
Bitmap newBmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
// 设置图片数据
newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
Bitmap resizeBmp = ThumbnailUtils.extractThumbnail(newBmp, width, height);
// return getHuiDu(resizeBmp, AppApplication.getIntance());
return resizeBmp;
}