Button to activate flash

Adding button to activate/deactivate flash during the capturing the phase.
This commit is contained in:
micaTewe
2015-09-15 00:50:42 +02:00
committed by Daniel Cousens
parent c80f254496
commit 9a365ce13c
4 changed files with 169 additions and 23 deletions

View File

@@ -1,9 +1,14 @@
#import <Cordova/CDV.h>
#import "ZBarSDK.h"
#import <UIKit/UIKit.h>
@interface CsZBar : CDVPlugin <ZBarReaderDelegate>
- (void)scan: (CDVInvokedUrlCommand*)command;
- (void)toggleflash;
@end

View File

@@ -1,4 +1,5 @@
#import "CsZBar.h"
#import <AVFoundation/AVFoundation.h>
#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; }