-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new Flash support for Android / iOS.
Android contribution from Aaron!
- Loading branch information
Showing
6 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
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
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 |
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
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() |
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
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() |