Catching JSON exceptions and raising an AlprException if anything goes wrong in the Java binding

This commit is contained in:
Matt Hill
2015-12-15 21:40:17 -05:00
parent aaf2cf534f
commit 9e4ebd4425
7 changed files with 37 additions and 11 deletions

View File

@@ -1,5 +1,7 @@
package com.openalpr.jni; package com.openalpr.jni;
import com.openalpr.jni.json.JSONException;
public class Alpr { public class Alpr {
static { static {
// Load the OpenALPR library at runtime // Load the OpenALPR library at runtime
@@ -36,17 +38,27 @@ public class Alpr {
return is_loaded(); return is_loaded();
} }
public AlprResults recognize(String imageFile) public AlprResults recognize(String imageFile) throws AlprException
{ {
try {
String json = native_recognize(imageFile); String json = native_recognize(imageFile);
return new AlprResults(json); return new AlprResults(json);
} catch (JSONException e)
{
throw new AlprException("Unable to parse ALPR results");
}
} }
public AlprResults recognize(byte[] imageBytes) public AlprResults recognize(byte[] imageBytes) throws AlprException
{ {
try {
String json = native_recognize(imageBytes); String json = native_recognize(imageBytes);
return new AlprResults(json); return new AlprResults(json);
} catch (JSONException e)
{
throw new AlprException("Unable to parse ALPR results");
}
} }

View File

@@ -1,13 +1,14 @@
package com.openalpr.jni; package com.openalpr.jni;
import com.openalpr.jni.json.JSONException;
import com.openalpr.jni.json.JSONObject; import com.openalpr.jni.json.JSONObject;
public class AlprCoordinate { public class AlprCoordinate {
private final int x; private final int x;
private final int y; private final int y;
AlprCoordinate(JSONObject coordinateObj) AlprCoordinate(JSONObject coordinateObj) throws JSONException
{ {
x = coordinateObj.getInt("x"); x = coordinateObj.getInt("x");
y = coordinateObj.getInt("y"); y = coordinateObj.getInt("y");

View File

@@ -0,0 +1,9 @@
package com.openalpr.jni;
public class AlprException extends Exception {
public AlprException(String s) {
super(s);
}
}

View File

@@ -1,6 +1,7 @@
package com.openalpr.jni; package com.openalpr.jni;
import com.openalpr.jni.json.JSONException;
import com.openalpr.jni.json.JSONObject; import com.openalpr.jni.json.JSONObject;
public class AlprPlate { public class AlprPlate {
@@ -8,7 +9,7 @@ public class AlprPlate {
private final float overall_confidence; private final float overall_confidence;
private final boolean matches_template; private final boolean matches_template;
AlprPlate(JSONObject plateObj) AlprPlate(JSONObject plateObj) throws JSONException
{ {
characters = plateObj.getString("plate"); characters = plateObj.getString("plate");
overall_confidence = (float) plateObj.getDouble("confidence"); overall_confidence = (float) plateObj.getDouble("confidence");

View File

@@ -2,6 +2,7 @@ package com.openalpr.jni;
import com.openalpr.jni.json.JSONArray; import com.openalpr.jni.json.JSONArray;
import com.openalpr.jni.json.JSONException;
import com.openalpr.jni.json.JSONObject; import com.openalpr.jni.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
@@ -30,7 +31,7 @@ public class AlprPlateResult {
private final int regionConfidence; private final int regionConfidence;
private final String region; private final String region;
AlprPlateResult(JSONObject plateResult) AlprPlateResult(JSONObject plateResult) throws JSONException
{ {
requested_topn = plateResult.getInt("requested_topn"); requested_topn = plateResult.getInt("requested_topn");

View File

@@ -1,5 +1,6 @@
package com.openalpr.jni; package com.openalpr.jni;
import com.openalpr.jni.json.JSONException;
import com.openalpr.jni.json.JSONObject; import com.openalpr.jni.json.JSONObject;
@@ -9,7 +10,7 @@ public class AlprRegionOfInterest {
private final int width; private final int width;
private final int height; private final int height;
AlprRegionOfInterest(JSONObject roiObj) AlprRegionOfInterest(JSONObject roiObj) throws JSONException
{ {
x = roiObj.getInt("x"); x = roiObj.getInt("x");
y = roiObj.getInt("y"); y = roiObj.getInt("y");

View File

@@ -1,6 +1,7 @@
package com.openalpr.jni; package com.openalpr.jni;
import com.openalpr.jni.json.JSONArray; import com.openalpr.jni.json.JSONArray;
import com.openalpr.jni.json.JSONException;
import com.openalpr.jni.json.JSONObject; import com.openalpr.jni.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
@@ -16,7 +17,7 @@ public class AlprResults {
private List<AlprRegionOfInterest> regionsOfInterest; private List<AlprRegionOfInterest> regionsOfInterest;
AlprResults(String json) AlprResults(String json) throws JSONException
{ {
JSONObject jobj = new JSONObject(json); JSONObject jobj = new JSONObject(json);