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

@ -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);

View File

@ -48,6 +48,15 @@
android:gravity="center_vertical"
android:background="#ff0000" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick ="toggleFlash"
android:text="Flash" />
</RelativeLayout>
</FrameLayout>

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; }