kivy: Kivy camera does not run on android device when compiled with Buildozer 1.0

Software Versions

  • Python: 3.6.5
  • OS: Ubuntu 18.04 LTS
  • Kivy: 1.11.1 and 2.0.0rc2(dev)
  • Kivy installation method: tested with Pip and Git(dev)
  • Buildozer: 1.0

Describe the bug Kivy camera does not run on android device when compiled with Buildozer 1.0.

Test 01 - Buildozer 0.34 with default configs

android.api = 19
android.minapi = 9
android.ndk = 9c

Result: It’s works fine for me with NDK 9c (others not work) , but the image appears rotated (90°).

Test 02 - Buildozer: 1.0 with default configs

android.api = 27
android.minapi = 21
android.ndk = 19b #I also tested 17c

Result: The app is compiled but closes after the presplash screen.

Test 03 - Buildozer: 1.0 with custom configs

android.api = 19
android.minapi = 18
android.ndk = 9c

Result: The Buildozer does not compile because the API Level was deprecated.

Related issues Issue #6369 kivy camera example does not work on android

Conclusions

  • Camera Kivy still works with ANDROID_SDK_VERSION = '20' and ANDROID_NDK_VERSION = '9c' with Buildozer 0.34.
  • The Camera widget depends of the android.hardware.Camera API (in camera_android.py file), that was deprecated in API level 21.
  • Buildozer 1.0 does not works with old API level.

Possible Solution

I think an alternative to solve the issue would be to upgrade the camera_android.py file with the Camera2 API, as in colour-blind-camera, but I still don’t know how to do it.

To Reproduce main.py:

'''
Camera Example
'''

# Uncomment these lines to see all the messages
# from kivy.logger import Logger
# import logging
# Logger.setLevel(logging.TRACE)

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
import time
Builder.load_string('''
<CameraClick>:
    orientation: 'vertical'
    Camera:
        id: camera
        resolution: (640, 480)
        play: False
    ToggleButton:
        text: 'Play'
        on_press: camera.play = not camera.play
        size_hint_y: None
        height: '48dp'
    Button:
        text: 'Capture'
        size_hint_y: None
        height: '48dp'
        on_press: root.capture()
''')

class CameraClick(BoxLayout):
    def capture(self):
        camera = self.ids['camera']
        timestr = time.strftime("%Y%m%d_%H%M%S")
        camera.export_to_png("IMG_{}.png".format(timestr))
        print("Captured")

class TestCamera(App):
    def build(self):
        return CameraClick()

TestCamera().run()

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 20 (9 by maintainers)

Most upvoted comments

I took a look and kivy.uix.camera.Camera() is missing the call to CoreCamera.init_camera() Also the default resolution causes the camera to return before doing anything

The two changes below get the Kivy Camera example https://kivy.org/doc/stable/examples/gen__camera__main__py.html working on Windows. Adding an init_camera() call is safe because all providers have init_camera() implemented.

There needs to be documentation for Windows saying to use the camera: pip install opencv-python

Please consider implementing these changes.

Next I’m going to look at Android, that might take a while or maybe forever.

kiv/uix/camera.py

    def _on_index(self, *largs):
        self._camera = None
        if self.index < 0:
            return
        ########### change from < 0 to < -1 ######################
        if self.resolution[0] < -1 or self.resolution[1] < -1: 
            return
        self._camera = CoreCamera(index=self.index,
                                  resolution=self.resolution, stopped=True)
        ########### add init_camera() ############################
        self._camera.init_camera() 
                    
        self._camera.bind(on_load=self._camera_loaded)
        if self.play:
            self._camera.start()
            self._camera.bind(on_texture=self.on_tex)





Now i’m working in camera_android.py in order fix the rotation issue.

Hi! Has anyone succeed to replicate the colour-blind-camera app?

We totally agree, I didn’t say it was bad either,

“bit rot” is a phrase that describes how software, untouched, decays. Of course the software doesn’t rot. It exists in an ever changing environment, and from the point of view of that environment it is decaying.

This is the issue with kivy.core.camera. Depreciated is somthing different.

I suppose, you misunderstood/misread my comment. I never said it is bad, and I decided to use it in my project just a few days ago. Also, I didn’t “make decisions”, I just repeated that it is old, lacks some features and needs an update - just what the devs said. For now, AFAIK, there are no people working on it, so one should consider implementing the camera functionality by other means to avoid such workarounds as in this issue.

Where is it documented that Kivy Camera is depreciated?

“Depreciated state” means “bit rot”, not depreciated. I suggest the decision the depreciate Kivy Camera should not be made passively, as the camera is possibly the most important hardware feature on a mobile device.

It is correct that Android Camera is depreciated in favor of Android Camera2. Camera2 works with Kivy as shown by the Color Blind Camera example.