11package edu .washington .cs .ubicomplab .rdt_reader .core ;
22
3- import android .app .ListActivity ;
43import android .content .Context ;
54import android .graphics .Bitmap ;
65import android .graphics .BitmapFactory ;
6+ import android .util .Base64 ;
77
88import org .json .JSONArray ;
99import org .json .JSONException ;
@@ -60,6 +60,15 @@ public class RDT {
6060
6161 public boolean rotated = false ;
6262
63+
64+ public RDT (JSONObject rdtConfig ) {
65+ try {
66+ init (rdtConfig , convertBase64StrToBitmap (rdtConfig .optString ("REF_IMG" )));
67+ } catch (Exception ex ) {
68+ ex .printStackTrace ();
69+ }
70+ }
71+
6372 public RDT (Context context , String rdtName ) {
6473 try {
6574 // Read config.json
@@ -69,89 +78,106 @@ public RDT(Context context, String rdtName) {
6978 is .read (buffer );
7079 is .close ();
7180 JSONObject obj = new JSONObject (new String (buffer , "UTF-8" )).getJSONObject (rdtName );
72-
73- // Load the template image
7481 refImageID = context .getResources ().getIdentifier (obj .getString ("REF_IMG" ),
7582 "drawable" , context .getPackageName ());
76- refImg = new Mat ();
7783 Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), refImageID );
78- Utils .bitmapToMat (bitmap , refImg );
79-
80- if (refImg .height () > refImg .width ()) {
81- Core .rotate (refImg , refImg , Core .ROTATE_90_COUNTERCLOCKWISE );
82- rotated = true ;
83- }
84-
85- cvtColor (refImg , refImg , Imgproc .COLOR_RGB2GRAY );
86- this .rdtName = rdtName ;
87-
88- // Pull data related to UI
89- viewFinderScaleH = obj .getDouble ("VIEW_FINDER_SCALE" );
90- viewFinderScaleW = (viewFinderScaleH * (double )refImg .height ()/(double )refImg .width ())+Constants .VIEW_FINDER_SCALE_W_PADDING ;
91- //viewFinderScaleW = obj.getDouble("VIEW_FINDER_SCALE_W");
92- JSONArray rectTL = obj .getJSONArray ("RESULT_WINDOW_TOP_LEFT" );
93- JSONArray rectBR = obj .getJSONArray ("RESULT_WINDOW_BOTTOM_RIGHT" );
94- resultWindowRect = rotated ? new Rect (new Point (rectTL .getDouble (1 ), rectTL .getDouble (0 )),
95- new Point (rectBR .getDouble (1 ), rectBR .getDouble (0 ))) :
96- new Rect (new Point (rectTL .getDouble (0 ), rectTL .getDouble (1 )),
97- new Point (rectBR .getDouble (0 ), rectBR .getDouble (1 )));
98-
99- // Pull data related to the result window
100- topLinePosition = rotated ? obj .getJSONArray ("TOP_LINE_POSITION" ).getDouble (1 ) - resultWindowRect .x : obj .getJSONArray ("TOP_LINE_POSITION" ).getDouble (0 ) - resultWindowRect .x ;
101- middleLinePosition = rotated ? obj .getJSONArray ("MIDDLE_LINE_POSITION" ).getDouble (1 ) - resultWindowRect .x : obj .getJSONArray ("MIDDLE_LINE_POSITION" ).getDouble (0 ) - resultWindowRect .x ;
102- bottomLinePosition = rotated ? obj .getJSONArray ("BOTTOM_LINE_POSITION" ).getDouble (1 ) - resultWindowRect .x : obj .getJSONArray ("BOTTOM_LINE_POSITION" ).getDouble (0 ) - resultWindowRect .x ;
103- topLineName = obj .getString ("TOP_LINE_NAME" );
104- middleLineName = obj .getString ("MIDDLE_LINE_NAME" );
105- bottomLineName = obj .getString ("BOTTOM_LINE_NAME" );
106- lineIntensity = obj .getInt ("LINE_INTENSITY" );
107- lineSearchWidth = obj .has ("LINE_SEARCH_WIDTH" ) ? obj .getInt ("LINE_SEARCH_WIDTH" ) :
108- Math .max ((int )((middleLinePosition -topLinePosition )/2.0 ),(int )((bottomLinePosition -middleLinePosition )/2.0 ));
109-
110- checkGlare = obj .has ("CHECK_GLARE" ) ? obj .getBoolean ("CHECK_GLARE" ) : false ;
111-
112- // Pull data related to fiducials
113- fiducials = obj .has ("FIDUCIALS" ) ? obj .getJSONArray ("FIDUCIALS" ) : new JSONArray ();
114- hasFiducial = fiducials .length () > 0 ;
115- distanctFromFiducialToResultWindow = 0 ;
116-
117- if (hasFiducial && fiducials .length () == 2 ) {
118- JSONArray trueFiducial1 = fiducials .getJSONArray (0 );
119- Point trueFiducialTL1 = rotated
120- ? new Point (trueFiducial1 .getJSONArray (0 ).getDouble (1 ), trueFiducial1 .getJSONArray (0 ).getDouble (0 ))
121- : new Point (trueFiducial1 .getJSONArray (0 ).getDouble (0 ), trueFiducial1 .getJSONArray (0 ).getDouble (1 ));
122- Point trueFiducialBR1 = rotated
123- ? new Point (trueFiducial1 .getJSONArray (1 ).getDouble (1 ), trueFiducial1 .getJSONArray (1 ).getDouble (0 ))
124- : new Point (trueFiducial1 .getJSONArray (1 ).getDouble (0 ), trueFiducial1 .getJSONArray (1 ).getDouble (1 ));
125-
126- JSONArray trueFiducial2 = fiducials .getJSONArray (1 );
127- Point trueFiducialTL2 = rotated
128- ? new Point (trueFiducial2 .getJSONArray (0 ).getDouble (1 ), trueFiducial2 .getJSONArray (0 ).getDouble (0 ))
129- : new Point (trueFiducial2 .getJSONArray (0 ).getDouble (0 ), trueFiducial2 .getJSONArray (0 ).getDouble (1 ));
130- Point trueFiducialBR2 = rotated
131- ? new Point (trueFiducial2 .getJSONArray (1 ).getDouble (1 ), trueFiducial2 .getJSONArray (1 ).getDouble (0 ))
132- : new Point (trueFiducial2 .getJSONArray (1 ).getDouble (0 ), trueFiducial2 .getJSONArray (1 ).getDouble (1 ));
133-
134- fiducialRects = new ArrayList <>();
135-
136- fiducialRects .add (new Rect (trueFiducialTL1 , trueFiducialBR1 ));
137- fiducialRects .add (new Rect (trueFiducialTL2 , trueFiducialBR2 ));
138-
139- distanctFromFiducialToResultWindow = resultWindowRect .x - (trueFiducialBR2 .x + trueFiducialBR1 .x )/2.0 ;
140- }
141-
142- // Store the reference's sharpness
143- Size kernel = new Size (SHARPNESS_GAUSSIAN_BLUR_WINDOW ,
144- SHARPNESS_GAUSSIAN_BLUR_WINDOW );
145- Imgproc .GaussianBlur (refImg , refImg , kernel , 0 , 0 );
146-
147- // Load the reference image's features
148- refDescriptor = new Mat ();
149- refKeypoints = new MatOfKeyPoint ();
150- detector = SIFT .create ();
151- matcher = BFMatcher .create (BFMatcher .BRUTEFORCE , false );
152- detector .detectAndCompute (refImg , new Mat (), refKeypoints , refDescriptor );
84+ init (obj , bitmap );
15385 } catch (Exception ex ) {
15486 ex .printStackTrace ();
15587 }
15688 }
89+
90+ private Bitmap convertBase64StrToBitmap (String base64Str ) {
91+ return convertByteArrayToBitmap (Base64 .decode (base64Str .getBytes (), Base64 .DEFAULT ));
92+ }
93+
94+ private Bitmap convertByteArrayToBitmap (final byte [] src ) {
95+ return BitmapFactory .decodeByteArray (src , 0 , src .length );
96+ }
97+
98+ private void init (JSONObject obj , Bitmap bitmap ) throws JSONException {
99+ // Load the template image
100+ refImg = new Mat ();
101+ Utils .bitmapToMat (bitmap , refImg );
102+
103+ if (refImg .height () > refImg .width ()) {
104+ Core .rotate (refImg , refImg , Core .ROTATE_90_COUNTERCLOCKWISE );
105+ rotated = true ;
106+ }
107+
108+ cvtColor (refImg , refImg , Imgproc .COLOR_RGB2GRAY );
109+ this .rdtName = rdtName ;
110+
111+ // Pull data related to UI
112+ viewFinderScaleH = obj .getDouble ("VIEW_FINDER_SCALE" );
113+ viewFinderScaleW = (viewFinderScaleH * (double )refImg .height ()/(double )refImg .width ())+Constants .VIEW_FINDER_SCALE_W_PADDING ;
114+ //viewFinderScaleW = obj.getDouble("VIEW_FINDER_SCALE_W");
115+ JSONArray rectTL = obj .getJSONArray ("RESULT_WINDOW_TOP_LEFT" );
116+ JSONArray rectBR = obj .getJSONArray ("RESULT_WINDOW_BOTTOM_RIGHT" );
117+ resultWindowRect = rotated ? new Rect (new Point (rectTL .getDouble (1 ), rectTL .getDouble (0 )),
118+ new Point (rectBR .getDouble (1 ), rectBR .getDouble (0 ))) :
119+ new Rect (new Point (rectTL .getDouble (0 ), rectTL .getDouble (1 )),
120+ new Point (rectBR .getDouble (0 ), rectBR .getDouble (1 )));
121+
122+ // Pull data related to the result window
123+ topLinePosition = rotated ? obj .getJSONArray ("TOP_LINE_POSITION" ).getDouble (1 ) - resultWindowRect .x : obj .getJSONArray ("TOP_LINE_POSITION" ).getDouble (0 ) - resultWindowRect .x ;
124+ middleLinePosition = rotated ? obj .getJSONArray ("MIDDLE_LINE_POSITION" ).getDouble (1 ) - resultWindowRect .x : obj .getJSONArray ("MIDDLE_LINE_POSITION" ).getDouble (0 ) - resultWindowRect .x ;
125+ bottomLinePosition = getBottomLinePosition (obj , rotated );
126+ topLineName = obj .getString ("TOP_LINE_NAME" );
127+ middleLineName = obj .getString ("MIDDLE_LINE_NAME" );
128+ bottomLineName = obj .optString ("BOTTOM_LINE_NAME" );
129+ lineIntensity = obj .getInt ("LINE_INTENSITY" );
130+ lineSearchWidth = obj .has ("LINE_SEARCH_WIDTH" ) ? obj .getInt ("LINE_SEARCH_WIDTH" ) :
131+ Math .max ((int )((middleLinePosition -topLinePosition )/2.0 ),(int )((bottomLinePosition -middleLinePosition )/2.0 ));
132+
133+ checkGlare = obj .has ("CHECK_GLARE" ) ? obj .getBoolean ("CHECK_GLARE" ) : false ;
134+
135+ // Pull data related to fiducials
136+ fiducials = obj .has ("FIDUCIALS" ) ? obj .getJSONArray ("FIDUCIALS" ) : new JSONArray ();
137+ hasFiducial = fiducials .length () > 0 ;
138+ distanctFromFiducialToResultWindow = 0 ;
139+
140+ if (hasFiducial && fiducials .length () == 2 ) {
141+ JSONArray trueFiducial1 = fiducials .getJSONArray (0 );
142+ Point trueFiducialTL1 = rotated
143+ ? new Point (trueFiducial1 .getJSONArray (0 ).getDouble (1 ), trueFiducial1 .getJSONArray (0 ).getDouble (0 ))
144+ : new Point (trueFiducial1 .getJSONArray (0 ).getDouble (0 ), trueFiducial1 .getJSONArray (0 ).getDouble (1 ));
145+ Point trueFiducialBR1 = rotated
146+ ? new Point (trueFiducial1 .getJSONArray (1 ).getDouble (1 ), trueFiducial1 .getJSONArray (1 ).getDouble (0 ))
147+ : new Point (trueFiducial1 .getJSONArray (1 ).getDouble (0 ), trueFiducial1 .getJSONArray (1 ).getDouble (1 ));
148+
149+ JSONArray trueFiducial2 = fiducials .getJSONArray (1 );
150+ Point trueFiducialTL2 = rotated
151+ ? new Point (trueFiducial2 .getJSONArray (0 ).getDouble (1 ), trueFiducial2 .getJSONArray (0 ).getDouble (0 ))
152+ : new Point (trueFiducial2 .getJSONArray (0 ).getDouble (0 ), trueFiducial2 .getJSONArray (0 ).getDouble (1 ));
153+ Point trueFiducialBR2 = rotated
154+ ? new Point (trueFiducial2 .getJSONArray (1 ).getDouble (1 ), trueFiducial2 .getJSONArray (1 ).getDouble (0 ))
155+ : new Point (trueFiducial2 .getJSONArray (1 ).getDouble (0 ), trueFiducial2 .getJSONArray (1 ).getDouble (1 ));
156+
157+ fiducialRects = new ArrayList <>();
158+
159+ fiducialRects .add (new Rect (trueFiducialTL1 , trueFiducialBR1 ));
160+ fiducialRects .add (new Rect (trueFiducialTL2 , trueFiducialBR2 ));
161+
162+ distanctFromFiducialToResultWindow = resultWindowRect .x - (trueFiducialBR2 .x + trueFiducialBR1 .x )/2.0 ;
163+ }
164+
165+ // Store the reference's sharpness
166+ Size kernel = new Size (SHARPNESS_GAUSSIAN_BLUR_WINDOW ,
167+ SHARPNESS_GAUSSIAN_BLUR_WINDOW );
168+ Imgproc .GaussianBlur (refImg , refImg , kernel , 0 , 0 );
169+
170+ // Load the reference image's features
171+ refDescriptor = new Mat ();
172+ refKeypoints = new MatOfKeyPoint ();
173+ detector = SIFT .create ();
174+ matcher = BFMatcher .create (BFMatcher .BRUTEFORCE , false );
175+ detector .detectAndCompute (refImg , new Mat (), refKeypoints , refDescriptor );
176+ }
177+
178+ private double getBottomLinePosition (JSONObject rdtConfig , boolean rotated ) throws JSONException {
179+ return rdtConfig .optJSONArray ("BOTTOM_LINE_POSITION" ) == null ? 0
180+ : rotated ? rdtConfig .getJSONArray ("BOTTOM_LINE_POSITION" ).getDouble (1 ) - resultWindowRect .x
181+ : rdtConfig .getJSONArray ("BOTTOM_LINE_POSITION" ).getDouble (0 ) - resultWindowRect .x ;
182+ }
157183}
0 commit comments