2009-11-10 09:45:02 +08:00
|
|
|
package com.phonegap;
|
2009-07-18 05:06:49 +08:00
|
|
|
|
2010-06-05 05:54:16 +08:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2010-09-03 00:27:48 +08:00
|
|
|
import java.io.File;
|
2010-09-04 03:01:24 +08:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
2010-06-05 05:54:16 +08:00
|
|
|
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
2010-09-07 02:13:09 +08:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
2010-09-08 02:59:54 +08:00
|
|
|
import com.phonegap.api.Plugin;
|
|
|
|
import com.phonegap.api.PluginManager;
|
|
|
|
import com.phonegap.api.PluginResult;
|
2010-06-05 05:54:16 +08:00
|
|
|
|
2010-09-03 00:27:48 +08:00
|
|
|
import android.app.Activity;
|
2010-09-04 03:01:24 +08:00
|
|
|
import android.content.ContentValues;
|
2010-09-07 02:13:09 +08:00
|
|
|
import android.content.Context;
|
2010-09-03 00:27:48 +08:00
|
|
|
import android.content.Intent;
|
2010-06-05 05:54:16 +08:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.Bitmap.CompressFormat;
|
2010-09-03 00:27:48 +08:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Environment;
|
2009-07-31 07:56:07 +08:00
|
|
|
import android.webkit.WebView;
|
2009-08-01 04:52:45 +08:00
|
|
|
|
2010-09-03 00:27:48 +08:00
|
|
|
/**
|
|
|
|
* This class launches the camera view, allows the user to take a picture, closes the camera view,
|
|
|
|
* and returns the captured image. When the camera view is closed, the screen displayed before
|
|
|
|
* the camera view was shown is redisplayed.
|
|
|
|
*/
|
2010-09-08 02:59:54 +08:00
|
|
|
public class CameraLauncher implements Plugin {
|
2010-09-07 02:13:09 +08:00
|
|
|
|
|
|
|
WebView webView; // WebView object
|
|
|
|
DroidGap ctx; // DroidGap object
|
|
|
|
|
2010-09-03 00:27:48 +08:00
|
|
|
private int mQuality; // Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality)
|
|
|
|
private Uri imageUri; // Uri of captured image
|
2010-09-04 05:00:21 +08:00
|
|
|
private boolean base64 = true;
|
2009-07-18 05:06:49 +08:00
|
|
|
|
2010-09-03 00:27:48 +08:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
2010-09-07 02:13:09 +08:00
|
|
|
public CameraLauncher() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the context of the Command. This can then be used to do things like
|
|
|
|
* get file paths associated with the Activity.
|
|
|
|
*
|
|
|
|
* @param ctx The context of the main Activity.
|
|
|
|
*/
|
|
|
|
public void setContext(DroidGap ctx) {
|
|
|
|
this.ctx = ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the main View of the application, this is the WebView within which
|
|
|
|
* a PhoneGap app runs.
|
|
|
|
*
|
|
|
|
* @param webView The PhoneGap WebView
|
|
|
|
*/
|
|
|
|
public void setView(WebView webView) {
|
|
|
|
this.webView = webView;
|
2009-07-31 07:56:07 +08:00
|
|
|
}
|
2010-09-07 02:13:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the request and returns CommandResult.
|
|
|
|
*
|
|
|
|
* @param action The command to execute.
|
|
|
|
* @param args JSONArry of arguments for the command.
|
|
|
|
* @return A CommandResult object with a status and message.
|
|
|
|
*/
|
2010-09-08 02:59:54 +08:00
|
|
|
public PluginResult execute(String action, JSONArray args) {
|
|
|
|
PluginResult.Status status = PluginResult.Status.OK;
|
2010-09-07 02:13:09 +08:00
|
|
|
String result = "";
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (action.equals("setBase64")) {
|
|
|
|
this.setBase64(args.getBoolean(0));
|
|
|
|
}
|
|
|
|
else if (action.equals("takePicture")) {
|
|
|
|
this.takePicture(args.getInt(0));
|
|
|
|
}
|
2010-09-08 02:59:54 +08:00
|
|
|
return new PluginResult(status, result);
|
2010-09-07 02:13:09 +08:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2010-09-08 02:59:54 +08:00
|
|
|
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
|
2010-09-07 02:13:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-11 00:34:06 +08:00
|
|
|
/**
|
|
|
|
* Identifies if action to be executed returns a value.
|
|
|
|
*
|
|
|
|
* @param action The action to execute
|
|
|
|
* @return T=returns value
|
|
|
|
*/
|
|
|
|
public boolean hasReturnValue(String action) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-07 02:13:09 +08:00
|
|
|
/**
|
|
|
|
* Called when the system is about to start resuming a previous activity.
|
|
|
|
*/
|
|
|
|
public void onPause() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the activity will start interacting with the user.
|
|
|
|
*/
|
|
|
|
public void onResume() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by AccelBroker when listener is to be shut down.
|
|
|
|
* Stop listener.
|
|
|
|
*/
|
|
|
|
public void onDestroy() {
|
|
|
|
}
|
2010-09-04 05:00:21 +08:00
|
|
|
|
2010-09-07 02:13:09 +08:00
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
// LOCAL METHODS
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
2010-09-04 05:00:21 +08:00
|
|
|
/**
|
|
|
|
* Set the type of data to return. The data can either be returned
|
|
|
|
* as a base64 string or a URI that points to the file.
|
|
|
|
* To display base64 string in an img tag, set the source to:
|
|
|
|
* img.src="data:image/jpeg;base64,"+result;
|
|
|
|
* or to display URI in an img tag
|
|
|
|
* img.src=result;
|
|
|
|
*
|
|
|
|
* @param b T=return base64 string (default), F=return URI
|
|
|
|
*/
|
|
|
|
public void setBase64(boolean b) {
|
|
|
|
this.base64 = b;
|
|
|
|
}
|
2010-09-03 00:27:48 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Take a picture with the camera.
|
|
|
|
* When an image is captured or the camera view is cancelled, the result is returned
|
|
|
|
* in DroidGap.onActivityResult, which forwards the result to this.onActivityResult.
|
|
|
|
*
|
|
|
|
* @param quality Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality)
|
|
|
|
*/
|
|
|
|
public void takePicture(int quality) {
|
|
|
|
this.mQuality = quality;
|
|
|
|
|
|
|
|
// Display camera
|
|
|
|
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
|
2010-09-04 03:01:24 +08:00
|
|
|
|
|
|
|
// Specify file so that large image is captured and returned
|
|
|
|
// TODO: What if there isn't any external storage?
|
2010-09-03 00:27:48 +08:00
|
|
|
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
|
2010-09-04 03:01:24 +08:00
|
|
|
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
|
2010-09-03 00:27:48 +08:00
|
|
|
this.imageUri = Uri.fromFile(photo);
|
2010-09-04 03:01:24 +08:00
|
|
|
|
2010-09-08 02:59:54 +08:00
|
|
|
this.ctx.startActivityForResult((Plugin) this, intent);
|
2010-09-03 00:27:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the camera view exits.
|
|
|
|
*
|
|
|
|
* @param requestCode The request code originally supplied to startActivityForResult(),
|
|
|
|
* allowing you to identify who this result came from.
|
|
|
|
* @param resultCode The integer result code returned by the child activity through its setResult().
|
2010-09-08 02:59:54 +08:00
|
|
|
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
|
2010-09-03 00:27:48 +08:00
|
|
|
*/
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
|
|
|
|
|
|
|
// If image available
|
|
|
|
if (resultCode == Activity.RESULT_OK) {
|
2010-09-04 03:01:24 +08:00
|
|
|
try {
|
|
|
|
// Read in bitmap of captured image
|
2010-09-07 02:13:09 +08:00
|
|
|
Bitmap bitmap = android.provider.MediaStore.Images.Media.getBitmap(this.ctx.getContentResolver(), imageUri);
|
2010-09-04 03:01:24 +08:00
|
|
|
|
|
|
|
// If sending base64 image back
|
|
|
|
if (this.base64) {
|
|
|
|
this.processPicture(bitmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If sending filename back
|
|
|
|
else {
|
|
|
|
// Create entry in media store for image
|
|
|
|
// (Don't use insertImage() because it uses default compression setting of 50 - no way to change it)
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
|
|
|
|
Uri uri = null;
|
|
|
|
try {
|
2010-09-07 02:13:09 +08:00
|
|
|
uri = this.ctx.getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
|
2010-09-04 03:01:24 +08:00
|
|
|
} catch (UnsupportedOperationException e) {
|
|
|
|
System.out.println("Can't write to external media storage.");
|
|
|
|
try {
|
2010-09-07 02:13:09 +08:00
|
|
|
uri = this.ctx.getContentResolver().insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
|
2010-09-04 03:01:24 +08:00
|
|
|
} catch (UnsupportedOperationException ex) {
|
|
|
|
System.out.println("Can't write to internal media storage.");
|
|
|
|
this.failPicture("Error capturing image - no media storage found.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add compressed version of captured image to returned media store Uri
|
2010-09-07 02:13:09 +08:00
|
|
|
OutputStream os = this.ctx.getContentResolver().openOutputStream(uri);
|
2010-09-04 03:01:24 +08:00
|
|
|
bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
|
|
|
|
os.close();
|
|
|
|
|
|
|
|
// Send Uri back to JavaScript for viewing image
|
2010-09-07 02:13:09 +08:00
|
|
|
this.ctx.sendJavascript("navigator.camera.success('" + uri.toString() + "');");
|
2010-09-04 03:01:24 +08:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2010-09-03 00:27:48 +08:00
|
|
|
this.failPicture("Error capturing image.");
|
2010-09-04 03:01:24 +08:00
|
|
|
}
|
2010-09-03 00:27:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If cancelled
|
|
|
|
else if (resultCode == Activity.RESULT_CANCELED) {
|
|
|
|
this.failPicture("Camera cancelled.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// If something else
|
|
|
|
else {
|
|
|
|
this.failPicture("Did not complete!");
|
|
|
|
}
|
2009-07-31 07:56:07 +08:00
|
|
|
}
|
|
|
|
|
2010-09-03 00:27:48 +08:00
|
|
|
/**
|
|
|
|
* Compress bitmap using jpeg, convert to Base64 encoded string, and return to JavaScript.
|
|
|
|
*
|
|
|
|
* @param bitmap
|
|
|
|
*/
|
|
|
|
public void processPicture(Bitmap bitmap) {
|
2010-06-05 05:54:16 +08:00
|
|
|
ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
|
|
|
|
try {
|
2010-09-03 00:27:48 +08:00
|
|
|
if (bitmap.compress(CompressFormat.JPEG, mQuality, jpeg_data)) {
|
2010-06-05 05:54:16 +08:00
|
|
|
byte[] code = jpeg_data.toByteArray();
|
|
|
|
byte[] output = Base64.encodeBase64(code);
|
|
|
|
String js_out = new String(output);
|
2010-09-07 02:13:09 +08:00
|
|
|
this.ctx.sendJavascript("navigator.camera.success('" + js_out + "');");
|
2010-06-05 05:54:16 +08:00
|
|
|
}
|
|
|
|
}
|
2010-09-03 00:27:48 +08:00
|
|
|
catch(Exception e) {
|
|
|
|
this.failPicture("Error compressing image.");
|
|
|
|
}
|
2009-08-01 04:52:45 +08:00
|
|
|
}
|
|
|
|
|
2010-09-03 00:27:48 +08:00
|
|
|
/**
|
|
|
|
* Send error message to JavaScript.
|
|
|
|
*
|
|
|
|
* @param err
|
|
|
|
*/
|
|
|
|
public void failPicture(String err) {
|
2010-09-07 02:13:09 +08:00
|
|
|
this.ctx.sendJavascript("navigator.camera.error('" + err + "');");
|
2009-07-18 05:06:49 +08:00
|
|
|
}
|
|
|
|
}
|