Skip to content

Commit

Permalink
new Flash support for Android / iOS.
Browse files Browse the repository at this point in the history
Android contribution from Aaron!
  • Loading branch information
tito committed Sep 27, 2015
1 parent 87513d8 commit 42058ef
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ Battery X X X X X X
Native file chooser X X X
Orientation X X
Audio recording X X
Flash X X X
================================== ============= ============= === ======= === =====
3 changes: 3 additions & 0 deletions plyer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@

#: Vibrator proxy to :class:`plyer.facades.Vibrator`
vibrator = Proxy('vibrator', facades.Vibrator)

#: Flash proxy to :class:`plyer.facades.Flash`
flash = Proxy('flash', facades.Flash)
3 changes: 2 additions & 1 deletion plyer/facades/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

__all__ = ('Accelerometer', 'Audio', 'Battery', 'Camera', 'Compass', 'Email',
'FileChooser', 'GPS', 'Gyroscope', 'IrBlaster', 'Orientation',
'Notification', 'Sms', 'TTS', 'UniqueID', 'Vibrator')
'Notification', 'Sms', 'TTS', 'UniqueID', 'Vibrator', 'Flash')

from plyer.facades.accelerometer import Accelerometer
from plyer.facades.audio import Audio
Expand All @@ -26,3 +26,4 @@
from plyer.facades.tts import TTS
from plyer.facades.uniqueid import UniqueID
from plyer.facades.vibrator import Vibrator
from plyer.facades.flash import Flash
40 changes: 40 additions & 0 deletions plyer/facades/flash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# coding=utf-8

class Flash(object):
"""Flash facade.
.. versionadded:: 1.2.5
This can be used to activate the flash of your camera on
Android and iOS
"""

def on(self):
"""Activate the flash
"""
self._on()

def off(self):
"""Deactiavte the flash
"""
self._off()

def release(self):
"""Release any access to the Flash / Camera.
Call this when you're done using the Flash.
This will release the Camera, and stop any process.
Next call to `_on` will reactivate it.
"""
self._release()

# private

def _on(self):
raise NotImplementedError()

def _off(self):
raise NotImplementedError()

def _release(self):
pass
55 changes: 55 additions & 0 deletions plyer/platforms/android/flash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# coding=utf-8
"""
Flash
-----
"""

from plyer.facades import Flash
from jnius import autoclass
from plyer.platforms.android import activity

Camera = autoclass("android.hardware.Camera")
CameraParameters = autoclass("android.hardware.Camera$Parameters")
SurfaceTexture = autoclass("android.graphics.SurfaceTexture")
PackageManager = autoclass('android.content.pm.PackageManager')
pm = activity.getPackageManager()
flash_available = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)


class AndroidFlash(Flash):
_camera = None

def _on(self):
if self._camera is None:
self._camera_open()
if not self._camera:
return
self._camera.setParameters(self._f_on)

def _off(self):
if not self._camera:
return
self._camera.setParameters(self._f_off)

def _release(self):
if not self._camera:
return
self._camera.stopPreview()
self._camera.release()
self._camera = None

def _camera_open(self):
if not flash_available:
return
self._camera = Camera.open()
self._f_on = cam.getParameters()
self._f_off = cam.getParameters()
self._f_on.setFlashMode(CameraParameters.FLASH_MODE_TORCH)
self._f_off.setFlashMode(CameraParameters.FLASH_MODE_OFF)
self._camera.startPreview()
# Need this for Nexus 5
self._camera.setPreviewTexture(SurfaceTexture(0))


def instance():
return AndroidFlash()
50 changes: 50 additions & 0 deletions plyer/platforms/ios/flash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# coding=utf-8
"""
Flash
-----
"""

from pyobjus import autoclass

NSString = autoclass("NSString")
AVCaptureDevice = autoclass("AVCaptureDevice")
AVMediaTypeVideo = NSString.alloc().initWithUTF8String_("vide")
AVCaptureTorchModeOff = 0
AVCaptureTorchModeOn = 1


class IosFlash(Flash):
_camera = None

def _on(self):
if self._camera is None:
self._camera_open()
if not self._camera:
return
self._camera.lockForConfiguration_(None)
try:
self._camera.setTorchMode(AVCaptureTorchModeOn)
finally:
self._camera.unlockForConfiguration()

def _off(self):
if not self._camera:
return
self._camera.lockForConfiguration_(None)
try:
self._camera.setTorchMode(AVCaptureTorchModeOff)
finally:
self._camera.unlockForConfiguration()

def _release(self):
pass

def _camera_open(self):
device = AVCaptureDevice.defaultDeviceWithMediaType_(AVMediaTypeVideo)
if not device:
return
self._camera = device


def instance():
return IosFlash()

0 comments on commit 42058ef

Please sign in to comment.