15

Please i am looking for a work around to get access Android camera through kivy, or a library that i can integrate with kivy in order to access the Camera.

I am developing an application for android but using python-kivy for the UI,

anything will be really appreciated,

thanks alot.

Android_coder
  • 9,953
  • 3
  • 17
  • 23
Plaix
  • 831
  • 3
  • 11
  • 20

6 Answers6

6

Here's my sample code, that works on Android. Just import that file https://github.com/kivy/plyer/blob/master/plyer/platforms/android/camera.py Also, don't forget to add CAMERA permissions to manifest.

main.py:

__version__ = '1.0'

import kivy

# importing file from https://github.com/kivy/plyer/blob/master/plyer/platforms/android/camera.py
# I downloaded it and saved it in the same directory:
from camera import AndroidCamera

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty, StringProperty

import base64

class MyCamera(AndroidCamera):
    pass

class BoxLayoutW(BoxLayout):
    my_camera = ObjectProperty(None)
    # /sdcard means internal mobile storage for that case:
    image_path = StringProperty('/sdcard/my_test_photo.png')

    def __init__(self, **kwargs):

        super(BoxLayoutW, self).__init__()

        self.my_camera = MyCamera()

    def take_shot(self):
        self.my_camera._take_picture(self.on_success_shot, self.image_path)

    def on_success_shot(self, loaded_image_path):
        # converting saved image to a base64 string:
        image_str = self.image_convert_base64
        return True

    #converting image to a base64, if you want to send it, for example, via POST:
    def image_convert_base64(self):
        with open(self.image_path, "rb") as image_file:
            encoded_string = base64.b64encode(image_file.read())
        if not encoded_string:
            encoded_string = ''
        return encoded_string

if __name__ == '__main__':

    class CameraApp(App):
        def build(self):
            main_window = BoxLayoutW()
            return main_window

    CameraApp().run()

camera.kv:

<BoxLayoutW>:

    Button:
        text: 'shot'
        on_release: root.take_shot()
Suzana
  • 4,251
  • 2
  • 28
  • 52
megastruktur
  • 61
  • 1
  • 2
  • @Suzana_K & megastruktur:I've used the code and it works fine saving the photo to /sdcard (it starts native camera interface for Android) but after the shot the app just closes/ it does not return to the app screen to make use of the photo. I'm using Kivy Launcher just for now. Can You help with this? – simplynail Aug 23 '16 at 11:56
3

Kivy has some native support for calling the camera. Check out this page from the new programming guide for a core provider or this page from the new programming guide for a uix widget. In theory, the core should be able to adapt between platforms and the widget should then be able to use the camera.

Qanthelas
  • 514
  • 2
  • 8
  • 14
  • 6
    The kivy Camera does not work with the android camera. I tried to start the camera a tons of time on Android device but always failed, but it works perfectly on a desktop environment. – Plaix Mar 21 '13 at 06:08
1

This links to a discution where a custom implementation can be found. It is based on PyJNIus's automatic wrapping of the android API's Camera class. Didn't try myself but you can give it a try...

seb
  • 71
  • 1
  • 5
1

thanks to this post i was able to solve a critical problem in my app thanks a lot guys here is my code that i used i hope that you guys can use it somewhere.

I made a screen and used the plyer camera function

from os import getcwd
from os.path import exists
from os.path import splitext

import kivy
kivy.require('1.8.0')

from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
from kivy.logger import Logger

from plyer import camera

i also used some other imports for the screens and the labels and popups etc which you can definatly look into depending upon your requirment

class ScreenFive(Screen): #camera screen


def gg1back(self):
    self.parent.current = 'First'

def do_capture(self):

    filepath = 'IMG_1.jpg'
    ext = splitext(filepath)[-1].lower()

    try:
        camera.take_picture(self.camera_callback,filepath)
    except NotImplementedError:
        popup = MsgPopup(
            "The Face_rec_image feature has not yet \n been implemented for this platform :(")
        popup.open()

def camera_callback(self, filepath):
    if(exists(filepath)):
        popup = MsgPopup("Picture saved!")
        popup.open()
    else:
        popup = MsgPopup("Could not save your picture!")
        popup.open()
BigZee
  • 456
  • 5
  • 22
0

As it was hard for me to find the answer how to use camera on android I thought I'll share my journey to the answer to save next person's time.

I couldn't find the way to make work Camera class straight from Kivy:

https://kivy.org/docs/examples/gen__camera__main__py.html

finally I found the solution posted above, and after wasting some time implementing it in my app it turned out it was impossible for me to return to the app after photo being taken - the app was terminated, so I couldn't go back to the app to make use of the picture (I was using Kivy Launcher). Just recently I found out this way of accessing Camera was abandoned (https://github.com/kivy/plyer/issues/16#issuecomment-54094174 )

But then I found the solution below and by just running the example code it looks like I will be able to get results I want (it just needs a little tweaking not to crash when android camera is canceled/no photo has been taken)

https://github.com/kivy/kivy/tree/master/examples/android/takepicture

EDIT: appears my app was terminated because I didn't implement on_pause: return True in topmost widget. Nevertheless the text above still might be helpful

simplynail
  • 313
  • 1
  • 4
  • 13
0

Some years later, the Android API has changed as to how it deals with permissions and storage providers.

I have a full working example for the Android camera through Kivy here. It basically requires some tweaking of the compiled manifest file in python-for-android, as well as working directly with the FileProvider.

alecvn
  • 541
  • 1
  • 4
  • 15