gpt4 book ai didi

android - 通过 Kivy 访问 Android 摄像头

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:07:02 25 4
gpt4 key购买 nike

我正在寻找一个解决方法来通过 kivy 访问 Android 相机,或者我可以与 kivy 集成 以访问相机的库。

我正在为 android 开发应用程序,但使用 python-kivy 作为 UI,

任何事情都会非常感激,

非常感谢。

最佳答案

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

主要.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()

相机.kv:

<BoxLayoutW>:

Button:
text: 'shot'
on_release: root.take_shot()

关于android - 通过 Kivy 访问 Android 摄像头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14910065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com