一.灰度图
/**
* 将彩色图转换为灰度图
*
* @param img 位图
* @return 返回转换好的位图
*/
public static Bitmap convertGreyImg(Bitmap img) {
int width = img.getWidth(); //获取位图的宽
int height = img.getHeight(); //获取位图的高
int[] pixels = new int[width * height]; //通过位图的大小创建像素点数组
img.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];
int red = ((grey & 0x00FF0000) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF);
grey = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11);
grey = alpha | (grey << 16) | (grey << 8) | grey;
pixels[width * i + j] = grey;
}
}
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
result.setPixels(pixels, 0, width, 0, 0, width, height);
return result;
}
二.二值化
/**
* 图片进行二值化黑白
*/
static Bitmap zeroAndOne(Bitmap bm) {
int width = bm.getWidth();//原图像宽度
int height = bm.getHeight();//原图像高度
int color;//用来存储某个像素点的颜色值
int r, g, b, a;//红,绿,蓝,透明度
//创建空白图像,宽度等于原图宽度,高度等于原图高度,用ARGB_8888渲染,这个不用了解,这样写就行了
Bitmap bmp = Bitmap.createBitmap(width, height
, Bitmap.Config.ARGB_8888);
int[] oldPx = new int[width * height];//用来存储原图每个像素点的颜色信息
int[] newPx = new int[width * height];//用来处理处理之后的每个像素点的颜色信息
/**
* 第一个参数oldPix[]:用来接收(存储)bm这个图像中像素点颜色信息的数组
* 第二个参数offset:oldPix[]数组中第一个接收颜色信息的下标值
* 第三个参数width:在行之间跳过像素的条目数,必须大于等于图像每行的像素数
* 第四个参数x:从图像bm中读取的第一个像素的横坐标
* 第五个参数y:从图像bm中读取的第一个像素的纵坐标
* 第六个参数width:每行需要读取的像素个数
* 第七个参数height:需要读取的行总数
*/
bm.getPixels(oldPx, 0, width, 0, 0, width, height);//获取原图中的像素信息
for (int i = 0; i < width * height; i++) {//循环处理图像中每个像素点的颜色值
color = oldPx[i];//取得某个点的像素值
r = Color.red(color);//取得此像素点的r(红色)分量
g = Color.green(color);//取得此像素点的g(绿色)分量
b = Color.blue(color);//取得此像素点的b(蓝色分量)
a = Color.alpha(color);//取得此像素点的a通道值
//此公式将r,g,b运算获得灰度值,经验公式不需要理解
int gray = (int) ((float) r * 0.3 + (float) g * 0.59 + (float) b * 0.11);
//下面前两个if用来做溢出处理,防止灰度公式得到到灰度超出范围(0-255)
if (gray > 255) {
gray = 255;
}
if (gray < 0) {
gray = 0;
}
if (gray != 0) {//如果某像素的灰度值不是0(黑色)就将其置为255(白色)
gray = 255;
}
newPx[i] = Color.argb(a, gray, gray, gray);//将处理后的透明度(没变),r,g,b分量重新合成颜色值并将其存储在数组中
}
/**
* 第一个参数newPix[]:需要赋给新图像的颜色数组//The colors to write the bitmap
* 第二个参数offset:newPix[]数组中第一个需要设置给图像颜色的下标值//The index of the first color to read from pixels[]
* 第三个参数width:在行之间跳过像素的条目数//The number of colors in pixels[] to skip between rows.
* Normally this value will be the same as the width of the bitmap,but it can be larger(or negative).
* 第四个参数x:从图像bm中读取的第一个像素的横坐标//The x coordinate of the first pixels to write to in the bitmap.
* 第五个参数y:从图像bm中读取的第一个像素的纵坐标//The y coordinate of the first pixels to write to in the bitmap.
* 第六个参数width:每行需要读取的像素个数The number of colors to copy from pixels[] per row.
* 第七个参数height:需要读取的行总数//The number of rows to write to the bitmap.
*/
bmp.setPixels(newPx, 0, width, 0, 0, width, height);//将处理后的像素信息赋给新图
return bmp;//返回处理后的图像
}
三.颜色反转
public class ImageProcessUtils {
private static int sWidth; //width
private static int sHeight; //height
private static int sRow; //Row--height
private static int sCol; //col--width
private static int sPixel = 0;
private static int sIndex;
//ARGB values
private static int sA = 0;
private static int sR = 0;
private static int sG = 0;
private static int sB = 0;
private static int[] sPixels;
private static int[] pStore;
private static int[] sOriginal;
public static Bitmap invertBitmap(Bitmap bitmap) {
sWidth = bitmap.getWidth();
sHeight = bitmap.getHeight();
sPixels = new int[sWidth * sHeight];
bitmap.getPixels(sPixels, 0, sWidth, 0, 0, sWidth, sHeight);
sIndex = 0;
for (sRow = 0; sRow < sHeight; sRow++) {
sIndex = sRow * sWidth;
for (sCol = 0; sCol < sWidth; sCol++) {
sPixel = sPixels[sIndex];
sA = (sPixel >> 24) & 0xff;
sR = (sPixel >> 16) & 0xff;
sG = (sPixel >> 8) & 0xff;
sB = sPixel & 0xff;
sR = 255 - sR;
sG = 255 - sG;
sB = 255 - sB;
sPixel = ((sA & 0xff) << 24 | (sR & 0xff) << 16 | (sG & 0xff) << 8 | sB & 0xff);
sPixels[sIndex] = sPixel;
sIndex++;
}
}
bitmap.setPixels(sPixels, 0, sWidth, 0, 0, sWidth, sHeight);
return bitmap;
}
}