mirror of
https://gitee.com/shuto/customCamera.git
synced 2026-05-02 00:07:24 +08:00
Passage au checkstyle de CameraLauncher.
This commit is contained in:
+106
-111
@@ -1,8 +1,12 @@
|
||||
package org.geneanet.customcamera;
|
||||
|
||||
import XXX_NAME_CURRENT_PACKAGE_XXX.CameraActivity;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.util.Base64;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.PluginResult;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
@@ -15,124 +19,115 @@ import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.util.Base64;
|
||||
|
||||
public class CameraLauncher extends CordovaPlugin {
|
||||
|
||||
protected CallbackContext callbackContext;
|
||||
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 RESULT_SUCCESS = 1;
|
||||
protected static final int RESULT_ERROR = 2;
|
||||
protected static final int RESULT_BACK = 3;
|
||||
|
||||
protected static final int REQUEST_CODE = 88224646;
|
||||
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;
|
||||
/**
|
||||
* Execute the plugin.
|
||||
*
|
||||
* @param action Action executed.
|
||||
* @param args Parameters for the plugin.
|
||||
* @param callbackContext Context of callback.
|
||||
* @return boolean
|
||||
* @throws JSONException Exception JSON if error in generateError.
|
||||
*/
|
||||
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(), CameraActivity.class);
|
||||
Intent intent = new Intent(this.cordova.getActivity(),
|
||||
CameraActivity.class);
|
||||
|
||||
if (args.getString(0) != "null") {
|
||||
byte[] imgBackgroundBase64;
|
||||
try {
|
||||
imgBackgroundBase64 = Base64.decode(args.getString(0), Base64.NO_WRAP);
|
||||
} catch (IllegalArgumentException e) {
|
||||
this.callbackContext.error(
|
||||
generateError(
|
||||
CameraLauncher.RESULT_ERROR,
|
||||
"Error decode base64 picture."
|
||||
)
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
TransferBigData.setImgBackgroundBase64(imgBackgroundBase64);
|
||||
}
|
||||
|
||||
intent.putExtra("miniature", args.getBoolean(1));
|
||||
intent.putExtra("saveInGallery", args.getBoolean(2));
|
||||
intent.putExtra("cameraBackgroundColor", args.getString(3));
|
||||
intent.putExtra("cameraBackgroundColorPressed", args.getString(4));
|
||||
if (args.getInt(5) >= 0 && args.getInt(5) <= 100) {
|
||||
intent.putExtra("quality", args.getInt(5));
|
||||
}
|
||||
|
||||
cordova.startActivityForResult((CordovaPlugin) this, intent, CameraLauncher.REQUEST_CODE);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
||||
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:
|
||||
try {
|
||||
byte[] output = Base64.encode(TransferBigData.getImgTaken(), 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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] loadFile(File file) throws IOException {
|
||||
InputStream is = new FileInputStream(file);
|
||||
|
||||
long length = file.length();
|
||||
byte[] bytes = new byte[(int)length];
|
||||
|
||||
int offset = 0;
|
||||
int numRead = 0;
|
||||
while (offset < bytes.length
|
||||
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
|
||||
offset += numRead;
|
||||
}
|
||||
|
||||
if (offset < bytes.length) {
|
||||
throw new IOException("Could not completely read file "+file.getName());
|
||||
}
|
||||
|
||||
is.close();
|
||||
return bytes;
|
||||
}
|
||||
|
||||
protected JSONObject generateError(int code, String message) {
|
||||
JSONObject resultForPlugin = new JSONObject();
|
||||
if (args.getString(0) != "null") {
|
||||
byte[] imgBackgroundBase64;
|
||||
try {
|
||||
resultForPlugin.put("code", code);
|
||||
resultForPlugin.put("message", message);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
imgBackgroundBase64 = Base64
|
||||
.decode(args.getString(0), Base64.NO_WRAP);
|
||||
} catch (IllegalArgumentException e) {
|
||||
this.callbackContext.error(generateError(CameraLauncher.RESULT_ERROR,
|
||||
"Error decode base64 picture."));
|
||||
|
||||
return resultForPlugin;
|
||||
return false;
|
||||
}
|
||||
TransferBigData.setImgBackgroundBase64(imgBackgroundBase64);
|
||||
}
|
||||
|
||||
intent.putExtra("miniature", args.getBoolean(1));
|
||||
intent.putExtra("saveInGallery", args.getBoolean(2));
|
||||
intent.putExtra("cameraBackgroundColor", args.getString(3));
|
||||
intent.putExtra("cameraBackgroundColorPressed", args.getString(4));
|
||||
if (args.getInt(5) >= 0 && args.getInt(5) <= 100) {
|
||||
intent.putExtra("quality", args.getInt(5));
|
||||
}
|
||||
|
||||
cordova.startActivityForResult((CordovaPlugin) this, intent,
|
||||
CameraLauncher.REQUEST_CODE);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call when the CameraActivity is over.
|
||||
* @param requestCode Code used for the activity luncher.
|
||||
* @param resultCode Return code.
|
||||
* @param intent Contains parameters of the activity.
|
||||
*/
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
||||
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:
|
||||
try {
|
||||
byte[] output = Base64.encode(TransferBigData.getImgTaken(),
|
||||
Base64.NO_WRAP);
|
||||
String jsOut = new String(output);
|
||||
|
||||
this.callbackContext.success(jsOut);
|
||||
} catch (Exception e) {
|
||||
this.callbackContext.error("Error to get content file.");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
this.callbackContext.error("Camera has crashed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate error in the plugin.
|
||||
*
|
||||
* @param code Error code.
|
||||
* @param message Error message.
|
||||
*
|
||||
* @return a JSON Object.
|
||||
*/
|
||||
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