【发布时间】:2013-02-01 08:02:19
【问题描述】:
我正在寻找一种解决方法以通过 kivy 访问 Android 摄像头,或者我可以与 kivy 集成 以访问摄像头的库。
我正在为 android 开发一个应用程序,但使用 python-kivy 作为 UI,
任何事情都会非常感激,
非常感谢。
【问题讨论】:
我正在寻找一种解决方法以通过 kivy 访问 Android 摄像头,或者我可以与 kivy 集成 以访问摄像头的库。
我正在为 android 开发一个应用程序,但使用 python-kivy 作为 UI,
任何事情都会非常感激,
非常感谢。
【问题讨论】:
这是我的示例代码,适用于 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()
【讨论】:
Kivy 对调用相机有一些原生支持。查看this page from the new programming guide 获取核心提供程序或查看this page from the new programming guide 获取uix 小部件。理论上,核心应该能够在平台之间适应,然后小部件应该能够使用相机。
【讨论】:
This 链接到可以找到自定义实现的问题。它基于 PyJNIus 对 android API 的 Camera 类的自动包装。 自己没试过,但你可以试一试...
【讨论】:
感谢这篇文章,我能够解决我的应用程序中的一个关键问题,非常感谢这里是我使用的代码,希望你们可以在某处使用它。
我做了一个屏幕并使用了 plyer 相机功能
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
我还为屏幕、标签和弹出窗口等使用了一些其他导入,您可以根据您的要求确定地查看它们
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()
【讨论】:
由于我很难找到如何在 android 上使用相机的答案,所以我想我会分享我的答案之旅以节省下一个人的时间。
我找不到直接从 Kivy 制作相机类的方法:
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。尽管如此,上面的文字仍然可能会有所帮助
【讨论】:
几年后,Android API 在处理权限和存储提供程序方面发生了变化。
我通过 Kivy here 为 Android 相机提供了一个完整的工作示例。它基本上需要对python-for-android 中的已编译清单文件进行一些调整,以及直接使用FileProvider。
【讨论】: