Phonegap-plugin-barcodescanner: Feature: Flashlight option (iOS)?

Created on 21 Jul 2015  路  11Comments  路  Source: phonegap/phonegap-plugin-barcodescanner

I've had some luck flicking the flashlight on in JS before launching the barcode scanner, and I've seen great results, is there a way we can add a button on the bottom left side to toggle the flashlight on/off from native code?

Looks like adding in functions from @EddyVerbruggen 's file would do the trick:

https://github.com/EddyVerbruggen/Flashlight-PhoneGap-Plugin/blob/master/src/ios/Flashlight.m

- (void)switchOn:(CDVInvokedUrlCommand*)command {
    CDVPluginResult* pluginResult;
    if ([self deviceHasFlashlight]) {
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        [device lockForConfiguration:nil];
        [device setTorchMode:AVCaptureTorchModeOn];
        [device setFlashMode:AVCaptureFlashModeOn];
        [device unlockForConfiguration];
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    } else {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Device is not capable of using the flashlight. Please test with flashlight.available()"];
    }
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)switchOff:(CDVInvokedUrlCommand*)command {
    CDVPluginResult* pluginResult;
    if ([self deviceHasFlashlight]) {
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        [device lockForConfiguration:nil];
        [device setTorchMode:AVCaptureTorchModeOff];
        [device setFlashMode:AVCaptureFlashModeOff];
        [device unlockForConfiguration];
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    } else {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Device is not capable of using the flashlight. Please test with flashlight.available()"];
    }
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

-(BOOL)deviceHasFlashlight {
    if (NSClassFromString(@"AVCaptureDevice")) {
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        return [device hasTorch] && [device hasFlash];
    }
    return false;
}
enhancement

Most helpful comment

@douglasevaristo Instead of the 馃槙 and 馃憥 can you please share what platform you're using, what problem you have exactly, how you're using the plugin, and how you're building it?

All 11 comments

+1

+1

+1

(until then, I'll use https://github.com/tjwoon/csZBar)

+1

+1

Landed in 6.0.4 on both iOS and Android. Just pass in showTorchButton : true as an option of the scan function.

"Landed in 6.0.4 on both iOS and Android. Just pass in showTorchButton : true as an option of the scan function." This don't work anymore 馃憥

@douglasevaristo Instead of the 馃槙 and 馃憥 can you please share what platform you're using, what problem you have exactly, how you're using the plugin, and how you're building it?

Hi. I can confirm that the torch button is not showing up. Flip button does show up.
I'm using ionic lab + ionic view to debug the app on iPhone 7 10.2.1 (14D27).

ionic view is an app that might not have latest version of the plugin, run your app instead of using ionic view

This thread has been automatically locked.

Was this page helpful?
0 / 5 - 0 ratings