Compare the pixel of the two different images, takes too long time

前端 未结 3 1817
春和景丽
春和景丽 2021-01-16 12:11

I want to compare the pixel of the two different images. I am comparing the pixel of first image with all the pixels of the second Image. Here is my code stuff:



        
3条回答
  •  天涯浪人
    2021-01-16 12:30

    If you're using API level 12 or above, there's a method called sameAs on Bitmap to do exactly what you're looking for. Otherwise, use getPixels and do something like:

    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;
    

    Or if you really want to do the counter thing to see how many pixels are the same, go ahead and do that.

    Actually, you can probably do something with the buffers too... Maybe something like

    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);
    

    I'm not sure how those two methods compare in performance, but it's something to play around with if you want.

提交回复
热议问题