2014-11-10 12:21:03 +01:00
|
|
|
package org.geneanet.customcamera;
|
|
|
|
|
|
2014-11-14 16:05:50 +01:00
|
|
|
import XXX_NAME_CURRENT_PACKAGE_XXX.CameraView;
|
2014-11-10 12:21:03 +01:00
|
|
|
import org.apache.cordova.CordovaPlugin;
|
|
|
|
|
import org.apache.cordova.CallbackContext;
|
2014-11-19 11:33:05 +01:00
|
|
|
import org.apache.cordova.PluginResult;
|
2014-11-10 12:21:03 +01:00
|
|
|
import org.json.JSONArray;
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
|
2014-11-20 17:18:52 +01:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
2014-11-10 12:21:03 +01:00
|
|
|
import android.content.Intent;
|
2014-11-18 17:03:11 +01:00
|
|
|
import android.os.Bundle;
|
2014-11-20 17:18:52 +01:00
|
|
|
import android.util.Base64;
|
2014-11-10 12:21:03 +01:00
|
|
|
|
|
|
|
|
public class CameraLauncher extends CordovaPlugin {
|
2014-11-20 17:18:52 +01:00
|
|
|
|
|
|
|
|
protected CallbackContext callbackContext;
|
|
|
|
|
|
2014-11-10 12:21:03 +01:00
|
|
|
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
2014-11-19 11:33:05 +01:00
|
|
|
if (action.equals("startCamera")) {
|
2014-11-20 17:18:52 +01:00
|
|
|
this.callbackContext = callbackContext;
|
|
|
|
|
|
2014-11-19 11:33:05 +01:00
|
|
|
Intent intent = new Intent(this.cordova.getActivity(), CameraView.class);
|
2014-11-18 17:03:11 +01:00
|
|
|
|
2014-11-19 11:33:05 +01:00
|
|
|
Bundle imgBackgroundBase64 = new Bundle();
|
|
|
|
|
imgBackgroundBase64.putString("imgBackgroundBase64", args.getString(0));
|
|
|
|
|
intent.putExtras(imgBackgroundBase64);
|
2014-11-18 17:03:11 +01:00
|
|
|
|
2014-11-20 17:18:52 +01:00
|
|
|
cordova.startActivityForResult((CordovaPlugin) this, intent, 123456789);
|
2014-11-19 11:33:05 +01:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2014-11-10 12:21:03 +01:00
|
|
|
}
|
2014-11-20 17:18:52 +01:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
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.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2014-11-10 12:21:03 +01:00
|
|
|
}
|