diff --git a/android/ZBarScannerActivity.java b/android/ZBarScannerActivity.java
index 06b3d86..9021835 100644
--- a/android/ZBarScannerActivity.java
+++ b/android/ZBarScannerActivity.java
@@ -7,9 +7,11 @@ import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
+import android.content.res.Configuration;
import android.content.res.Resources;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
+import android.hardware.Camera.Parameters;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.AutoFocusCallback;
import android.os.Bundle;
@@ -176,25 +178,6 @@ implements SurfaceHolder.Callback {
die(e.getMessage());
return;
}
-
- Camera.Parameters camParams = camera.getParameters();
- if(flashMode.equals("on")) {
- camParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
- } else if(flashMode.equals("off")) {
- camParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
- } else {
- camParams.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
- }
- if (android.os.Build.VERSION.SDK_INT >= 14) {
- camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
- }
-
- try { camera.setParameters(camParams); }
- catch (RuntimeException e) {
- Log.d("csZBar", "Unsupported camera parameter reported for flash mode: "+flashMode);
- }
-
- tryStartPreview();
}
@Override
@@ -251,7 +234,78 @@ implements SurfaceHolder.Callback {
holder = hld;
tryStartPreview();
}
+ public void onConfigurationChanged(Configuration newConfig)
+ {
+ super.onConfigurationChanged(newConfig);
+ int rotation = getWindowManager().getDefaultDisplay().getRotation();
+ switch(rotation)
+ {
+ case 0: // '\0'
+ rotation = 90;
+ break;
+ case 1: // '\001'
+ rotation = 0;
+ break;
+
+ case 2: // '\002'
+ rotation = 270;
+ break;
+
+ case 3: // '\003'
+ rotation = 180;
+ break;
+
+ default:
+ rotation = 90;
+ break;
+ }
+ camera.setDisplayOrientation(rotation);
+ android.hardware.Camera.Parameters params = camera.getParameters();
+ tryStopPreview();
+ tryStartPreview();
+
+ }
+
+ public void toggleFlash(View view)
+ {
+
+
+ camera.startPreview();
+ android.hardware.Camera.Parameters camParams = camera.getParameters();
+ //If the flash is set to off
+ if(camParams.getFlashMode().equals(Parameters.FLASH_MODE_OFF) && !(camParams.getFlashMode().equals(Parameters.FLASH_MODE_TORCH))&& !(camParams.getFlashMode().equals(Parameters.FLASH_MODE_ON)))
+ camParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
+ else //if(camParams.getFlashMode() == Parameters.FLASH_MODE_ON || camParams.getFlashMode()== Parameters.FLASH_MODE_TORCH)
+ camParams.setFlashMode(Parameters.FLASH_MODE_OFF);
+ try
+ {
+ // camera.setParameters(camParams);
+ camera.setPreviewDisplay(holder);
+ camera.setPreviewCallback(previewCb);
+ camera.startPreview();
+ if (android.os.Build.VERSION.SDK_INT >= 14) {
+ camera.autoFocus(autoFocusCb); // We are not using any of the
+ // continuous autofocus modes as that does not seem to work
+ // well with flash setting of "on"... At least with this
+ // simple and stupid focus method, we get to turn the flash
+ // on during autofocus.
+ camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
+ }
+ //tryStopPreview();
+ //tryStartPreview();
+ //camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
+ camera.setParameters(camParams);
+
+
+ }
+ catch(RuntimeException e)
+ {
+ Log.d("csZBar", (new StringBuilder("Unsupported camera parameter reported for flash mode: ")).append(flashMode).toString());
+ } catch (IOException e) {
+ Log.d("csZBar", (new StringBuilder("Wrong holder data")).append(flashMode).toString());
+ }
+ }
// Continuously auto-focus -----------------------------------------
// For API Level < 14
@@ -380,8 +434,37 @@ implements SurfaceHolder.Callback {
private void tryStartPreview () {
if(holder != null) {
try {
+ int rotation = getWindowManager().getDefaultDisplay().getRotation();
+ switch(rotation)
+ {
+ case 0: // '\0'
+ rotation = 90;
+ break;
+
+ case 1: // '\001'
+ rotation = 0;
+ break;
+
+ case 2: // '\002'
+ rotation = 270;
+ break;
+
+ case 3: // '\003'
+ rotation = 180;
+ break;
+
+ default:
+ rotation = 90;
+ break;
+ }
// 90 degrees rotation for Portrait orientation Activity.
- camera.setDisplayOrientation(90);
+ camera.setDisplayOrientation(rotation);
+ android.hardware.Camera.Parameters camParams = camera.getParameters();
+
+ //camParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
+
+ camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
+ camera.setParameters(camParams);
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(previewCb);
diff --git a/android/res/layout/cszbarscanner.xml b/android/res/layout/cszbarscanner.xml
index 4566d31..9fb5cbe 100644
--- a/android/res/layout/cszbarscanner.xml
+++ b/android/res/layout/cszbarscanner.xml
@@ -48,6 +48,15 @@
android:gravity="center_vertical"
android:background="#ff0000" />
+
+
diff --git a/ios/CsZBar.h b/ios/CsZBar.h
index e774ae2..f043e6c 100644
--- a/ios/CsZBar.h
+++ b/ios/CsZBar.h
@@ -1,9 +1,14 @@
#import
#import "ZBarSDK.h"
+#import
@interface CsZBar : CDVPlugin
- (void)scan: (CDVInvokedUrlCommand*)command;
+- (void)toggleflash;
+
+
+
@end
diff --git a/ios/CsZBar.m b/ios/CsZBar.m
index 9d28c35..439a076 100644
--- a/ios/CsZBar.m
+++ b/ios/CsZBar.m
@@ -1,4 +1,5 @@
#import "CsZBar.h"
+#import
#import "AlmaZBarReaderViewController.h"
#pragma mark - State
@@ -7,6 +8,7 @@
@property bool scanInProgress;
@property NSString *scanCallbackId;
@property AlmaZBarReaderViewController *scanReader;
+
@end
@@ -25,12 +27,30 @@
{
self.scanInProgress = NO;
}
+- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
+ return;
+}
+- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
+{
+ return YES; //(interfaceOrientation == UIInterfaceOrientationPortrait);
+}
+/*
+- (void)viewDidLoad {
+ UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
+ [button setTitle:@"Turn on Flash" forState:UIControlStateNormal];
+ [button sizeToFit];
+ // Set a new (x,y) point for the button's center
+ button.center = CGPointMake(320/2, 60);
+ [button addTarget:self action:@selector(flashOn) forControlEvents:UIControlEventTouchUpInside];
+ [self.viewController parentViewController:button];
+}*/
#pragma mark - Plugin API
- (void)scan: (CDVInvokedUrlCommand*)command;
{
+
if(self.scanInProgress) {
[self.commandDelegate
sendPluginResult: [CDVPluginResult
@@ -53,16 +73,24 @@
// as not all devices will have a rear-facing camera.
self.scanReader.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
+ self.scanReader.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
+
NSString *flash = [params objectForKey:@"flash"];
- if([flash isEqualToString:@"on"]) {
+ if([flash isEqualToString:@"on"]) {
self.scanReader.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
} else if([flash isEqualToString:@"off"]) {
self.scanReader.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
+ }else if([flash isEqualToString:@"auto"]) {
+ self.scanReader.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
}
// Hack to hide the bottom bar's Info button... originally based on http://stackoverflow.com/a/16353530
UIView *infoButton = [[[[[self.scanReader.view.subviews objectAtIndex:2] subviews] objectAtIndex:0] subviews] objectAtIndex:3];
[infoButton setHidden:YES];
+ // Add an action in current code file (i.e. target)
+ // [infoButton addTarget: action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
+
+ //UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setTitle:@"Press Me" forState:UIControlStateNormal]; [button sizeToFit]; [self.view addSubview:button];
BOOL drawSight = [params objectForKey:@"drawSight"] ? [[params objectForKey:@"drawSight"] boolValue] : true;
if(drawSight){
@@ -70,12 +98,12 @@
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CGFloat dim = screenWidth < screenHeight ? screenWidth / 1.1 : screenHeight / 1.1;
- UIView *polygonView = [[UIView alloc] initWithFrame: CGRectMake ( (screenWidth/2) - (dim/2), (screenHeight/2) - (dim/2), dim, dim)];
+ UIView *polygonView = [[UIView alloc] initWithFrame: CGRectMake ( (screenWidth/2) - (dim/2), (screenHeight/2) - (dim/2), dim, dim)];
//polygonView.center = self.scanReader.view.center;
//polygonView.layer.borderColor = [UIColor greenColor].CGColor;
//polygonView.layer.borderWidth = 3.0f;
- UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(dim / 2, 0, 1, dim)];
+ UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,dim / 2, dim, 1)];
lineView.backgroundColor = [UIColor redColor];
[polygonView addSubview:lineView];
@@ -87,6 +115,23 @@
}
}
+- (void)toggleflash{
+ AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
+ [device lockForConfiguration:nil];
+ if (device.torchAvailable == 1) {
+ if (device.torchMode == 0) {
+ [device setTorchMode:AVCaptureTorchModeOn];
+ [device setFlashMode:AVCaptureFlashModeOn];
+
+ }else{
+ [device setTorchMode:AVCaptureTorchModeOff];
+ [device setFlashMode:AVCaptureFlashModeOff];
+ }
+
+ }
+ [device unlockForConfiguration];
+
+}
#pragma mark - Helpers
@@ -98,6 +143,10 @@
#pragma mark - ZBarReaderDelegate
+- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
+ return;
+}
+
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
if ([self.scanReader isBeingDismissed]) { return; }