mirror of
https://gitee.com/shuto/customCamera.git
synced 2026-05-02 00:07:24 +08:00
- Refacto & comments code.
- Normalisation error message for js. - Manage back button. - Managa unavailable camera.
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
package org.geneanet.customcamera;
|
||||
|
||||
import XXX_NAME_CURRENT_PACKAGE_XXX.CameraView;
|
||||
import XXX_NAME_CURRENT_PACKAGE_XXX.CameraActivity;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.PluginResult;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
@@ -22,17 +23,23 @@ public class CameraLauncher extends CordovaPlugin {
|
||||
|
||||
protected CallbackContext callbackContext;
|
||||
|
||||
protected static final int RESULT_SUCCESS = 1;
|
||||
protected static final int RESULT_ERROR = 2;
|
||||
protected static final int RESULT_BACK = 3;
|
||||
|
||||
protected static final int REQUEST_CODE = 88224646;
|
||||
|
||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||
if (action.equals("startCamera")) {
|
||||
this.callbackContext = callbackContext;
|
||||
|
||||
Intent intent = new Intent(this.cordova.getActivity(), CameraView.class);
|
||||
Intent intent = new Intent(this.cordova.getActivity(), CameraActivity.class);
|
||||
|
||||
Bundle imgBackgroundBase64 = new Bundle();
|
||||
imgBackgroundBase64.putString("imgBackgroundBase64", args.getString(0));
|
||||
intent.putExtras(imgBackgroundBase64);
|
||||
|
||||
cordova.startActivityForResult((CordovaPlugin) this, intent, 123456789);
|
||||
cordova.startActivityForResult((CordovaPlugin) this, intent, CameraLauncher.REQUEST_CODE);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -41,19 +48,41 @@ public class CameraLauncher extends CordovaPlugin {
|
||||
}
|
||||
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
||||
if (requestCode == 123456789 && resultCode == 1) {
|
||||
String pathPicture = intent.getStringExtra("pathPicture");
|
||||
// Log.d("customCamera", pathPicture);
|
||||
try {
|
||||
File fl = new File(pathPicture);
|
||||
byte[] ret = loadFile(fl);
|
||||
if (requestCode == CameraLauncher.REQUEST_CODE) {
|
||||
switch (resultCode)
|
||||
{
|
||||
case CameraLauncher.RESULT_ERROR:
|
||||
this.callbackContext.error(
|
||||
generateError(
|
||||
CameraLauncher.RESULT_ERROR,
|
||||
intent.getStringExtra("errorMessage")
|
||||
)
|
||||
);
|
||||
break;
|
||||
case CameraLauncher.RESULT_BACK:
|
||||
this.callbackContext.error(
|
||||
generateError(
|
||||
CameraLauncher.RESULT_BACK,
|
||||
"Error because back camera."
|
||||
)
|
||||
);
|
||||
break;
|
||||
case CameraLauncher.RESULT_SUCCESS:
|
||||
String pathPicture = intent.getStringExtra("pathPicture");
|
||||
try {
|
||||
File fl = new File(pathPicture);
|
||||
byte[] ret = loadFile(fl);
|
||||
|
||||
byte[] output = Base64.encode(ret, Base64.NO_WRAP);
|
||||
String js_out = new String(output);
|
||||
|
||||
this.callbackContext.success(js_out);
|
||||
} catch (Exception e) {
|
||||
this.callbackContext.error("Error to get content file.");
|
||||
byte[] output = Base64.encode(ret, Base64.NO_WRAP);
|
||||
String js_out = new String(output);
|
||||
|
||||
this.callbackContext.success(js_out);
|
||||
} catch (Exception e) {
|
||||
this.callbackContext.error("Error to get content file.");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
this.callbackContext.error("Camera has crashed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,4 +107,16 @@ public class CameraLauncher extends CordovaPlugin {
|
||||
is.close();
|
||||
return bytes;
|
||||
}
|
||||
|
||||
protected JSONObject generateError(int code, String message) {
|
||||
JSONObject resultForPlugin = new JSONObject();
|
||||
try {
|
||||
resultForPlugin.put("code", code);
|
||||
resultForPlugin.put("message", message);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return resultForPlugin;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user