mirror of
https://github.com/kerberos-io/openalpr-base.git
synced 2025-10-06 13:46:52 +08:00
Catching JSON exceptions and raising an AlprException if anything goes wrong in the Java binding
This commit is contained in:
@@ -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
|
||||||
{
|
{
|
||||||
String json = native_recognize(imageFile);
|
try {
|
||||||
return new AlprResults(json);
|
String json = native_recognize(imageFile);
|
||||||
|
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
|
||||||
{
|
{
|
||||||
String json = native_recognize(imageBytes);
|
try {
|
||||||
return new AlprResults(json);
|
String json = native_recognize(imageBytes);
|
||||||
|
return new AlprResults(json);
|
||||||
|
} catch (JSONException e)
|
||||||
|
{
|
||||||
|
throw new AlprException("Unable to parse ALPR results");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -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");
|
||||||
|
@@ -0,0 +1,9 @@
|
|||||||
|
package com.openalpr.jni;
|
||||||
|
|
||||||
|
|
||||||
|
public class AlprException extends Exception {
|
||||||
|
|
||||||
|
public AlprException(String s) {
|
||||||
|
super(s);
|
||||||
|
}
|
||||||
|
}
|
@@ -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");
|
||||||
|
@@ -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");
|
||||||
|
|
||||||
|
@@ -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");
|
||||||
|
@@ -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);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user