Kivy相机显示屏旋转-90度 [英] Kivy Camera display is rotated -90 degrees
问题描述
我正在使用带有kivy框架的python访问android 4.4中的相机.
I am accessing my camera in android 4.4 using python with kivy framework.
这是我的简单代码:
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()
但是当我运行我的代码时,它显示如下:
But when I ran my code, it displayed this:
理想情况下,它看起来应该像这样:
Ideally, it should look like this:
看起来像kivy相机正在以-90度内置显示输出.这是正常现象还是错误?还是我应该自己旋转显示器?
It looks like the kivy Camera is displaying the output with a built in -90 degrees. Is this normal or a bug? Or should I rotate the display myself?
推荐答案
Cristian的解决方案不适用于我. 它将在写入canvas.before的行上引发一个Parser异常:
The Solution from Cristian does not work for me. It will throw an Parser Exception at the line where canvas.before is written:
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只能在其他对象中调用,以下代码对我有用:
With regards that the canvas.before and canvas.after can only be called within other Objects the following Code is working for me:
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()
资源:
- https://kivy.org/doc/stable/examples/gen__canvas__rotation__py.html
- https://github.com /kivy-garden/garden.zbarcam/blob/20171220/zbarcam/zbarcam.kv#L22
- https://kivy.org/doc/stable/examples/gen__canvas__rotation__py.html
- https://github.com/kivy-garden/garden.zbarcam/blob/20171220/zbarcam/zbarcam.kv#L22
这篇关于Kivy相机显示屏旋转-90度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!