1package com.android.rs.refocus; 2 3import android.content.Context; 4import android.graphics.Bitmap; 5import android.graphics.BitmapFactory; 6import android.os.Environment; 7import android.util.Log; 8 9import java.io.ByteArrayOutputStream; 10import java.io.File; 11import java.nio.ByteBuffer; 12 13/** 14 * Created by xinyiwang on 6/30/15. 15 */ 16public class ImageCompare { 17 private static byte[] loadImageByteArray(String file_path) { 18 Bitmap bitmap = BitmapFactory.decodeFile(file_path); 19 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 20 bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 21 return stream.toByteArray(); 22 } 23 24 public static boolean compareImage(String file1, String file2) { 25 byte[] first = loadImageByteArray(file1); 26 byte[] second = loadImageByteArray(file2); 27 28 for (int i = 0; i < first.length; i++) { 29 int v1 = 0xFF & first[i]; 30 int v2 = 0xFF & second[i]; 31 int error = Math.abs(v1 - v2); 32 if (error > 2) { 33 return false; 34 } 35 } 36 return true; 37 } 38 39 private static byte[] loadBitmapByteArray(Bitmap bitmap) { 40 int bytes = bitmap.getByteCount(); 41 ByteBuffer buffer = ByteBuffer.allocate(bytes); 42 bitmap.copyPixelsToBuffer(buffer); 43 byte[] array = buffer.array(); 44 return array; 45 } 46 47 public static class CompareValue { 48 float aveDiff = 0; 49 float diffPercent = 0f; 50 } 51 52 public static void compareBitmap(Bitmap bitmap1, Bitmap bitmap2, CompareValue result) { 53 byte[] first = loadBitmapByteArray(bitmap1); 54 byte[] second = loadBitmapByteArray(bitmap2); 55 56 int loopCount = first.length > second.length ? second.length : first.length; 57 58 int diffCount = 0; 59 int diffSum = 0; 60 for (int i = 0; i < loopCount; i++) { 61 int v1 = 0xFF & first[i]; 62 int v2 = 0xFF & second[i]; 63 int error = Math.abs(v1 - v2); 64 if (error > 0) { 65 diffCount++; 66 //if (error > result.maxDiff) { 67 //result.maxDiff = error; 68 //} 69 diffSum += error; 70 } 71 } 72 result.diffPercent = ((float)diffCount)/first.length; 73 result.aveDiff = ((float)diffSum)/first.length; 74 } 75 76 public static void compareIntermediate(String folder1, String folder2) { 77 File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 78 String folder_path = folder.getAbsolutePath(); 79 //String folder_path = "/storage/self/primary/"; 80 String file_path_1_base = folder_path + "/" + folder1; 81 String file_path_2_base = folder_path + "/" + folder2; 82 File dir1 = new File(file_path_1_base); 83 84 for ( File imgFile : dir1.listFiles()) { 85 String file_path_2 = file_path_2_base + "/" + imgFile.getName(); 86 String file_path_1 = file_path_1_base + "/" + imgFile.getName(); 87 System.out.println(file_path_1); 88 System.out.println(file_path_2); 89 boolean same = compareImage(file_path_1, file_path_2); 90 if (same) { 91 Log.d("imageCompare:", imgFile.getName() + " is the same!"); 92 } else { 93 Log.d("imageCompare:", imgFile.getName() + " is different!"); 94 } 95 } 96 } 97 98 public static void printWrongIndex(String folder1, String folder2, String file, Context mContext) { 99 File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 100 String folder_path = folder.getAbsolutePath(); 101 //String folder_path = "/storage/self/primary/"; 102 String file_path_1 = folder_path + "/" + folder1 + "/" + file; 103 String file_path_2 = folder_path + "/" + folder2 + "/" + file; 104 105 106 107 Bitmap bitmap1 = BitmapFactory.decodeFile(file_path_1); 108 ByteArrayOutputStream stream1 = new ByteArrayOutputStream(); 109 bitmap1.compress(Bitmap.CompressFormat.PNG, 100, stream1); 110 byte[] first = stream1.toByteArray(); 111 112 Bitmap bitmap2 = BitmapFactory.decodeFile(file_path_1); 113 ByteArrayOutputStream stream2 = new ByteArrayOutputStream(); 114 bitmap2.compress(Bitmap.CompressFormat.PNG, 100, stream2); 115 byte[] second = stream2.toByteArray(); 116 117 int width = bitmap1.getWidth(); 118 int height = bitmap1.getHeight(); 119 120 byte[] difference = new byte[first.length]; 121 //System.out.println("Total pixel: " + width * height); 122 //System.out.println("intdifference length: " + intdifference.length); 123 //System.out.println("byte array length" + first.length); 124 125 for (int i = 0; i < first.length; i++) { 126 int v1 = 0xFF & first[i]; 127 int v2 = 0xFF & second[i]; 128 int error = Math.abs(v1 - v2); 129 //if (error > 2 ) { 130 // intdifference[i/4] = 0; 131 //} else { 132 // intdifference[i/4] = 255; 133 //} 134 difference[i] = (byte)(first[i] - second[i]); 135 } 136 137 //Bitmap differenceBitmap = Bitmap.createBitmap(difference, width, height, Bitmap.Config.ARGB_8888); 138 139 Bitmap differenceBitmap = BitmapFactory.decodeByteArray(difference,0, difference.length); 140 MediaStoreSaver.savePNG(differenceBitmap, "difference", "updateSharp1Difference.png", mContext); 141 System.out.println("difference image saved!"); 142 } 143} 144