9
0
mirror of https://gitee.com/shuto/customCamera.git synced 2026-05-02 00:07:24 +08:00

Ajout du passage en base64 de l'image au js.

This commit is contained in:
Christophe BOUCAUT
2014-11-20 17:18:52 +01:00
parent d47681e20e
commit 383b8feaaf
3 changed files with 72 additions and 17 deletions
+53 -4
View File
@@ -7,26 +7,75 @@ import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
public class CameraLauncher extends CordovaPlugin {
protected CallbackContext callbackContext;
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);
Bundle imgBackgroundBase64 = new Bundle();
imgBackgroundBase64.putString("imgBackgroundBase64", args.getString(0));
intent.putExtras(imgBackgroundBase64);
cordova.getActivity().startActivity(intent);
PluginResult r = new PluginResult(PluginResult.Status.OK, "base64retour");
callbackContext.sendPluginResult(r);
cordova.startActivityForResult((CordovaPlugin) this, intent, 123456789);
return true;
}
return false;
}
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;
}
}