On my N1, it was about 4x faster to use ColorMatrix.
Bitmap image =
BitmapFactory.decodeResource(this.getResources(),
R.drawable.testimage);
Paint paint = new Paint();
int width = image.getWidth();
int height = image.getHeight();
int[] pixels = new int[width * height];
image.getPixels(pixels, 0, width, 0, 0, width, height);
boolean useColorMatrix = false;
long start = System.nanoTime();
if (useColorMatrix) {
ColorMatrix cm = new ColorMatrix();
float a = 77f;
float b = 151f;
float c = 28f;
float t = 120 * -256f;
cm.set(new float[] {
a, b, c, 0, t,
a, b, c, 0, t,
a, b, c, 0, t,
0, 0, 0, 1, 0 });
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(image, 0, 0,
paint);
} else {
Config config = Config.ARGB_8888;
Bitmap threshold =
Bitmap.createBitmap(image.getWidth(), image.getHeight(),
config);
for (int i = 0; i < pixels.length; i++) {
int point = pixels[i];
if(point < Color.GRAY) {
pixels[i] = Color.BLACK;
} else {
pixels[i] = Color.WHITE;
}
}
threshold.setPixels(pixels, 0, width, 0, 0, width,
height);
canvas.drawBitmap(threshold, 0, 0, paint);
}
long end = System.nanoTime();
double elapsed = (end - start) / 1e6;
paint.setColor(Color.WHITE);
canvas.drawText("elapsed = " + elapsed, 0, 100, paint);
On Apr 1, 5:33 pm, paladin <[email protected]> wrote:
> I think I should have mentioned that I already had desaturated the
> image. So the color comparison is correct.
>
> On Apr 1, 5:55 pm, Kevin Huber <[email protected]> wrote:
>
> > You should work on the array of pixel data directly.
> > This document may be
> > helpfulhttp://www.3programmers.com/mwells/documents/pdf/Final%20Report.pdf
>
> > I don't think the comparison you are doing if (point < Color.GRAY) is
> > valid since Bitmap pixels are ARGB data. I would expect to see code
> > bit shifting out the R G B components.
>
> > -Kevin
>
> > On Apr 1, 4:14 pm, paladin <[email protected]> wrote:
>
> > > I am using the following code to flip each pixel in a bitmap to black
> > > or white:
>
> > > for(int x = 0; x < newBitmap.getWidth(); x++) {
> > > for(int y = 0; y < newBitmap.getHeight(); y++) {
> > > int point = newBitmap.getPixel(x, y);
> > > if(point < Color.GRAY) {
> > > paint.setColor(Color.BLACK);
> > > c.drawPoint(x, y, paint);
> > > } else {
> > > paint.setColor(Color.WHITE);
> > > c.drawPoint(x, y, paint);
> > > }
> > > }
> > > }
>
> > > It works, but it is extraordinarily slow for the size image I'm using
> > > (half the resolution of the camera, 1296x972). I have also tried
> > > setPixel, and I'm getting about the same performance. Anyone out there
> > > know of a faster way to do this?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
To unsubscribe, reply using "remove me" as the subject.