项目地址:https://github.com/Rbtoooooooooo/camera_take_demo
得到该像素点的r值,g值,b值。将rgb值加起来去平均值avg,如果0<
avg<
85则rgb的值都设置为0,如果85<=avg<170则设置rgb的为127,其他情况rgb的值设置为255,然后在根据新的rgb值设置新的像素值。
我的除了黑白两个颜色外,其实还有灰色。
int avg;
for (int i = 0; i < width * height; i++) {
color = oldPx[i];
a = Color.alpha(color);
r = Color.red(color);
g = Color.green(color);
b = Color.blue(color);
avg = (int)(r + g + b) / 3;
if (avg >= 0 && avg < 85) { // 黑色
r = 0;
g = 0;
b = 0;
} else if (avg >= 85 && avg < 170) { // 灰色
r = 127;
g = 127;
b = 127;
} else { // 白色
r = 255;
g = 255;
b = 255;
}
newPx[i] = Color.argb(a, r, g, b);
}
bmp.setPixels(newPx, 0, width, 0, 0, width, height);
得到该像素点的rgb值后,根据前人总结的经验,即特定的公式,就可以得到新的rgb值,然后在生成新的像素点
for (int i = 0; i < width * height; i++) {
color = oldPx[i];
a = Color.alpha(color);
r = Color.red(color);
g = Color.green(color);
b = Color.blue(color);
r1 = (int) (0.393 * r + 0.769 * g + 0.189 * b);
g1 = (int) (0.349 * r + 0.686 * g + 0.168 * b);
b1 = (int) (0.272 * r + 0.534 * g + 0.131 * b);
if (r1 > 255) {
r1 = 255;
}
if (g1 > 255) {
g1 = 255;
}
if (b1 > 255) {
b1 = 255;
}
newPx[i] = Color.argb(a, r1, g1, b1);
}
bmp.setPixels(newPx, 0, width, 0, 0, width, height);
for (int i = 0; i < width * height; i++) {
color = oldPx[i];
r = Color.red(color);
g = Color.green(color);
b = Color.blue(color);
a = Color.alpha(color);
r = 255 - r;
g = 255 - g;
b = 255 - b;
if (r > 255) {
r = 255;
} else if (r < 0) {
r = 0;
}
if (g > 255) {
g = 255;
} else if (g < 0) {
g = 0;
}
if (b > 255) {
b = 255;
} else if (b < 0) {
b = 0;
}
newPx[i] = Color.argb(a, r, g, b);
}
bmp.setPixels(newPx, 0, width, 0, 0, width, height);
取i-1处的像素点,i处的新像素点的rgb由i处的旧像素点的rgb减去i-1处的像素点的rgb再加上127。若加上127后大于255,则令其等于255
for (int i = 1; i < width * height; i++) {
colorBefore = oldPx[i - 1];
a = Color.alpha(colorBefore);
r = Color.red(colorBefore);
g = Color.green(colorBefore);
b = Color.blue(colorBefore);
color = oldPx[i];
r1 = Color.red(color);
g1 = Color.green(color);
b1 = Color.blue(color);
r = (r - r1 + 127);
g = (g - g1 + 127);
b = (b - b1 + 127);
if (r > 255) {
r = 255;
}
if (g > 255) {
g = 255;
}
if (b > 255) {
b = 255;
}
newPx[i] = Color.argb(a, r, g, b);
}
bmp.setPixels(newPx, 0, width, 0, 0, width, height);
令新图左边的像素点等于原图右边的像素点,新图右边的像素点等于左边的像素点。
for (int i = 0; i < pHeight; i++) {
for (int j = 0; j < pWidth; j++) {
newPexils[i*pWidth+j] = oldPexils[i*pWidth + pWidth - j - 1];
}
}
bitmap.setPixels(newPexils, 0, pWidth, 0, 0, pWidth, pHeight);