问题
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