/
ScanUtils.java
executable file
·591 lines (499 loc) · 22.6 KB
/
ScanUtils.java
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
package com.adityaarora.liveedgedetection.util;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.hardware.Camera;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.Surface;
import com.adityaarora.liveedgedetection.view.Quadrilateral;
import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.opencv.utils.Converters;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import static org.opencv.core.CvType.CV_8UC1;
import static org.opencv.imgproc.Imgproc.THRESH_BINARY;
import static org.opencv.imgproc.Imgproc.THRESH_OTSU;
/**
* This class provides utilities for camera.
*/
public class ScanUtils {
private static final String TAG = ScanUtils.class.getSimpleName();
public static boolean compareFloats(double left, double right) {
double epsilon = 0.00000001;
return Math.abs(left - right) < epsilon;
}
public static Camera.Size determinePictureSize(Camera camera, Camera.Size previewSize) {
if (camera == null) return null;
Camera.Parameters cameraParams = camera.getParameters();
List<Camera.Size> pictureSizeList = cameraParams.getSupportedPictureSizes();
Collections.sort(pictureSizeList, new Comparator<Camera.Size>() {
@Override
public int compare(Camera.Size size1, Camera.Size size2) {
Double h1 = Math.sqrt(size1.width * size1.width + size1.height * size1.height);
Double h2 = Math.sqrt(size2.width * size2.width + size2.height * size2.height);
return h2.compareTo(h1);
}
});
Camera.Size retSize = null;
// if the preview size is not supported as a picture size
float reqRatio = ((float) previewSize.width) / previewSize.height;
float curRatio, deltaRatio;
float deltaRatioMin = Float.MAX_VALUE;
for (Camera.Size size : pictureSizeList) {
curRatio = ((float) size.width) / size.height;
deltaRatio = Math.abs(reqRatio - curRatio);
if (deltaRatio < deltaRatioMin) {
deltaRatioMin = deltaRatio;
retSize = size;
}
if (ScanUtils.compareFloats(deltaRatio, 0)) {
break;
}
}
return retSize;
}
public static Camera.Size getOptimalPreviewSize(Camera camera, int w, int h) {
if (camera == null) return null;
final double targetRatio = (double) h / w;
Camera.Parameters cameraParams = camera.getParameters();
List<Camera.Size> previewSizeList = cameraParams.getSupportedPreviewSizes();
Collections.sort(previewSizeList, new Comparator<Camera.Size>() {
@Override
public int compare(Camera.Size size1, Camera.Size size2) {
double ratio1 = (double) size1.width / size1.height;
double ratio2 = (double) size2.width / size2.height;
Double ratioDiff1 = Math.abs(ratio1 - targetRatio);
Double ratioDiff2 = Math.abs(ratio2 - targetRatio);
if (ScanUtils.compareFloats(ratioDiff1, ratioDiff2)) {
Double h1 = Math.sqrt(size1.width * size1.width + size1.height * size1.height);
Double h2 = Math.sqrt(size2.width * size2.width + size2.height * size2.height);
return h2.compareTo(h1);
}
return ratioDiff1.compareTo(ratioDiff2);
}
});
return previewSizeList.get(0);
}
public static int getDisplayOrientation(Activity activity, int cameraId) {
Camera.CameraInfo info = new Camera.CameraInfo();
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
DisplayMetrics dm = new DisplayMetrics();
Camera.getCameraInfo(cameraId, info);
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int displayOrientation;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
displayOrientation = (info.orientation + degrees) % 360;
displayOrientation = (360 - displayOrientation) % 360;
} else {
displayOrientation = (info.orientation - degrees + 360) % 360;
}
return displayOrientation;
}
public static Camera.Size getOptimalPictureSize(Camera camera, final int width, final int height, final Camera.Size previewSize) {
if (camera == null) return null;
Camera.Parameters cameraParams = camera.getParameters();
List<Camera.Size> supportedSizes = cameraParams.getSupportedPictureSizes();
Camera.Size size = camera.new Size(width, height);
// convert to landscape if necessary
if (size.width < size.height) {
int temp = size.width;
size.width = size.height;
size.height = temp;
}
Camera.Size requestedSize = camera.new Size(size.width, size.height);
double previewAspectRatio = (double) previewSize.width / (double) previewSize.height;
if (previewAspectRatio < 1.0) {
// reset ratio to landscape
previewAspectRatio = 1.0 / previewAspectRatio;
}
Log.d(TAG, "CameraPreview previewAspectRatio " + previewAspectRatio);
double aspectTolerance = 0.1;
double bestDifference = Double.MAX_VALUE;
for (int i = 0; i < supportedSizes.size(); i++) {
Camera.Size supportedSize = supportedSizes.get(i);
// Perfect match
if (supportedSize.equals(requestedSize)) {
Log.d(TAG, "CameraPreview optimalPictureSize " + supportedSize.width + 'x' + supportedSize.height);
return supportedSize;
}
double difference = Math.abs(previewAspectRatio - ((double) supportedSize.width / (double) supportedSize.height));
if (difference < bestDifference - aspectTolerance) {
// better aspectRatio found
if ((width != 0 && height != 0) || (supportedSize.width * supportedSize.height < 2048 * 1024)) {
size.width = supportedSize.width;
size.height = supportedSize.height;
bestDifference = difference;
}
} else if (difference < bestDifference + aspectTolerance) {
// same aspectRatio found (within tolerance)
if (width == 0 || height == 0) {
// set highest supported resolution below 2 Megapixel
if ((size.width < supportedSize.width) && (supportedSize.width * supportedSize.height < 2048 * 1024)) {
size.width = supportedSize.width;
size.height = supportedSize.height;
}
} else {
// check if this pictureSize closer to requested width and height
if (Math.abs(width * height - supportedSize.width * supportedSize.height) < Math.abs(width * height - size.width * size.height)) {
size.width = supportedSize.width;
size.height = supportedSize.height;
}
}
}
}
Log.d(TAG, "CameraPreview optimalPictureSize " + size.width + 'x' + size.height);
return size;
}
public static Camera.Size getOptimalPreviewSize(int displayOrientation, List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (displayOrientation == 90 || displayOrientation == 270) {
targetRatio = (double) h / w;
}
if (sizes == null) {
return null;
}
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
Log.d("optimal preview size", "w: " + optimalSize.width + " h: " + optimalSize.height);
return optimalSize;
}
public static int configureCameraAngle(Activity activity) {
int angle;
Display display = activity.getWindowManager().getDefaultDisplay();
switch (display.getRotation()) {
case Surface.ROTATION_0: // This is display orientation
angle = 90; // This is camera orientation
break;
case Surface.ROTATION_90:
angle = 0;
break;
case Surface.ROTATION_180:
angle = 270;
break;
case Surface.ROTATION_270:
angle = 180;
break;
default:
angle = 90;
break;
}
return angle;
}
public static Quadrilateral detectLargestQuadrilateral(Mat mat) {
Mat mGrayMat = new Mat(mat.rows(), mat.cols(), CV_8UC1);
Imgproc.cvtColor(mat, mGrayMat, Imgproc.COLOR_BGR2GRAY, 4);
Imgproc.threshold(mGrayMat, mGrayMat, 150, 255, THRESH_BINARY + THRESH_OTSU);
List<MatOfPoint> largestContour = findLargestContour(mGrayMat);
if (null != largestContour) {
Quadrilateral mLargestRect = findQuadrilateral(largestContour);
if (mLargestRect != null)
return mLargestRect;
}
return null;
}
public static double getMaxCosine(double maxCosine, Point[] approxPoints) {
Log.i(TAG, "ANGLES ARE:");
for (int i = 2; i < 5; i++) {
double cosine = Math.abs(angle(approxPoints[i % 4], approxPoints[i - 2], approxPoints[i - 1]));
Log.i(TAG, String.valueOf(cosine));
maxCosine = Math.max(cosine, maxCosine);
}
return maxCosine;
}
private static double angle(Point p1, Point p2, Point p0) {
double dx1 = p1.x - p0.x;
double dy1 = p1.y - p0.y;
double dx2 = p2.x - p0.x;
double dy2 = p2.y - p0.y;
return (dx1 * dx2 + dy1 * dy2) / Math.sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
}
private static Point[] sortPoints(Point[] src) {
ArrayList<Point> srcPoints = new ArrayList<>(Arrays.asList(src));
Point[] result = {null, null, null, null};
Comparator<Point> sumComparator = new Comparator<Point>() {
@Override
public int compare(Point lhs, Point rhs) {
return Double.valueOf(lhs.y + lhs.x).compareTo(rhs.y + rhs.x);
}
};
Comparator<Point> diffComparator = new Comparator<Point>() {
@Override
public int compare(Point lhs, Point rhs) {
return Double.valueOf(lhs.y - lhs.x).compareTo(rhs.y - rhs.x);
}
};
// top-left corner = minimal sum
result[0] = Collections.min(srcPoints, sumComparator);
// bottom-right corner = maximal sum
result[2] = Collections.max(srcPoints, sumComparator);
// top-right corner = minimal difference
result[1] = Collections.min(srcPoints, diffComparator);
// bottom-left corner = maximal difference
result[3] = Collections.max(srcPoints, diffComparator);
return result;
}
private static List<MatOfPoint> findLargestContour(Mat inputMat) {
Mat mHierarchy = new Mat();
List<MatOfPoint> mContourList = new ArrayList<>();
//finding contours
Imgproc.findContours(inputMat, mContourList, mHierarchy, Imgproc.RETR_EXTERNAL,
Imgproc.CHAIN_APPROX_SIMPLE);
Mat mContoursMat = new Mat();
mContoursMat.create(inputMat.rows(), inputMat.cols(), CvType.CV_8U);
if (mContourList.size() != 0) {
Collections.sort(mContourList, new Comparator<MatOfPoint>() {
@Override
public int compare(MatOfPoint lhs, MatOfPoint rhs) {
return Double.valueOf(Imgproc.contourArea(rhs)).compareTo(Imgproc.contourArea(lhs));
}
});
return mContourList;
}
return null;
}
private static Quadrilateral findQuadrilateral(List<MatOfPoint> mContourList) {
for (MatOfPoint c : mContourList) {
MatOfPoint2f c2f = new MatOfPoint2f(c.toArray());
double peri = Imgproc.arcLength(c2f, true);
MatOfPoint2f approx = new MatOfPoint2f();
Imgproc.approxPolyDP(c2f, approx, 0.02 * peri, true);
Point[] points = approx.toArray();
// select biggest 4 angles polygon
if (approx.rows() == 4) {
Point[] foundPoints = sortPoints(points);
return new Quadrilateral(approx, foundPoints);
}
}
return null;
}
public static Bitmap enhanceReceipt(Bitmap image, Point topLeft, Point topRight, Point bottomLeft, Point bottomRight) {
int resultWidth = (int) (topRight.x - topLeft.x);
int bottomWidth = (int) (bottomRight.x - bottomLeft.x);
if (bottomWidth > resultWidth)
resultWidth = bottomWidth;
int resultHeight = (int) (bottomLeft.y - topLeft.y);
int bottomHeight = (int) (bottomRight.y - topRight.y);
if (bottomHeight > resultHeight)
resultHeight = bottomHeight;
Mat inputMat = new Mat(image.getHeight(), image.getHeight(), CvType.CV_8UC1);
Utils.bitmapToMat(image, inputMat);
Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC1);
List<Point> source = new ArrayList<>();
source.add(topLeft);
source.add(topRight);
source.add(bottomLeft);
source.add(bottomRight);
Mat startM = Converters.vector_Point2f_to_Mat(source);
Point ocvPOut1 = new Point(0, 0);
Point ocvPOut2 = new Point(resultWidth, 0);
Point ocvPOut3 = new Point(0, resultHeight);
Point ocvPOut4 = new Point(resultWidth, resultHeight);
List<Point> dest = new ArrayList<>();
dest.add(ocvPOut1);
dest.add(ocvPOut2);
dest.add(ocvPOut3);
dest.add(ocvPOut4);
Mat endM = Converters.vector_Point2f_to_Mat(dest);
Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);
Imgproc.warpPerspective(inputMat, outputMat, perspectiveTransform, new Size(resultWidth, resultHeight));
Bitmap output = Bitmap.createBitmap(resultWidth, resultHeight, Bitmap.Config.ARGB_8888);
Utils.matToBitmap(outputMat, output);
return output;
}
public static String[] saveToInternalMemory(Bitmap bitmap, String mFileDirectory, String
mFileName, Context mContext, int mQuality) {
String[] mReturnParams = new String[2];
File mDirectory = getBaseDirectoryFromPathString(mFileDirectory, mContext);
File mPath = new File(mDirectory, mFileName);
try {
FileOutputStream mFileOutputStream = new FileOutputStream(mPath);
//Compress method used on the Bitmap object to write image to output stream
bitmap.compress(Bitmap.CompressFormat.JPEG, mQuality, mFileOutputStream);
mFileOutputStream.close();
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
mReturnParams[0] = mDirectory.getAbsolutePath();
mReturnParams[1] = mFileName;
return mReturnParams;
}
private static File getBaseDirectoryFromPathString(String mPath, Context mContext) {
ContextWrapper mContextWrapper = new ContextWrapper(mContext);
return mContextWrapper.getDir(mPath, Context.MODE_PRIVATE);
}
public static Bitmap decodeBitmapFromFile(String path, String imageName) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
return BitmapFactory.decodeFile(new File(path, imageName).getAbsolutePath(),
options);
}
/*
* This method converts the dp value to px
* @param context context
* @param dp value in dp
* @return px value
*/
public static int dp2px(Context context, float dp) {
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
return Math.round(px);
}
public static Bitmap decodeBitmapFromByteArray(byte[] data, int reqWidth, int reqHeight) {
// Raw height and width of image
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
// Calculate inSampleSize
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
options.inSampleSize = inSampleSize;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeByteArray(data, 0, data.length, options);
}
@Deprecated
public static Bitmap loadEfficientBitmap(byte[] data, int width, int height) {
Bitmap bmp;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, width, height);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options);
return bmp;
}
private static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap resize(Bitmap image, int maxWidth, int maxHeight) {
if (maxHeight > 0 && maxWidth > 0) {
int width = image.getWidth();
int height = image.getHeight();
float ratioBitmap = (float) width / (float) height;
float ratioMax = (float) maxWidth / (float) maxHeight;
int finalWidth = maxWidth;
int finalHeight = maxHeight;
if (ratioMax > 1) {
finalWidth = (int) ((float) maxHeight * ratioBitmap);
} else {
finalHeight = (int) ((float) maxWidth / ratioBitmap);
}
image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
return image;
} else {
return image;
}
}
public static Bitmap resizeToScreenContentSize(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(
bm, 0, 0, width, height, matrix, false);
bm.recycle();
return resizedBitmap;
}
public static ArrayList<PointF> getPolygonDefaultPoints(Bitmap bitmap) {
ArrayList<PointF> points;
points = new ArrayList<>();
points.add(new PointF(bitmap.getWidth() * (0.14f), (float) bitmap.getHeight() * (0.13f)));
points.add(new PointF(bitmap.getWidth() * (0.84f), (float) bitmap.getHeight() * (0.13f)));
points.add(new PointF(bitmap.getWidth() * (0.14f), (float) bitmap.getHeight() * (0.83f)));
points.add(new PointF(bitmap.getWidth() * (0.84f), (float) bitmap.getHeight() * (0.83f)));
return points;
}
public static boolean isScanPointsValid(Map<Integer, PointF> points) {
return points.size() == 4;
}
}