Accessing android flashlight(camera LED flash) with kivy/python

会有一股神秘感。 提交于 2021-02-18 18:17:13

问题


I can't figure out how to access the led light on my android with python or kivy, I have tried installing python-for-android to be able to import the android module into my code but it's not the module can't be found. I cloned python-for-android as instructed here. I didn't install the ndk or sdk as per that page as I thought since kivy already uses them they were already installed. Can someone please point me in the right direction?


回答1:


Yes, you can write this app in Kivy from the desktop, you just won't be able to test it on the desktop. You will have to build and deploy to an Android device to test each time.

Adapted from How to turn on camera flash light programmatically in Android?:

To check if flash capability is available:

PythonActivity = autoclass('org.renpy.android.PythonActivity')
PackageManager = autoclass('android.content.pm.PackageManager')
pm = PythonActivity.mActivity.getPackageManager()
flash_available = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)

To use the flashlight, your app will need the FLASHLIGHT and CAMERA permissions. You can add these to buildozer.spec or the python-for-android command line.

Finally, to turn flash on:

Camera = autoclass('android.hardware.Camera')
CameraParameters = autoclass('android.hardware.Camera$Parameters')
cam = Camera.open()
params = cam.getParameters()
params.setFlashMode(CameraParameters.FLASH_MODE_TORCH)
cam.setParameters(params)
cam.startPreview()

And off:

cam.stopPreview()
cam.release()


来源:https://stackoverflow.com/questions/28111643/accessing-android-flashlightcamera-led-flash-with-kivy-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!