forked from geneanet/customCamera
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraLauncher.java
More file actions
161 lines (137 loc) · 5.52 KB
/
Copy pathCameraLauncher.java
File metadata and controls
161 lines (137 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package org.geneanet.customcamera;
import XXX_NAME_CURRENT_PACKAGE_XXX.CameraActivity;
import android.content.Intent;
import android.util.Base64;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
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;
/**
* 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);
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;
}
TmpFileUtils.createTmpFile(this.cordova.getActivity(), CameraActivity.NAME_FILE_BACKGROUND, imgBackgroundBase64);
imgBackgroundBase64 = null;
}
if (args.getString(1) != "null") {
byte[] imgBackgroundBase64OtherOrientation;
try {
imgBackgroundBase64OtherOrientation = Base64
.decode(args.getString(1), Base64.NO_WRAP);
} catch (IllegalArgumentException e) {
this.callbackContext.error(generateError(CameraLauncher.RESULT_ERROR,
"Error decode base64 picture."));
return false;
}
TmpFileUtils.createTmpFile(this.cordova.getActivity(), CameraActivity.NAME_FILE_BACKGROUND_OTHER, imgBackgroundBase64OtherOrientation);
imgBackgroundBase64OtherOrientation = null;
}
// If we don't have a background image, disable miniature and opacity options.
if (args.getString(0) == "null") {
intent.putExtra("miniature", false);
intent.putExtra("opacity", false);
} else {
intent.putExtra("miniature", args.getBoolean(2));
intent.putExtra("opacity", args.getBoolean(7));
}
intent.putExtra("saveInGallery", args.getBoolean(3));
intent.putExtra("cameraBackgroundColor", args.getString(4));
intent.putExtra("cameraBackgroundColorPressed", args.getString(5));
if (args.getInt(6) >= 0 && args.getInt(6) <= 100) {
intent.putExtra("quality", args.getInt(6));
}
intent.putExtra("startOrientation", this.cordova.getActivity().getResources().getConfiguration().orientation);
intent.putExtra("defaultFlash", args.getInt(8));
intent.putExtra("switchFlash", args.getBoolean(9));
intent.putExtra("defaultCamera", args.getInt(10));
intent.putExtra("switchCamera", args.getBoolean(11));
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) {
// remove temporary files.
this.cordova.getActivity().deleteFile(CameraActivity.NAME_FILE_BACKGROUND);
this.cordova.getActivity().deleteFile(CameraActivity.NAME_FILE_BACKGROUND_OTHER);
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(TmpFileUtils.getTmpFileContent(this.cordova.getActivity(), CameraActivity.NAME_FILE_PICTURE_TAKEN),
Base64.NO_WRAP);
this.cordova.getActivity().deleteFile(CameraActivity.NAME_FILE_PICTURE_TAKEN);
String jsOut = new String(output);
output = null;
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;
}
}