lundi 4 juillet 2016

Kivy-Python-Android Torch App


There is this torch app example available online. It uses a 'Switch' to turn flashlight on. It works. I've been trying/struggling to get a 'message' output on the device when the flashlight is turned on and off and I cannot seem to get the right code. Can anyone provide a hint. Here's the working torch app code (somehow, the text 'enlightme' never appears):

from kivy.app import App
from kivy.uix.switch import Switch
from jnius import autoclass

Camera = autoclass('android.hardware.Camera')
Parameters = autoclass('android.hardware.Camera$Parameters')


__version__ = '0.1'


class FlashApp(App):
    def build(self):
        self.root = Switch(text='enlightenme')
        self.root.bind(active=self.toggle_flash)
        self.camera = None
        return self.root

    def toggle_flash(self, *args):
        if self.camera == None:
            self.camera = Camera.open()

        p = self.camera.getParameters()

        if self.root.active:
            p.setFlashMode(Parameters.FLASH_MODE_TORCH)
            self.camera.setParameters(p)
            self.camera.startPreview()
        else:
            p.setFlashMode(Parameters.FLASH_MODE_OFF)
            self.camera.stopPreview()
            self.camera.setParameters(p)
            self.camera.release()
            self.camera = None

if __name__ == '__main__':
    FlashApp().run()

Aucun commentaire:

Enregistrer un commentaire