Created
November 1, 2017 14:34
-
-
Save vakrilov/fd111af5a42ea29196a206f705ec1ae8 to your computer and use it in GitHub Desktop.
Flashlight
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = require("tns-core-modules/application"); | |
var plat = require("tns-core-modules/platform"); | |
exports.turnOn = function (args) { | |
flash(true); | |
} | |
exports.turnOff = function (args) { | |
flash(false); | |
} | |
function flash(onOrOff) { | |
if (plat.isAndroid) { | |
flashAndroid(onOrOff); | |
} else { | |
flashIos(onOrOff); | |
} | |
} | |
function flashAndroid(onOrOff) { | |
var context = app.android.context; | |
var cameraService = context.getSystemService(android.content.Context.CAMERA_SERVICE) | |
var cameras = cameraService.getCameraIdList(); | |
for (var i = 0; i < cameras.length; i++) { | |
var characteristics = cameraService.getCameraCharacteristics(cameras[i]); | |
var flashAvailable = characteristics.get(android.hardware.camera2.CameraCharacteristics.FLASH_INFO_AVAILABLE) == "true"; | |
if (flashAvailable) { | |
cameraService.setTorchMode(cameras[i], onOrOff); | |
} | |
} | |
} | |
function flashIos(onOrOff) { | |
var device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo); | |
device.lockForConfiguration(null); | |
if (onOrOff) { | |
device.setTorchModeOnWithLevelError(AVCaptureMaxAvailableTorchLevel, null); | |
device.flashMode = AVCaptureFlashMode.AVCaptureFlashModeOn; | |
} else { | |
device.torchMode = AVCaptureTorchMode.AVCaptureTorchModeOff; | |
device.flashMode = AVCaptureFlashMode.AVCaptureFlashModeOff; | |
} | |
device.unlockForConfiguration(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment