Changed to use the Camera Intents to our own peril.

This commit is contained in:
Joe Bowser 2010-06-04 14:54:16 -07:00
parent 653190c6a1
commit 8f18dae2c1
4 changed files with 54 additions and 241 deletions

View File

@ -32,11 +32,6 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.phonegap.CameraPreview"
android:label="@string/app_name" android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden">
<action android:name="android.intent.action.PICK" />
</activity>
</application>
<uses-sdk android:minSdkVersion="2" />

View File

@ -1,5 +1,11 @@
package com.phonegap;
import java.io.ByteArrayOutputStream;
import org.apache.commons.codec.binary.Base64;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.webkit.WebView;
@ -21,9 +27,23 @@ public class CameraLauncher {
}
/* Return Base64 Encoded String to Javascript */
public void processPicture( String js_out )
public void processPicture( Bitmap bitmap )
{
mAppView.loadUrl("javascript:navigator.camera.win('" + js_out + "');");
ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
try {
if (bitmap.compress(CompressFormat.JPEG, quality, jpeg_data))
{
byte[] code = jpeg_data.toByteArray();
byte[] output = Base64.encodeBase64(code);
String js_out = new String(output);
mAppView.loadUrl("javascript:navigator.camera.win('" + js_out + "');");
}
}
catch(Exception e)
{
failPicture("fail");
}
}
public void failPicture(String err)

View File

@ -1,218 +0,0 @@
package com.phonegap;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class CameraPreview extends Activity implements SurfaceHolder.Callback{
private static final String TAG = "PhoneGapCamera";
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private RelativeLayout root;
Camera mCamera;
boolean mPreviewRunning = false;
int quality;
Intent mIntent;
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
Log.e(TAG, "onCreate");
getWindow().setFormat(PixelFormat.TRANSLUCENT);
RelativeLayout.LayoutParams containerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
LinearLayout.LayoutParams surfaceParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT, 0.0F);
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
root = new RelativeLayout(this);
root.setLayoutParams(containerParams);
mSurfaceView = new SurfaceView(this);
mSurfaceView.setLayoutParams(surfaceParams);
root.addView(mSurfaceView);
Button stopButton = new Button(this);
stopButton.setText("click");
buttonParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
buttonParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
buttonParams.rightMargin = 5;
buttonParams.topMargin = 5;
stopButton.setLayoutParams(buttonParams);
root.addView(stopButton);
setContentView(root);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mIntent = this.getIntent();
quality = mIntent.getIntExtra("quality", 100);
stopButton.setOnClickListener(mSnapListener);
}
private OnClickListener mSnapListener = new OnClickListener() {
public void onClick(View v) {
mCamera.takePicture(null, null, mPictureCallback);
}
};
public boolean onCreateOptionsMenu(android.view.Menu menu) {
MenuItem item = menu.add(0, 0, 0, "goto gallery");
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Uri target = Uri.parse("content://media/external/images/media");
Intent intent = new Intent(Intent.ACTION_VIEW, target);
startActivity(intent);
return true;
}
});
return true;
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
}
/*
* We got the data, send it back to PhoneGap to be handled and processed.
*
*/
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
Log.e(TAG, "PICTURE CALLBACK: data.length = " + data.length);
storeAndExit(data);
}
};
/*
* We can't just store and exit, because Android freezes up when we try to cram a picture across a process in a Bundle.
* We HAVE to compress this data and send back the compressed data
*/
public void storeAndExit(byte[] data)
{
ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
Bitmap myMap = BitmapFactory.decodeByteArray(data, 0, data.length);
try {
if (myMap.compress(CompressFormat.JPEG, quality, jpeg_data))
{
byte[] code = jpeg_data.toByteArray();
byte[] output = Base64.encodeBase64(code);
String js_out = new String(output);
mIntent.putExtra("picture", js_out);
setResult(RESULT_OK, mIntent);
}
}
catch(Exception e)
{
//Do shit here
}
finish();
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
return super.onKeyDown(keyCode, event);
}
if (keyCode == KeyEvent.KEYCODE_CAMERA || keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_SEARCH) {
mCamera.takePicture(null, null, mPictureCallback);
return true;
}
return false;
}
protected void onResume()
{
Log.e(TAG, "onResume");
super.onResume();
}
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
}
protected void onStop()
{
Log.e(TAG, "onStop");
super.onStop();
}
public void surfaceCreated(SurfaceHolder holder)
{
Log.e(TAG, "surfaceCreated");
mCamera = Camera.open();
//mCamera.startPreview();
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
Log.e(TAG, "surfaceChanged");
// XXX stopPreview() will crash if preview is not running
if (mPreviewRunning) {
mCamera.stopPreview();
}
Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(w, h);
mCamera.setParameters(p);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera.startPreview();
mPreviewRunning = true;
}
public void surfaceDestroyed(SurfaceHolder holder)
{
Log.e(TAG, "surfaceDestroyed");
mCamera.stopPreview();
mPreviewRunning = false;
mCamera.release();
}
}

View File

@ -23,15 +23,20 @@ package com.phonegap;
*/
import java.io.File;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.ViewGroup;
@ -46,6 +51,7 @@ import android.webkit.WebViewClient;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.widget.LinearLayout;
import android.os.Build.*;
import android.provider.MediaStore;
public class DroidGap extends Activity {
@ -64,6 +70,8 @@ public class DroidGap extends Activity {
private Storage cupcakeStorage;
private CryptoHandler crypto;
private Uri imageUri;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
@ -344,26 +352,34 @@ public class DroidGap extends Activity {
// This is required to start the camera activity! It has to come from the previous activity
public void startCamera(int quality)
{
Intent i = new Intent(this, CameraPreview.class);
i.setAction("android.intent.action.PICK");
i.putExtra("quality", quality);
startActivityForResult(i, 0);
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));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, 0);
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
String data;
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK)
{
data = intent.getStringExtra("picture");
// Send the graphic back to the class that needs it
launcher.processPicture(data);
}
else
{
launcher.failPicture("Did not complete!");
}
super.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!");
}
}
public WebView getView()