如果您使用的是12级或更高级别的API,则Bitmap上有一种称为sameAs的方法可以完全满足您的需求.否则,请使用getPixels并执行以下操作:

int width = bitmap1.getWidth();

int height = bitmap1.getHeight();

int pixelCount = width * height;

int[] pixels1 = new int[pixelCount];

int[] pixels2 = new int[pixelCount];

bitmap1.getPixels(pixels1, 0, 0, 0, 0, width, height);

bitmap2.getPixels(pixels2, 0, 0, 0, 0, width, height);

for (int i = 0; i < pixelCount; i++) {

if (pixels1[i] != pixels2[i]) {

return false;

}

}

return true;

或者,如果您真的想做点相反的事情以查看有多少个像素相同,请继续进行操作.

实际上,您也可以对缓冲区做一些事情……也许像

int width = bitmap1.getWidth();

int height = bitmap1.getHeight();

int pixelCount = width * height;

IntBuffer buffer1 = IntBuffer.allocate(pixelCount);

IntBuffer buffer2 = IntBuffer.allocate(pixelCount);

bitmap1.copyPixelsToBuffer(buffer1);

bitmap2.copyPixelsToBuffer(buffer2);

int result = buffer1.compareTo(buffer2);

我不确定这两种方法的性能如何比较,但是如果需要,可以尝试一下.

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐