mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-26 03:53:09 +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" />
|
<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>
|
||||||
|
|
||||||
|
|
||||||
|
@ -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>
|
||||||
|
@ -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))
|
|
||||||
{
|
public void failPicture(String err)
|
||||||
byte[] code = jpeg_data.toByteArray();
|
{
|
||||||
byte[] output = Base64.encodeBase64(code);
|
mAppView.loadUrl("javascript:navigator.camera.fail('" + err + "');");
|
||||||
String js_out = output.toString();
|
|
||||||
mAppView.loadUrl("javascript:Camera.win('" + js_out + "');");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
mAppView.loadUrl("javascript:Camera.fail();");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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,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)
|
public void storeAndExit(byte[] data)
|
||||||
{
|
{
|
||||||
mIntent.putExtra("picture", data);
|
ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
|
||||||
setResult(RESULT_OK, mIntent);
|
Bitmap myMap = BitmapFactory.decodeByteArray(data, 0, data.length);
|
||||||
finish();
|
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)
|
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
|
// 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)
|
||||||
// Send the graphic back to the class that needs it
|
{
|
||||||
launcher.processPicture(data);
|
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