2013-02-16 176 views
7

请让我寻找工作以获得访问通过kivy的Android相机,或者我可以将与kivy集成以便访问相机的库。通过Kivy访问Android相机

我很多开发Android应用程序,但使用中的python-kivy的UI,

任何事情都会非常感激,

感谢。

回答

1

This链接到可以找到自定义实现的discution。它基于PyJNIus自动包装android API的Camera类。 没有尝试自己,但你可以试试看...

5

这是我的示例代码,适用于Android。只需导入该文件https://github.com/kivy/plyer/blob/master/plyer/platforms/android/camera.py 此外,不要忘记添加CAMERA权限清单。

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() 
+0

@Suzana_K&megastruktur:我已经使用了代码,它可以很好地将照片保存到/ sdcard(它为Android开启原生摄像头界面),但是在拍摄之后,应用刚关闭/它不会返回到应用屏幕使用照片。我现在只使用Kivy启动器。你能帮助吗? – simplynail 2016-08-23 11:56:17

0

由于这是我很难找到答案如何使用相机在Android上,我想我会分享我的旅程以保存下一个人的时间的答案。

我找不到从Kivy使工作Camera类直接的方式:

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

终于让我找到了解决办法上面贴和后浪费了一些时间在我的应用程序执行它,它变成了拍摄照片后我无法重新回到应用程序 - 应用程序被终止,所以我无法回到应用程序来使用照片(我使用的是Kivy Launcher)。 就在最近,我发现访问摄像机的这种方式被放弃(https://github.com/kivy/plyer/issues/16#issuecomment-54094174

但后来我发现下面的解决方案,并通过刚刚运行示例代码,它看起来像我就能得到结果我想(只是需要一个小调整不会崩溃的时候Android摄像头被取消/没有照片已拍摄)

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

编辑: 出现在我的应用程序被终止,因为我没有在最上面的窗口小部件实现on_pause: return True。尽管如此,上面的文字仍然可能有帮助