cordova-android/framework/src/com/phonegap/CameraLauncher.java

56 lines
1.1 KiB
Java
Raw Normal View History

package com.phonegap;
2009-07-18 05:06:49 +08:00
import java.io.ByteArrayOutputStream;
import org.apache.commons.codec.binary.Base64;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.webkit.WebView;
2009-07-18 05:06:49 +08:00
public class CameraLauncher {
2009-07-18 05:06:49 +08:00
private WebView mAppView;
private DroidGap mGap;
2010-06-05 06:21:57 +08:00
int mQuality;
2009-07-18 05:06:49 +08:00
CameraLauncher(WebView view, DroidGap gap)
2009-07-18 05:06:49 +08:00
{
mAppView = view;
mGap = gap;
}
public void takePicture(int quality)
{
2010-06-05 06:21:57 +08:00
mQuality = quality;
mGap.startCamera();
}
/* Return Base64 Encoded String to Javascript */
public void processPicture( Bitmap bitmap )
{
ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
try {
2010-06-05 06:21:57 +08:00
if (bitmap.compress(CompressFormat.JPEG, mQuality, 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)
{
mAppView.loadUrl("javascript:navigator.camera.fail('" + err + "');");
2009-07-18 05:06:49 +08:00
}
}