11package com .documentscanner ;
22
3- import android .app .Activity ;
43import android .content .Context ;
54import android .content .SharedPreferences ;
65import android .graphics .Bitmap ;
7- import android .graphics .Color ;
86import android .graphics .Paint ;
97import android .graphics .Path ;
108import android .graphics .drawable .shapes .PathShape ;
1614
1715import com .documentscanner .views .OpenNoteCameraView ;
1816import com .google .zxing .BinaryBitmap ;
19- import com .google .zxing .ChecksumException ;
20- import com .google .zxing .FormatException ;
2117import com .google .zxing .LuminanceSource ;
2218import com .google .zxing .NotFoundException ;
2319import com .google .zxing .RGBLuminanceSource ;
3531import org .opencv .core .Core ;
3632import org .opencv .core .CvType ;
3733import org .opencv .core .Mat ;
38- import org .opencv .core .MatOfInt ;
3934import org .opencv .core .MatOfPoint ;
4035import org .opencv .core .MatOfPoint2f ;
4136import org .opencv .core .Point ;
42- import org .opencv .core .Rect ;
43- import org .opencv .core .Scalar ;
4437import org .opencv .core .Size ;
4538import org .opencv .imgcodecs .Imgcodecs ;
4639import org .opencv .imgproc .Imgproc ;
4740
48- import org .opencv .calib3d .Calib3d ;
4941import java .util .ArrayList ;
5042import java .util .Arrays ;
5143import java .util .Collections ;
5244import java .util .Comparator ;
5345import java .util .Date ;
5446import java .util .HashMap ;
55- import java .util .List ;
5647
57- /**
58- * Created by allgood on 05/03/16.
59- */
6048public class ImageProcessor extends Handler {
6149
6250 private static final String TAG = "ImageProcessor" ;
63- private final Handler mUiHandler ;
6451 private final OpenNoteCameraView mMainActivity ;
6552 private boolean mBugRotate ;
66- private boolean colorMode = false ;
67- private boolean filterMode = true ;
6853 private double colorGain = 1 ; // contrast
6954 private double colorBias = 10 ; // bright
70- private int colorThresh = 115 ; // threshold
7155 private Size mPreviewSize ;
7256 private Point [] mPreviewPoints ;
73- private ResultPoint [] qrResultPoints ;
7457 private int numOfSquares = 0 ;
7558 private int numOfRectangles = 10 ;
76- private boolean noGrayscale = false ;
7759
78- public ImageProcessor (Looper looper , Handler uiHandler , OpenNoteCameraView mainActivity , Context context ) {
60+ public ImageProcessor (Looper looper , OpenNoteCameraView mainActivity , Context context ) {
7961 super (looper );
80- mUiHandler = uiHandler ;
8162 this .mMainActivity = mainActivity ;
8263 SharedPreferences sharedPref = PreferenceManager .getDefaultSharedPreferences (context );
8364 mBugRotate = sharedPref .getBoolean ("bug_rotate" , false );
@@ -95,10 +76,6 @@ public void setContrast(double contrast) {
9576 this .colorGain = contrast ;
9677 }
9778
98- public void setRemoveGrayScale (boolean grayscale ) {
99- this .noGrayscale = grayscale ;
100- }
101-
10279 public void handleMessage (Message msg ) {
10380
10481 if (msg .obj .getClass () == OpenNoteMessage .class ) {
@@ -108,50 +85,33 @@ public void handleMessage(Message msg) {
10885 String command = obj .getCommand ();
10986
11087 Log .d (TAG , "Message Received: " + command + " - " + obj .getObj ().toString ());
88+ // TODO: Manage command.equals("colorMode" || "filterMode"), return boolean
11189
11290 if (command .equals ("previewFrame" )) {
11391 processPreviewFrame ((PreviewFrame ) obj .getObj ());
11492 } else if (command .equals ("pictureTaken" )) {
11593 processPicture ((Mat ) obj .getObj ());
116- } else if (command .equals ("colorMode" )) {
117- colorMode = (Boolean ) obj .getObj ();
118- } else if (command .equals ("filterMode" )) {
119- filterMode = (Boolean ) obj .getObj ();
12094 }
12195 }
12296 }
12397
12498 private void processPreviewFrame (PreviewFrame previewFrame ) {
12599
126- Result [] results = {};
127-
128100 Mat frame = previewFrame .getFrame ();
129101
130- try {
131- results = zxing (frame );
132- } catch (ChecksumException | FormatException e ) {
133- // TODO Auto-generated catch block
134- e .printStackTrace ();
135- }
136-
137- boolean qrOk = false ;
138- String currentQR = null ;
102+ Result [] results = zxing (frame );
139103
140104 for (Result result : results ) {
141105 String qrText = result .getText ();
142106 if (Utils .isMatch (qrText , "^P.. V.. S[0-9]+" ) && checkQR (qrText )) {
143107 Log .d (TAG , "QR Code valid: " + result .getText ());
144- qrOk = true ;
145- currentQR = qrText ;
146- qrResultPoints = result .getResultPoints ();
108+ ResultPoint [] qrResultPoints = result .getResultPoints ();
147109 break ;
148110 } else {
149111 Log .d (TAG , "QR Code ignored: " + result .getText ());
150112 }
151113 }
152114
153- boolean autoMode = previewFrame .isAutoMode ();
154- boolean previewOnly = previewFrame .isPreviewOnly ();
155115 boolean focused = mMainActivity .isFocused ();
156116
157117 if (detectPreviewDocument (frame ) && focused ) {
@@ -170,7 +130,7 @@ private void processPreviewFrame(PreviewFrame previewFrame) {
170130
171131 }
172132
173- public void processPicture (Mat picture ) {
133+ private void processPicture (Mat picture ) {
174134
175135 Mat img = Imgcodecs .imdecode (picture , Imgcodecs .CV_LOAD_IMAGE_UNCHANGED );
176136 picture .release ();
@@ -191,7 +151,6 @@ public void processPicture(Mat picture) {
191151 picture .release ();
192152
193153 mMainActivity .setImageProcessorBusy (false );
194- mMainActivity .setAttemptToFocus (false );
195154 mMainActivity .waitSpinnerInvisible ();
196155
197156 }
@@ -213,15 +172,11 @@ private ScannedDocument detectDocument(Mat inputRgba) {
213172
214173 sd .originalPoints = new Point [4 ];
215174
216- sd .originalPoints [0 ] = new Point (sd .widthWithRatio - quad .points [3 ].y , quad .points [3 ].x ); // Topleft
175+ sd .originalPoints [0 ] = new Point (sd .widthWithRatio - quad .points [3 ].y , quad .points [3 ].x ); // TopLeft
217176 sd .originalPoints [1 ] = new Point (sd .widthWithRatio - quad .points [0 ].y , quad .points [0 ].x ); // TopRight
218177 sd .originalPoints [2 ] = new Point (sd .widthWithRatio - quad .points [1 ].y , quad .points [1 ].x ); // BottomRight
219178 sd .originalPoints [3 ] = new Point (sd .widthWithRatio - quad .points [2 ].y , quad .points [2 ].x ); // BottomLeft
220179
221- sd .previewPoints = mPreviewPoints ;
222-
223- MatOfPoint c = quad .contour ;
224-
225180 sd .quadrilateral = quad ;
226181 sd .previewPoints = mPreviewPoints ;
227182 sd .previewSize = mPreviewSize ;
@@ -235,7 +190,7 @@ private ScannedDocument detectDocument(Mat inputRgba) {
235190 return sd .setProcessed (doc );
236191 }
237192
238- private HashMap <String , Long > pageHistory = new HashMap <>();
193+ private final HashMap <String , Long > pageHistory = new HashMap <>();
239194
240195 private boolean checkQR (String qrCode ) {
241196
@@ -357,15 +312,15 @@ private Point[] sortPoints(Point[] src) {
357312 Comparator <Point > sumComparator = new Comparator <Point >() {
358313 @ Override
359314 public int compare (Point lhs , Point rhs ) {
360- return Double .valueOf (lhs .y + lhs .x ). compareTo ( rhs .y + rhs .x );
315+ return Double .compare (lhs .y + lhs .x , rhs .y + rhs .x );
361316 }
362317 };
363318
364319 Comparator <Point > diffComparator = new Comparator <Point >() {
365320
366321 @ Override
367322 public int compare (Point lhs , Point rhs ) {
368- return Double .valueOf (lhs .y - lhs .x ). compareTo ( rhs .y - rhs .x );
323+ return Double .compare (lhs .y - lhs .x , rhs .y - rhs .x );
369324 }
370325 };
371326
@@ -375,10 +330,10 @@ public int compare(Point lhs, Point rhs) {
375330 // bottom-right corner = maximal sum
376331 result [2 ] = Collections .max (srcPoints , sumComparator );
377332
378- // top-right corner = minimal diference
333+ // top-right corner = minimal difference
379334 result [1 ] = Collections .min (srcPoints , diffComparator );
380335
381- // bottom-left corner = maximal diference
336+ // bottom-left corner = maximal difference
382337 result [3 ] = Collections .max (srcPoints , diffComparator );
383338
384339 return result ;
@@ -413,52 +368,9 @@ private void enhanceDocument(Mat src) {
413368 src .convertTo (src , CvType .CV_8UC1 , colorGain , colorBias );
414369 }
415370
416- /**
417- * When a pixel have any of its three elements above the threshold value and the
418- * average of the three values are less than 80% of the higher one, brings all
419- * three values to the max possible keeping the relation between them, any
420- * absolute white keeps the value, all others go to absolute black.
421- *
422- * src must be a 3 channel image with 8 bits per channel
423- *
424- * @param src
425- * @param threshold
426- */
427- private void colorThresh (Mat src , int threshold ) {
428- Size srcSize = src .size ();
429- int size = (int ) (srcSize .height * srcSize .width ) * 3 ;
430- byte [] d = new byte [size ];
431- src .get (0 , 0 , d );
432-
433- for (int i = 0 ; i < size ; i += 3 ) {
434-
435- // the "& 0xff" operations are needed to convert the signed byte to double
436-
437- // avoid unneeded work
438- if ((double ) (d [i ] & 0xff ) == 255 ) {
439- continue ;
440- }
441-
442- double max = Math .max (Math .max ((double ) (d [i ] & 0xff ), (double ) (d [i + 1 ] & 0xff )),
443- (double ) (d [i + 2 ] & 0xff ));
444- double mean = ((double ) (d [i ] & 0xff ) + (double ) (d [i + 1 ] & 0xff ) + (double ) (d [i + 2 ] & 0xff )) / 3 ;
445-
446- if (max > threshold && mean < max * 0.8 ) {
447- d [i ] = (byte ) ((double ) (d [i ] & 0xff ) * 255 / max );
448- d [i + 1 ] = (byte ) ((double ) (d [i + 1 ] & 0xff ) * 255 / max );
449- d [i + 2 ] = (byte ) ((double ) (d [i + 2 ] & 0xff ) * 255 / max );
450- } else {
451- d [i ] = d [i + 1 ] = d [i + 2 ] = 0 ;
452- }
453- }
454- src .put (0 , 0 , d );
455- }
456-
457371 private Mat fourPointTransform (Mat src , Point [] pts ) {
458372
459373 double ratio = src .size ().height / 500 ;
460- int height = Double .valueOf (src .size ().height / ratio ).intValue ();
461- int width = Double .valueOf (src .size ().width / ratio ).intValue ();
462374
463375 Point tl = pts [0 ];
464376 Point tr = pts [1 ];
@@ -495,9 +407,9 @@ private Mat fourPointTransform(Mat src, Point[] pts) {
495407
496408 private ArrayList <MatOfPoint > findContours (Mat src ) {
497409
498- Mat grayImage = null ;
499- Mat cannedImage = null ;
500- Mat resizedImage = null ;
410+ Mat grayImage ;
411+ Mat cannedImage ;
412+ Mat resizedImage ;
501413
502414 double ratio = src .size ().height / 500 ;
503415 int height = Double .valueOf (src .size ().height / ratio ).intValue ();
@@ -513,7 +425,7 @@ private ArrayList<MatOfPoint> findContours(Mat src) {
513425 Imgproc .GaussianBlur (grayImage , grayImage , new Size (5 , 5 ), 0 );
514426 Imgproc .Canny (grayImage , cannedImage , 80 , 100 , 3 , false );
515427
516- ArrayList <MatOfPoint > contours = new ArrayList <MatOfPoint >();
428+ ArrayList <MatOfPoint > contours = new ArrayList <>();
517429 Mat hierarchy = new Mat ();
518430
519431 Imgproc .findContours (cannedImage , contours , hierarchy , Imgproc .RETR_TREE , Imgproc .CHAIN_APPROX_SIMPLE );
@@ -524,7 +436,7 @@ private ArrayList<MatOfPoint> findContours(Mat src) {
524436
525437 @ Override
526438 public int compare (MatOfPoint lhs , MatOfPoint rhs ) {
527- return Double .valueOf (Imgproc .contourArea (rhs )). compareTo ( Imgproc .contourArea (lhs ));
439+ return Double .compare (Imgproc .contourArea (rhs ), Imgproc .contourArea (lhs ));
528440 }
529441 });
530442
@@ -535,9 +447,9 @@ public int compare(MatOfPoint lhs, MatOfPoint rhs) {
535447 return contours ;
536448 }
537449
538- private QRCodeMultiReader qrCodeMultiReader = new QRCodeMultiReader ();
450+ private final QRCodeMultiReader qrCodeMultiReader = new QRCodeMultiReader ();
539451
540- public Result [] zxing (Mat inputImage ) throws ChecksumException , FormatException {
452+ private Result [] zxing (Mat inputImage ) {
541453
542454 int w = inputImage .width ();
543455 int h = inputImage .height ();
@@ -564,7 +476,7 @@ public Result[] zxing(Mat inputImage) throws ChecksumException, FormatException
564476 Result [] results = {};
565477 try {
566478 results = qrCodeMultiReader .decodeMultiple (bitmap );
567- } catch (NotFoundException e ) {
479+ } catch (NotFoundException ignored ) {
568480 }
569481
570482 return results ;
0 commit comments