Skip to content

Instantly share code, notes, and snippets.

@vakrilov
Created November 1, 2017 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vakrilov/fd111af5a42ea29196a206f705ec1ae8 to your computer and use it in GitHub Desktop.
Save vakrilov/fd111af5a42ea29196a206f705ec1ae8 to your computer and use it in GitHub Desktop.
Flashlight
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