我开发了一个用于光学字符识别的cordova插件。我将图像发送到android,图像处理部分由插件完成,处理后插件将JSON对象返回给Cordova。该插件第一次正常工作只有正确接收数据但当我第二次处理图像时,插件返回的数据是正确的,但在cordova端,Json对象没有更新,它显示以前的数据。 / p>
try {
if (ACTION.equals(action))
{
Log.d("action name", action);
Log.d("json object", args.getString(0));
//JSONObject arg_object = args.getJSONObject(0);
this.callbackContext = callbackContext;
if(ACTION_CAMERA.equals(args.getString(0))){
Log.d("action name", "camera");
cordova.getThreadPool().execute(new Runnable() {
public void run() {
camera();
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, json)); // Thread-safe.
}
});
}
/*
* sending the JSON object containing OCR results and BASE64 encoded string to javaScript
*/
//callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, json));
result = true;
Log.d("after results", "after results");
}
else{
callbackContext.error("Invalid action");
result = false;
}
return result;
}
catch(Exception e)
{
System.err.println("Exception: " + e.getMessage());
callbackContext.error(e.getMessage());
return false;
}
}
我的cordova代码是
function onPhotoURISuccess(obj) {
alert(obj['all']);
// clear image div contents
$("#base64Image").empty();
$("#base64Image").attr('src', '');
var image = new Image();
image.src = 'data:image/png;base64,' + obj['imageBase64'];
$("#base64Image").append(image);
$('#base64Image').width();
}
// A button will call this function
function capturePhoto() {
var success = function(message) {
onPhotoURISuccess(message);
}
var failure = function() {
alert("Error calling OCR 1 Plugin");
}
OCRPlugin.greet("camera", success, failure);
}