mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-20 23:56:20 +08:00
Working Camera Attempt, need to merge back into trunk
This commit is contained in:
parent
66c3a47067
commit
df6eaf4f9d
@ -23,6 +23,11 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</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>
|
||||
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<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>
|
||||
</resources>
|
||||
|
@ -1,13 +1,7 @@
|
||||
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 org.apache.commons.codec.binary.Base64;
|
||||
|
||||
|
||||
public class CameraLauncher {
|
||||
|
||||
@ -23,25 +17,18 @@ public class CameraLauncher {
|
||||
|
||||
public void takePicture(int quality)
|
||||
{
|
||||
mGap.startCamera();
|
||||
mGap.startCamera(quality);
|
||||
}
|
||||
|
||||
/* Return Base64 Encoded String to Javascript */
|
||||
public void processPicture( byte[] data )
|
||||
public void processPicture( String js_out )
|
||||
{
|
||||
mAppView.loadUrl("javascript:navigator.camera.win('" + js_out + "');");
|
||||
}
|
||||
|
||||
public void failPicture(String err)
|
||||
{
|
||||
ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
|
||||
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
|
||||
{
|
||||
mAppView.loadUrl("javascript:Camera.fail();");
|
||||
}
|
||||
mAppView.loadUrl("javascript:navigator.camera.fail('" + err + "');");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,16 @@
|
||||
package com.phonegap.demo;
|
||||
|
||||
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;
|
||||
@ -19,7 +25,7 @@ import android.widget.Button;
|
||||
|
||||
public class CameraPreview extends Activity implements SurfaceHolder.Callback{
|
||||
|
||||
private static final String TAG = "CameraApiTest";
|
||||
private static final String TAG = "PhoneGapCamera";
|
||||
private SurfaceView mSurfaceView;
|
||||
private SurfaceHolder mSurfaceHolder;
|
||||
|
||||
@ -45,6 +51,8 @@ public class CameraPreview extends Activity implements SurfaceHolder.Callback{
|
||||
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
|
||||
mIntent = this.getIntent();
|
||||
|
||||
quality = mIntent.getIntExtra("quality", 100);
|
||||
|
||||
Button stopButton = (Button) findViewById(R.id.go);
|
||||
stopButton.setOnClickListener(mSnapListener);
|
||||
}
|
||||
@ -86,12 +94,29 @@ 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)
|
||||
{
|
||||
mIntent.putExtra("picture", data);
|
||||
setResult(RESULT_OK, mIntent);
|
||||
finish();
|
||||
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);
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
//Do shit here
|
||||
}
|
||||
finish();
|
||||
}
|
||||
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event)
|
||||
|
@ -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
|
||||
public void startCamera()
|
||||
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);
|
||||
}
|
||||
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
|
||||
{
|
||||
byte [] data;
|
||||
String data;
|
||||
super.onActivityResult(requestCode, resultCode, intent);
|
||||
data = intent.getByteArrayExtra("picture");
|
||||
// Send the graphic back to the class that needs it
|
||||
launcher.processPicture(data);
|
||||
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!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user