Working Camera Attempt, need to merge back into trunk

This commit is contained in:
Joe Bowser 2009-07-31 13:52:45 -07:00
parent 66c3a47067
commit df6eaf4f9d
5 changed files with 59 additions and 33 deletions

View File

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

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="app_name">PhoneGap</string> <string name="app_name">PhoneGap</string>
<string name="url">file:///android_asset/index.html</string> <string name="url">file:///android_asset/www/index.html</string>
<string name="go">Snap</string> <string name="go">Snap</string>
</resources> </resources>

View File

@ -1,13 +1,7 @@
package com.phonegap.demo; package com.phonegap.demo;
import java.io.ByteArrayOutputStream;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.webkit.WebView; import android.webkit.WebView;
import org.apache.commons.codec.binary.Base64;
public class CameraLauncher { public class CameraLauncher {
@ -23,25 +17,18 @@ public class CameraLauncher {
public void takePicture(int quality) public void takePicture(int quality)
{ {
mGap.startCamera(); mGap.startCamera(quality);
} }
/* Return Base64 Encoded String to Javascript */ /* Return Base64 Encoded String to Javascript */
public void processPicture( byte[] data ) public void processPicture( String js_out )
{ {
ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream(); mAppView.loadUrl("javascript:navigator.camera.win('" + js_out + "');");
Bitmap myMap = BitmapFactory.decodeByteArray(data, 0, data.length);
if (myMap.compress(CompressFormat.JPEG, quality, jpeg_data))
{
byte[] code = jpeg_data.toByteArray();
byte[] output = Base64.encodeBase64(code);
String js_out = output.toString();
mAppView.loadUrl("javascript:Camera.win('" + js_out + "');");
} }
else
public void failPicture(String err)
{ {
mAppView.loadUrl("javascript:Camera.fail();"); mAppView.loadUrl("javascript:navigator.camera.fail('" + err + "');");
}
} }
} }

View File

@ -1,10 +1,16 @@
package com.phonegap.demo; package com.phonegap.demo;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat; import android.graphics.PixelFormat;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera; import android.hardware.Camera;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
@ -19,7 +25,7 @@ import android.widget.Button;
public class CameraPreview extends Activity implements SurfaceHolder.Callback{ public class CameraPreview extends Activity implements SurfaceHolder.Callback{
private static final String TAG = "CameraApiTest"; private static final String TAG = "PhoneGapCamera";
private SurfaceView mSurfaceView; private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder; private SurfaceHolder mSurfaceHolder;
@ -45,6 +51,8 @@ public class CameraPreview extends Activity implements SurfaceHolder.Callback{
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mIntent = this.getIntent(); mIntent = this.getIntent();
quality = mIntent.getIntExtra("quality", 100);
Button stopButton = (Button) findViewById(R.id.go); Button stopButton = (Button) findViewById(R.id.go);
stopButton.setOnClickListener(mSnapListener); stopButton.setOnClickListener(mSnapListener);
} }
@ -86,11 +94,28 @@ public class CameraPreview extends Activity implements SurfaceHolder.Callback{
} }
}; };
// Store what we have and get out! /*
* 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) public void storeAndExit(byte[] data)
{ {
mIntent.putExtra("picture", 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 = output.toString();
mIntent.putExtra("picture", js_out);
setResult(RESULT_OK, mIntent); setResult(RESULT_OK, mIntent);
}
}
catch(Exception e)
{
//Do shit here
}
finish(); finish();
} }

View File

@ -134,19 +134,28 @@ public class DroidGap extends Activity {
// This is required to start the camera activity! It has to come from the previous activity // This is required to start the camera activity! It has to come from the previous activity
public void startCamera() public void startCamera(int quality)
{ {
Intent i = new Intent(this, CameraPreview.class); Intent i = new Intent(this, CameraPreview.class);
i.setAction("android.intent.action.PICK");
i.putExtra("quality", quality);
startActivityForResult(i, 0); startActivityForResult(i, 0);
} }
protected void onActivityResult(int requestCode, int resultCode, Intent intent) protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{ {
byte [] data; String data;
super.onActivityResult(requestCode, resultCode, intent); super.onActivityResult(requestCode, resultCode, intent);
data = intent.getByteArrayExtra("picture"); if (resultCode == RESULT_OK)
{
data = intent.getStringExtra("picture");
// Send the graphic back to the class that needs it // Send the graphic back to the class that needs it
launcher.processPicture(data); launcher.processPicture(data);
} }
else
{
launcher.failPicture("Did not complete!");
}
}
} }