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-06-05 05:54:16 +08:00
|
|
|
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
|
|
2010-09-03 00:27:48 +08:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
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;
|
|
|
|
import android.provider.MediaStore;
|
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.
|
|
|
|
*/
|
2009-07-18 05:06:49 +08:00
|
|
|
public class CameraLauncher {
|
2009-07-31 07:56:07 +08:00
|
|
|
|
2010-09-03 00:27:48 +08:00
|
|
|
private WebView mAppView; // Webview object
|
|
|
|
private DroidGap mGap; // DroidGap object
|
|
|
|
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
|
2009-07-18 05:06:49 +08:00
|
|
|
|
2010-09-03 00:27:48 +08:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param view
|
|
|
|
* @param gap
|
|
|
|
*/
|
|
|
|
CameraLauncher(WebView view, DroidGap gap) {
|
2009-07-18 05:06:49 +08:00
|
|
|
mAppView = view;
|
2009-07-31 07:56:07 +08:00
|
|
|
mGap = gap;
|
|
|
|
}
|
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");
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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!");
|
|
|
|
}
|
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-03 00:27:48 +08:00
|
|
|
mGap.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) {
|
|
|
|
mGap.sendJavascript("navigator.camera.error('" + err + "');");
|
2009-07-18 05:06:49 +08:00
|
|
|
}
|
|
|
|
}
|