Android - 比较位图的快速算法

时间:2016-07-26 19:48:57

标签: android performance bitmap

我正在寻找重复的图片。

我正在使用此代码来执行此操作

private static boolean compare(Bitmap b1, Bitmap b2) {
    if (b1.getWidth() == b2.getWidth() && b1.getHeight() == b2.getHeight()) {
        int[] pixels1 = new int[b1.getWidth() * b1.getHeight()];
        int[] pixels2 = new int[b2.getWidth() * b2.getHeight()];
        b1.getPixels(pixels1, 0, b1.getWidth(), 0, 0, b1.getWidth(), b1.getHeight());
        b2.getPixels(pixels2, 0, b2.getWidth(), 0, 0, b2.getWidth(), b2.getHeight());
        if (Arrays.equals(pixels1, pixels2)) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

代码工作正常,但是当我放入图像列表时它非常慢。

public static Bitmap getBitmapFromPath(String path){
    File sd = Environment.getExternalStorageDirectory();
    File image = new File(path);
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inPreferredConfig = Bitmap.Config.RGB_565;
    bmOptions.inSampleSize = 2;
    Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
    return bitmap;
}

public static ArrayList<String> compareListOfImages(Activity activity){
    ArrayList<String> pathList = FileUtils.getImagesPath(activity);
    ArrayList<String> pathListResult = new ArrayList<>();
    if(pathList!=null) {
        for (int i = 0; i < pathList.size(); i++){
            Log.d("I", i + " ITEM");
            Bitmap bitmap1 = FileUtils.getBitmapFromPath(pathList.get(i));
            for (int k =0; k <pathList.size(); k++){
                Log.d("PATH", pathList.get(k));
                Bitmap bitmap2 = FileUtils.getBitmapFromPath(pathList.get(k));
                if(!pathList.get(i).equals(pathList.get(k))) {
                    if (FileUtils.compare(bitmap1, bitmap2)) {
                        pathListResult.add(pathList.get(k));
                    }
                }
            }
        }
    }

    return pathListResult;
} 

有人知道如何更快地获得结果吗?

0 个答案:

没有答案