This is the code I’m using to convert two colored pictures to grayscale and compare them pixel by pixel. My app keeps force closing though. here’s the code:
public boolean equals(Bitmap bitmap1, Bitmap bitmap2) {
Bitmap grayscaleBitmap1 = Bitmap.createBitmap(
bitmap1.getWidth(), bitmap1.getHeight(),
Bitmap.Config.RGB_565);
Canvas c = new Canvas(grayscaleBitmap1);
Paint p = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(cm);
p.setColorFilter(filter);
c.drawBitmap(bitmap1, 0, 0, p);
Bitmap grayscaleBitmap2 = Bitmap.createBitmap(
bitmap2.getWidth(), bitmap2.getHeight(),
Bitmap.Config.RGB_565);
Canvas c1 = new Canvas(grayscaleBitmap2);
c1.drawBitmap(bitmap2, 0, 0, p);
ByteBuffer buffer1 = ByteBuffer.allocate(grayscaleBitmap1.getHeight()
* grayscaleBitmap1.getRowBytes());
grayscaleBitmap1.copyPixelsToBuffer(buffer1);
ByteBuffer buffer2 = ByteBuffer.allocate(grayscaleBitmap2.getHeight()
* grayscaleBitmap2.getRowBytes());
grayscaleBitmap2.copyPixelsToBuffer(buffer2);
return Arrays.equals(buffer1.array(), buffer2.array());
}
Here’s the logcat.
These lines says that you have wrote Toast somewhere in your async Task.
please put your Toast into
runOnUiThread(); as i shown below :
and you are done.