我正在开发一个像学习图像处理和android应用程序的Instagram。但是我卡住了,我的应用程序中实施灰度过滤器时出现问题。我正在尝试一种简单的方法来将位图中的各个像素转换为灰度。如何操作位图中的像素?
这里是全班我写信给不同的过滤器应用于图像:
package com.dosa2.photoeditor.ImageEffects;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
public class ImageEffects {
Bitmap bitmap;
int width, height;
public ImageEffects(Bitmap bitmap) {
this.bitmap = bitmap;
width = bitmap.getWidth();
height = bitmap.getHeight();
}
public Bitmap toGrayscale() {
Bitmap resultBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(resultBitmap);
Paint p = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
p.setColorFilter(f);
c.drawBitmap(bitmap, 0, 0, p);
return resultBitmap;
}
public Bitmap toGrayscale2() {
Bitmap resultBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for(int i=0;i<height;i++) {
for (int j=0;i<width;j++) {
int c = bitmap.getPixel(i,j);
resultBitmap.setPixel(i, j, (Color.red(c)+Color.blue(c)+Color.green(c)/3));
}
}
return resultBitmap;
}
}
我曾尝试2种方法的位图转换成灰度。前者似乎在工作(但我无法理解),后者则不是。 任何人都可以帮我吗?如果有更简单的方式来操纵Android中的图片,请务必提及。
前者(使用ColorMatrix)是最有效的。因为它可以一次完成整个位图,而不必循环到2个循环。您可能一开始并不了解ColorMAtrices,但有很多文档(不仅在Java中,而且在Android中也是如此 - 例如,我也在VB,NET和C#中使用它们) –
我在哪里可以找到文档或一个良好的学习资源?对于Google而言,这个Google地址是 – jstraj
。 –