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