Skip to content

Commit 6d6571a

Browse files
Fix NPE when extracting rdt config data
1 parent 61f990d commit 6d6571a

File tree

1 file changed

+9
-3
lines changed
  • lib/src/main/java/edu/washington/cs/ubicomplab/rdt_reader/core

1 file changed

+9
-3
lines changed

lib/src/main/java/edu/washington/cs/ubicomplab/rdt_reader/core/RDT.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public RDT(Context context, String rdtName) {
8080
JSONObject obj = new JSONObject(new String(buffer, "UTF-8")).getJSONObject(rdtName);
8181
refImageID = context.getResources().getIdentifier(obj.getString("REF_IMG"),
8282
"drawable", context.getPackageName());
83-
refImg = new Mat();
8483
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), refImageID);
8584
init(obj, bitmap);
8685
} catch (Exception ex) {
@@ -98,6 +97,7 @@ private Bitmap convertByteArrayToBitmap(final byte[] src) {
9897

9998
private void init(JSONObject obj, Bitmap bitmap) throws JSONException {
10099
// Load the template image
100+
refImg = new Mat();
101101
Utils.bitmapToMat(bitmap, refImg);
102102

103103
if(refImg.height() > refImg.width()) {
@@ -122,10 +122,10 @@ private void init(JSONObject obj, Bitmap bitmap) throws JSONException {
122122
// Pull data related to the result window
123123
topLinePosition = rotated ? obj.getJSONArray("TOP_LINE_POSITION").getDouble(1) - resultWindowRect.x : obj.getJSONArray("TOP_LINE_POSITION").getDouble(0) - resultWindowRect.x;
124124
middleLinePosition = rotated ? obj.getJSONArray("MIDDLE_LINE_POSITION").getDouble(1) - resultWindowRect.x: obj.getJSONArray("MIDDLE_LINE_POSITION").getDouble(0) - resultWindowRect.x;
125-
bottomLinePosition = rotated ? obj.getJSONArray("BOTTOM_LINE_POSITION").getDouble(1) - resultWindowRect.x: obj.getJSONArray("BOTTOM_LINE_POSITION").getDouble(0) - resultWindowRect.x;
125+
bottomLinePosition = getBottomLinePosition(obj, rotated);
126126
topLineName = obj.getString("TOP_LINE_NAME");
127127
middleLineName = obj.getString("MIDDLE_LINE_NAME");
128-
bottomLineName = obj.getString("BOTTOM_LINE_NAME");
128+
bottomLineName = obj.optString("BOTTOM_LINE_NAME");
129129
lineIntensity = obj.getInt("LINE_INTENSITY");
130130
lineSearchWidth = obj.has("LINE_SEARCH_WIDTH") ? obj.getInt("LINE_SEARCH_WIDTH") :
131131
Math.max((int)((middleLinePosition-topLinePosition)/2.0),(int)((bottomLinePosition-middleLinePosition)/2.0));
@@ -174,4 +174,10 @@ private void init(JSONObject obj, Bitmap bitmap) throws JSONException {
174174
matcher = BFMatcher.create(BFMatcher.BRUTEFORCE, false);
175175
detector.detectAndCompute(refImg, new Mat(), refKeypoints, refDescriptor);
176176
}
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+
}
177183
}

0 commit comments

Comments
 (0)