Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Flash switch issue on newer API #806

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 37 additions & 20 deletions plyer/platforms/android/flash.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
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)
from plyer.platforms.android import SDK_INT
if SDK_INT >= 23:
Context = autoclass('android.content.Context')
CameraManager = activity.getSystemService(Context.CAMERA_SERVICE)
CameraCharacteristics = autoclass("android.hardware.camera2.CameraCharacteristics")
CamId = CameraManager.getCameraIdList()[0] # First Camera
flash_available = CameraManager.getCameraCharacteristics(CamId).get(CameraCharacteristics.FLASH_INFO_AVAILABLE)
else :
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):
Expand All @@ -24,31 +31,41 @@ def _on(self):
self._camera_open()
if not self._camera:
return
self._camera.setParameters(self._f_on)

if SDK_INT >= 23:
CameraManager.setTorchMode(self._camera, True)
else:
self._camera.setParameters(self._f_on)

def _off(self):
if not self._camera:
return
self._camera.setParameters(self._f_off)
if SDK_INT >= 23:
CameraManager.setTorchMode(self._camera, False)
else:
self._camera.setParameters(self._f_off)

def _release(self):
if not self._camera:
return
self._camera.stopPreview()
self._camera.release()
if SDK_INT < 23:
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 = Camera.getParameters()
self._f_off = Camera.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))
if SDK_INT >= 23 :
self._camera = CamId
else:
self._camera = Camera.open()
self._f_on = Camera.getParameters()
self._f_off = Camera.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():
Expand Down