Added an option for camera flash to be on/off/auto during QR code scanning

This commit is contained in:
Woon Tien Jing 2014-12-06 16:58:46 +08:00
parent 889be2cc3a
commit 0b0699f7b4
4 changed files with 25 additions and 1 deletions

View File

@ -26,6 +26,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"
flash: "on" || "off" || "auto" // defaults to "auto"
}
```

View File

@ -14,6 +14,7 @@ import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.AutoFocusCallback;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
@ -51,6 +52,7 @@ implements SurfaceHolder.Callback {
// Customisable stuff
String whichCamera;
String flashMode;
// For retrieving R.* resources, from the actual app package
// (we can't use actual.application.package.R.* in our code as we
@ -81,6 +83,7 @@ implements SurfaceHolder.Callback {
String textTitle = params.optString("text_title");
String textInstructions = params.optString("text_instructions");
whichCamera = params.optString("camera");
flashMode = params.optString("flash");
// Initiate instance variables
autoFocusHandler = new Handler();
@ -147,6 +150,19 @@ implements SurfaceHolder.Callback {
return;
}
Camera.Parameters camParams = camera.getParameters();
if(flashMode.equals("on")) {
camParams.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
} else if(flashMode.equals("off")) {
camParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
} else {
camParams.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
}
try { camera.setParameters(camParams); }
catch (RuntimeException e) {
Log.d("csZBar", "Unsupported camera parameter reported for flash mode: "+flashMode);
}
tryStartPreview();
}

View File

@ -47,12 +47,18 @@
// Get user parameters
NSDictionary *params = (NSDictionary*) [command argumentAtIndex:0];
NSString * camera = [params objectForKey:@"camera"];
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;
}
NSString *flash = [params objectForKey:@"flash"];
if([flash isEqualToString:@"on"]) {
self.scanReader.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
} else if([flash isEqualToString:@"off"]) {
self.scanReader.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
}
// 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];

View File

@ -13,6 +13,7 @@ ZBar.prototype = {
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";
if(params.flash != "on" && params.flash != "off") params.flash = "auto";
exec(success, failure, 'CsZBar', 'scan', [params]);
},