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;
|
|
|
|
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
|
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.Bitmap.CompressFormat;
|
2009-07-31 07:56:07 +08:00
|
|
|
import android.webkit.WebView;
|
2009-08-01 04:52:45 +08:00
|
|
|
|
2009-07-18 05:06:49 +08:00
|
|
|
|
|
|
|
public class CameraLauncher {
|
2009-07-31 07:56:07 +08:00
|
|
|
|
2009-07-18 05:06:49 +08:00
|
|
|
private WebView mAppView;
|
2009-07-31 07:56:07 +08:00
|
|
|
private DroidGap mGap;
|
2010-06-05 06:21:57 +08:00
|
|
|
int mQuality;
|
2009-07-18 05:06:49 +08:00
|
|
|
|
2009-07-31 07:56:07 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void takePicture(int quality)
|
|
|
|
{
|
2010-06-05 06:21:57 +08:00
|
|
|
mQuality = quality;
|
|
|
|
mGap.startCamera();
|
2009-07-31 07:56:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return Base64 Encoded String to Javascript */
|
2010-06-05 05:54:16 +08:00
|
|
|
public void processPicture( Bitmap bitmap )
|
2009-08-01 04:52:45 +08:00
|
|
|
{
|
2010-06-05 05:54:16 +08:00
|
|
|
ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
|
|
|
|
try {
|
2010-06-05 06:21:57 +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);
|
|
|
|
mAppView.loadUrl("javascript:navigator.camera.win('" + js_out + "');");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(Exception e)
|
|
|
|
{
|
|
|
|
failPicture("fail");
|
|
|
|
}
|
|
|
|
|
2009-08-01 04:52:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void failPicture(String err)
|
2009-07-31 07:56:07 +08:00
|
|
|
{
|
2009-08-01 04:52:45 +08:00
|
|
|
mAppView.loadUrl("javascript:navigator.camera.fail('" + err + "');");
|
2009-07-18 05:06:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|