Kivy相机显示屏旋转-90度

时间:2018-06-22 02:14:34

标签: android python kivy

我正在使用带有kivy框架的python访问android 4.4中的相机。

这是我的简单代码:

from kivy.app import App
from kivy.uix.camera import Camera
from kivy.core.window import Window

class CamApp(App):
    def build(self):
        return Camera(resolution= Window.size)

CamApp().run()

但是当我运行代码时,它显示如下:

enter image description here

理想情况下,它应该看起来像这样:

enter image description here

看起来像kivy相机正在以-90度内置显示输出。这是正常现象还是错误?还是我应该自己旋转显示器?

3 个答案:

答案 0 :(得分:1)

我认为这是一个错误,但是如果您使用以下代码,则可以旋转小部件:

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()
    canvas.before:
        PushMatrix
        Rotate:
            angle: -90
            origin: self.center
    canvas.after:
        PopMatrix
''')


class CameraClick(BoxLayout):
    def capture(self):
        '''
        Function to capture the images and give them the names
        according to their captured time and date.
        '''
        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()

这在Android上对我有用。

答案 1 :(得分:0)

最好在奇异的代码中使用opencv。它为您提供了许多操作图片和视频的选项和工具。 此代码已准备就绪,可用于PC。 我遇到了180度翻转问题,并使用opencv进行了修复。您可以使用我的代码以及以下链接:https://www.tutorialkart.com/opencv/python/opencv-python-rotate-image/

import os

import cv2
from kivy.app import App
from kivy.clock import Clock
from kivy.graphics.texture import Texture
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.image import Image


class KivyCamera(Image):
    def __init__(self, capture=None, fps=0, **kwargs):
        super(KivyCamera, self).__init__(**kwargs)
        # self.capture = cv2.VideoCapture("/sdcard2/python-apk/2.mp4")
        # print "file path exist :" + str(os.path.exists("/sdcard2/python-apk/1.mkv"))
        self.capture = cv2.VideoCapture(0)
        Clock.schedule_interval(self.update, 1.0 / fps)

    def update(self, dt):
        ret, frame = self.capture.read()
        # print str(os.listdir('/sdcard2/'))
        if ret:
            # convert it to texture
            buf1 = cv2.flip(frame, 0)
            buf = buf1.tostring()
            image_texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
            image_texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
            # display image from the texture
            self.texture = image_texture


class CamApp(App):
    def build(self):
        self.my_camera = KivyCamera(fps=12)
        self.box = BoxLayout(orientation='vertical')
        btn1 = Button(text="Hello")
        self.box.add_widget(btn1)
        # l = Label(text=cv2.__version__, font_size=150)
        # self.box.add_widget(l)
        self.box.add_widget(self.my_camera)
        return self.box

    def on_stop(self):
        # without this, app will not exit even if the window is closed
        # self.capture.release()
        pass

    def on_pause(self):
        return True


if __name__ == '__main__':
    CamApp().run()enter code here

答案 2 :(得分:0)

Cristian的解决方案对我不起作用。 它将在canvas.before编写的行上引发Parser Exception:

kivy.lang.parser.ParserException: Parser: File "<inline>", line 21:
I python  :  ...
I python  :       19:        height: '48dp'
I python  :       20:        on_press: root.capture()
I python  :  >>   21:    canvas.before:
I python  :       22:        PushMatrix
I python  :       23:        Rotate:
I python  :  ...
I python  :  Invalid class name

关于canvas.before和canvas.after只能在其他对象中调用,以下代码对我有用:

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
        canvas.before:
            PushMatrix
            Rotate:
                angle: -90
                origin: self.center
        canvas.after:
            PopMatrix
    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):
        '''
        Function to capture the images and give them the names
        according to their captured time and date.
        '''
        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()

资源: