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 ();
@@ -212,15 +172,11 @@ private ScannedDocument detectDocument(Mat inputRgba) {
212172
213173 sd .originalPoints = new Point [4 ];
214174
215- 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
216176 sd .originalPoints [1 ] = new Point (sd .widthWithRatio - quad .points [0 ].y , quad .points [0 ].x ); // TopRight
217177 sd .originalPoints [2 ] = new Point (sd .widthWithRatio - quad .points [1 ].y , quad .points [1 ].x ); // BottomRight
218178 sd .originalPoints [3 ] = new Point (sd .widthWithRatio - quad .points [2 ].y , quad .points [2 ].x ); // BottomLeft
219179
220- sd .previewPoints = mPreviewPoints ;
221-
222- MatOfPoint c = quad .contour ;
223-
224180 sd .quadrilateral = quad ;
225181 sd .previewPoints = mPreviewPoints ;
226182 sd .previewSize = mPreviewSize ;
@@ -234,7 +190,7 @@ private ScannedDocument detectDocument(Mat inputRgba) {
234190 return sd .setProcessed (doc );
235191 }
236192
237- private HashMap <String , Long > pageHistory = new HashMap <>();
193+ private final HashMap <String , Long > pageHistory = new HashMap <>();
238194
239195 private boolean checkQR (String qrCode ) {
240196
@@ -356,15 +312,15 @@ private Point[] sortPoints(Point[] src) {
356312 Comparator <Point > sumComparator = new Comparator <Point >() {
357313 @ Override
358314 public int compare (Point lhs , Point rhs ) {
359- return Double .valueOf (lhs .y + lhs .x ). compareTo ( rhs .y + rhs .x );
315+ return Double .compare (lhs .y + lhs .x , rhs .y + rhs .x );
360316 }
361317 };
362318
363319 Comparator <Point > diffComparator = new Comparator <Point >() {
364320
365321 @ Override
366322 public int compare (Point lhs , Point rhs ) {
367- return Double .valueOf (lhs .y - lhs .x ). compareTo ( rhs .y - rhs .x );
323+ return Double .compare (lhs .y - lhs .x , rhs .y - rhs .x );
368324 }
369325 };
370326
@@ -374,10 +330,10 @@ public int compare(Point lhs, Point rhs) {
374330 // bottom-right corner = maximal sum
375331 result [2 ] = Collections .max (srcPoints , sumComparator );
376332
377- // top-right corner = minimal diference
333+ // top-right corner = minimal difference
378334 result [1 ] = Collections .min (srcPoints , diffComparator );
379335
380- // bottom-left corner = maximal diference
336+ // bottom-left corner = maximal difference
381337 result [3 ] = Collections .max (srcPoints , diffComparator );
382338
383339 return result ;
@@ -412,52 +368,9 @@ private void enhanceDocument(Mat src) {
412368 src .convertTo (src , CvType .CV_8UC1 , colorGain , colorBias );
413369 }
414370
415- /**
416- * When a pixel have any of its three elements above the threshold value and the
417- * average of the three values are less than 80% of the higher one, brings all
418- * three values to the max possible keeping the relation between them, any
419- * absolute white keeps the value, all others go to absolute black.
420- *
421- * src must be a 3 channel image with 8 bits per channel
422- *
423- * @param src
424- * @param threshold
425- */
426- private void colorThresh (Mat src , int threshold ) {
427- Size srcSize = src .size ();
428- int size = (int ) (srcSize .height * srcSize .width ) * 3 ;
429- byte [] d = new byte [size ];
430- src .get (0 , 0 , d );
431-
432- for (int i = 0 ; i < size ; i += 3 ) {
433-
434- // the "& 0xff" operations are needed to convert the signed byte to double
435-
436- // avoid unneeded work
437- if ((double ) (d [i ] & 0xff ) == 255 ) {
438- continue ;
439- }
440-
441- double max = Math .max (Math .max ((double ) (d [i ] & 0xff ), (double ) (d [i + 1 ] & 0xff )),
442- (double ) (d [i + 2 ] & 0xff ));
443- double mean = ((double ) (d [i ] & 0xff ) + (double ) (d [i + 1 ] & 0xff ) + (double ) (d [i + 2 ] & 0xff )) / 3 ;
444-
445- if (max > threshold && mean < max * 0.8 ) {
446- d [i ] = (byte ) ((double ) (d [i ] & 0xff ) * 255 / max );
447- d [i + 1 ] = (byte ) ((double ) (d [i + 1 ] & 0xff ) * 255 / max );
448- d [i + 2 ] = (byte ) ((double ) (d [i + 2 ] & 0xff ) * 255 / max );
449- } else {
450- d [i ] = d [i + 1 ] = d [i + 2 ] = 0 ;
451- }
452- }
453- src .put (0 , 0 , d );
454- }
455-
456371 private Mat fourPointTransform (Mat src , Point [] pts ) {
457372
458373 double ratio = src .size ().height / 500 ;
459- int height = Double .valueOf (src .size ().height / ratio ).intValue ();
460- int width = Double .valueOf (src .size ().width / ratio ).intValue ();
461374
462375 Point tl = pts [0 ];
463376 Point tr = pts [1 ];
@@ -494,9 +407,9 @@ private Mat fourPointTransform(Mat src, Point[] pts) {
494407
495408 private ArrayList <MatOfPoint > findContours (Mat src ) {
496409
497- Mat grayImage = null ;
498- Mat cannedImage = null ;
499- Mat resizedImage = null ;
410+ Mat grayImage ;
411+ Mat cannedImage ;
412+ Mat resizedImage ;
500413
501414 double ratio = src .size ().height / 500 ;
502415 int height = Double .valueOf (src .size ().height / ratio ).intValue ();
@@ -512,7 +425,7 @@ private ArrayList<MatOfPoint> findContours(Mat src) {
512425 Imgproc .GaussianBlur (grayImage , grayImage , new Size (5 , 5 ), 0 );
513426 Imgproc .Canny (grayImage , cannedImage , 80 , 100 , 3 , false );
514427
515- ArrayList <MatOfPoint > contours = new ArrayList <MatOfPoint >();
428+ ArrayList <MatOfPoint > contours = new ArrayList <>();
516429 Mat hierarchy = new Mat ();
517430
518431 Imgproc .findContours (cannedImage , contours , hierarchy , Imgproc .RETR_TREE , Imgproc .CHAIN_APPROX_SIMPLE );
@@ -523,7 +436,7 @@ private ArrayList<MatOfPoint> findContours(Mat src) {
523436
524437 @ Override
525438 public int compare (MatOfPoint lhs , MatOfPoint rhs ) {
526- return Double .valueOf (Imgproc .contourArea (rhs )). compareTo ( Imgproc .contourArea (lhs ));
439+ return Double .compare (Imgproc .contourArea (rhs ), Imgproc .contourArea (lhs ));
527440 }
528441 });
529442
@@ -534,9 +447,9 @@ public int compare(MatOfPoint lhs, MatOfPoint rhs) {
534447 return contours ;
535448 }
536449
537- private QRCodeMultiReader qrCodeMultiReader = new QRCodeMultiReader ();
450+ private final QRCodeMultiReader qrCodeMultiReader = new QRCodeMultiReader ();
538451
539- public Result [] zxing (Mat inputImage ) throws ChecksumException , FormatException {
452+ private Result [] zxing (Mat inputImage ) {
540453
541454 int w = inputImage .width ();
542455 int h = inputImage .height ();
@@ -563,7 +476,7 @@ public Result[] zxing(Mat inputImage) throws ChecksumException, FormatException
563476 Result [] results = {};
564477 try {
565478 results = qrCodeMultiReader .decodeMultiple (bitmap );
566- } catch (NotFoundException e ) {
479+ } catch (NotFoundException ignored ) {
567480 }
568481
569482 return results ;
0 commit comments