Added front/back camera option

This commit is contained in:
Woon Tien Jing 2014-12-06 16:38:00 +08:00
parent 40e48e6c7c
commit 4ef4dfb51f
4 changed files with 35 additions and 2 deletions

View File

@ -25,6 +25,7 @@ Arguments:
{
text_title: "OPTIONAL Title Text - default = 'Scan QR Code'", // Android only
text_instructions: "OPTIONAL Instruction Text - default = 'Please point your camera at the QR code.'", // Android only
camera: "front" || "back" // defaults to "back"
}
```

View File

@ -1,6 +1,7 @@
package org.cloudsky.cordovaPlugins;
import java.io.IOException;
import java.lang.RuntimeException;
import org.json.JSONException;
import org.json.JSONObject;
@ -8,6 +9,7 @@ import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.AutoFocusCallback;
import android.os.Bundle;
@ -47,6 +49,9 @@ implements SurfaceHolder.Callback {
private ImageScanner scanner;
private int surfW, surfH;
// Customisable stuff
String whichCamera;
// For retrieving R.* resources, from the actual app package
// (we can't use actual.application.package.R.* in our code as we
// don't know the applciation package name when writing this plugin).
@ -75,6 +80,7 @@ implements SurfaceHolder.Callback {
catch (JSONException e) { params = new JSONObject(); }
String textTitle = params.optString("text_title");
String textInstructions = params.optString("text_instructions");
whichCamera = params.optString("camera");
// Initiate instance variables
autoFocusHandler = new Handler();
@ -119,10 +125,26 @@ implements SurfaceHolder.Callback {
super.onResume();
try {
if(whichCamera.equals("front")) {
int numCams = Camera.getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for(int i=0; i<numCams; i++) {
Camera.getCameraInfo(i, cameraInfo);
if(cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
camera = Camera.open(i);
}
}
} else {
camera = Camera.open();
} catch (Exception e){
}
if(camera == null) throw new Exception ("Error: No suitable camera found.");
} catch (RuntimeException e) {
die("Error: Could not open the camera.");
return;
} catch (Exception e) {
die(e.getMessage());
return;
}
tryStartPreview();

View File

@ -45,6 +45,15 @@
self.scanReader.readerDelegate = self;
self.scanReader.supportedOrientationsMask = ZBarOrientationMaskAll;
// Get user parameters
NSDictionary *params = (NSDictionary*) [command argumentAtIndex:0];
NSString * camera = [params objectForKey:@"camera"];
if([camera isEqualToString:@"front"]) {
// We do not set any specific device for the default "back" setting,
// as not all devices will have a rear-facing camera.
self.scanReader.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
// Hack to hide the bottom bar's Info button... http://stackoverflow.com/a/16353530
UIView *infoButton = [[[[[self.scanReader.view.subviews objectAtIndex:1] subviews] objectAtIndex:0] subviews] objectAtIndex:3];
[infoButton setHidden:YES];

View File

@ -12,6 +12,7 @@ ZBar.prototype = {
params = params || {};
if(params.text_title === undefined) params.text_title = "Scan QR Code";
if(params.text_instructions === undefined) params.text_instructions = "Please point your camera at the QR code.";
if(params.camera != "front") params.camera = "back";
exec(success, failure, 'CsZBar', 'scan', [params]);
},