Merge branch 'master' of github.com:phonegap/phonegap-android

This commit is contained in:
brianleroux 2010-09-02 16:42:12 -07:00
commit d2e19d8818
3 changed files with 163 additions and 83 deletions

View File

@ -1,12 +1,16 @@
/** /**
* This class provides access to the device camera. * This class provides access to the device camera.
*
* @constructor * @constructor
*/ */
function Camera() { function Camera() {
this.successCallback = null;
} this.errorCallback = null;
this.options = null;
};
/** /**
* Takes a photo and returns the image as a base64 encoded `String`.
* *
* @param {Function} successCallback * @param {Function} successCallback
* @param {Function} errorCallback * @param {Function} errorCallback
@ -14,27 +18,52 @@ function Camera() {
*/ */
Camera.prototype.getPicture = function(successCallback, errorCallback, options) { Camera.prototype.getPicture = function(successCallback, errorCallback, options) {
this.winCallback = successCallback; // successCallback required
this.failCallback = errorCallback; if (typeof successCallback != "function") {
if (options.quality) console.log("Camera Error: successCallback is not a function");
{ return;
GapCam.takePicture(options.quality); }
}
else
{
GapCam.takePicture(80);
}
}
Camera.prototype.win = function(picture) // errorCallback optional
{ if (errorCallback && (typeof errorCallback != "function")) {
this.winCallback(picture); console.log("Camera Error: errorCallback is not a function");
} return;
}
Camera.prototype.fail = function(err) this.successCallback = successCallback;
{ this.errorCallback = errorCallback;
this.failCallback(err); this.options = options;
} if (options.quality) {
GapCam.takePicture(options.quality);
}
else {
GapCam.takePicture(80);
}
};
/**
* Callback function from native code that is called when image has been captured.
*
* @param picture The base64 encoded string of the image
*/
Camera.prototype.success = function(picture) {
if (this.successCallback) {
this.successCallback(picture);
}
};
/**
* Callback function from native code that is called when there is an error
* capturing an image, or the capture is cancelled.
*
* @param err The error message
*/
Camera.prototype.error = function(err) {
if (this.errorCallback) {
this.errorCallback(err);
}
};
PhoneGap.addConstructor(function() { PhoneGap.addConstructor(function() {
if (typeof navigator.camera == "undefined") navigator.camera = new Camera(); if (typeof navigator.camera == "undefined") navigator.camera = new Camera();

View File

@ -1,55 +1,122 @@
package com.phonegap; package com.phonegap;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat; import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.webkit.WebView; import android.webkit.WebView;
/**
* 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.
*/
public class CameraLauncher { public class CameraLauncher {
private WebView mAppView; private WebView mAppView; // Webview object
private DroidGap mGap; private DroidGap mGap; // DroidGap object
int mQuality; 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
CameraLauncher(WebView view, DroidGap gap) /**
{ * Constructor.
*
* @param view
* @param gap
*/
CameraLauncher(WebView view, DroidGap gap) {
mAppView = view; mAppView = view;
mGap = gap; mGap = gap;
} }
public void takePicture(int quality) /**
{ * Take a picture with the camera.
mQuality = quality; * When an image is captured or the camera view is cancelled, the result is returned
mGap.startCamera(); * 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");
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
this.imageUri = Uri.fromFile(photo);
mGap.startActivityForResult(intent, DroidGap.CAMERA_ACTIVIY_RESULT);
} }
/* Return Base64 Encoded String to Javascript */ /**
public void processPicture( Bitmap bitmap ) * 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().
* @param data An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
// If image available
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = this.imageUri;
mGap.getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = mGap.getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
this.processPicture(bitmap);
} catch (Exception e) {
this.failPicture("Error capturing image.");
}
}
// If cancelled
else if (resultCode == Activity.RESULT_CANCELED) {
this.failPicture("Camera cancelled.");
}
// If something else
else {
this.failPicture("Did not complete!");
}
}
/**
* Compress bitmap using jpeg, convert to Base64 encoded string, and return to JavaScript.
*
* @param bitmap
*/
public void processPicture(Bitmap bitmap) {
ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream(); ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
try { try {
if (bitmap.compress(CompressFormat.JPEG, mQuality, jpeg_data)) if (bitmap.compress(CompressFormat.JPEG, mQuality, jpeg_data)) {
{
byte[] code = jpeg_data.toByteArray(); byte[] code = jpeg_data.toByteArray();
byte[] output = Base64.encodeBase64(code); byte[] output = Base64.encodeBase64(code);
String js_out = new String(output); String js_out = new String(output);
mGap.sendJavascript("navigator.camera.win('" + js_out + "');"); mGap.sendJavascript("navigator.camera.success('" + js_out + "');");
} }
} }
catch(Exception e) catch(Exception e) {
{ this.failPicture("Error compressing image.");
failPicture("fail");
} }
} }
public void failPicture(String err) /**
{ * Send error message to JavaScript.
mGap.sendJavascript("navigator.camera.fail('" + err + "');"); *
* @param err
*/
public void failPicture(String err) {
mGap.sendJavascript("navigator.camera.error('" + err + "');");
} }
} }

View File

@ -23,7 +23,6 @@
*/ */
import java.io.File;
import com.phonegap.api.Command; import com.phonegap.api.Command;
import com.phonegap.api.CommandManager; import com.phonegap.api.CommandManager;
@ -57,7 +56,6 @@ import android.webkit.WebSettings.LayoutAlgorithm;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.os.Build.*; import android.os.Build.*;
import android.provider.MediaStore;
/** /**
* This class is the main Android activity that represents the PhoneGap * This class is the main Android activity that represents the PhoneGap
@ -82,7 +80,9 @@ import android.provider.MediaStore;
public class DroidGap extends Activity { public class DroidGap extends Activity {
private static final String LOG_TAG = "DroidGap"; private static final String LOG_TAG = "DroidGap";
protected WebView appView; public static final int CAMERA_ACTIVIY_RESULT = 1;
protected WebView appView; // The webview for our app
protected ImageView splashScreen; protected ImageView splashScreen;
protected Boolean loadInWebView = false; protected Boolean loadInWebView = false;
private LinearLayout root; private LinearLayout root;
@ -102,7 +102,6 @@ public class DroidGap extends Activity {
private CallbackServer callbackServer; private CallbackServer callbackServer;
private CommandManager commandManager; private CommandManager commandManager;
private Uri imageUri;
private String url; // The initial URL for our app private String url; // The initial URL for our app
private String baseUrl; // The base of the initial URL for our app private String baseUrl; // The base of the initial URL for our app
@ -241,7 +240,6 @@ public class DroidGap extends Activity {
accel.destroy(); accel.destroy();
} }
if (launcher != null) { if (launcher != null) {
} }
if (mContacts != null) { if (mContacts != null) {
@ -611,36 +609,22 @@ public class DroidGap extends Activity {
root.addView(appView); root.addView(appView);
} }
// This is required to start the camera activity! It has to come from the previous activity @Override
public void startCamera() /**
{ * Called when an activity you launched exits, giving you the requestCode you started it with,
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); * the resultCode it returned, and any additional data from it.
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg"); *
intent.putExtra(MediaStore.EXTRA_OUTPUT, * @param requestCode The request code originally supplied to startActivityForResult(),
Uri.fromFile(photo)); * allowing you to identify who this result came from.
imageUri = Uri.fromFile(photo); * @param resultCode The integer result code returned by the child activity through its setResult().
startActivityForResult(intent, 0); * @param data An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
} */
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
protected void onActivityResult(int requestCode, int resultCode, Intent intent) // If camera result
{ if (requestCode == DroidGap.CAMERA_ACTIVIY_RESULT) {
super.onActivityResult(requestCode, resultCode, intent); launcher.onActivityResult(requestCode, resultCode, intent);
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
launcher.processPicture(bitmap);
} catch (Exception e) {
launcher.failPicture("Did not complete!");
}
}
else
{
launcher.failPicture("Did not complete!");
} }
} }
} }