[英]Kivy: Camera does not respect pos_hint (but does respect size_hint)
I'm writing a program in Kivy, and have run into a roadblock.我正在 Kivy 中编写程序,但遇到了障碍。 Camera objects (from
kivy.uix.camera
) respond to changes in size_hint
, but do not respond to changes in pos_hint
.相机对象(来自
kivy.uix.camera
)响应size_hint
的变化,但不响应pos_hint
的变化。 For pos_hint, it instead always centers the camera feed in the imaginary box drawn by size_hint (ie if size_hint: (0.5, 0.5)
, then the feed would be centered within the bottom left half of the window, regardless of what pos_hint is set to).对于 pos_hint,它总是将相机馈送集中在由 size_hint 绘制的假想框中(即,如果
size_hint: (0.5, 0.5)
,则馈送将在 window 的左下半部分居中,无论 pos_hint 设置为什么)。
Here is a minimum reproducible example.这是一个最小的可重现示例。 If you want different
size_hint
or pos_hint
values, feel free to alter the values given:如果您想要不同
size_hint
或pos_hint
值,请随意更改给定的值:
import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.camera import Camera
from kivy.core.window import Window
Window.fullscreen = 'auto' # uses display's current resolution
class CameraStream(FloatLayout):
pass
class RootWidget(FloatLayout):
camera_stream = ObjectProperty(None)
def __init__(self, **kwargs):
super(RootWidget, self).__init__(**kwargs)
class LifterApp(App):
def build(self):
self.root = RootWidget()
return self.root
if __name__ == '__main__':
LifterApp().run()
#:kivy 1.11.1
<CameraStream>:
camera_stream: camera_stream_id
Camera:
id: camera_stream_id
resolution: (320, 240)
play: True
<RootWidget>:
camera_stream: camera_stream_id
CameraStream:
id: camera_stream_id
size_hint: (0.5, 0.5) # this does work
#size_hint: (0.1, 0.1) # ...so does this
#size_hint: (1, 1) # ...and this
pos_hint: {'center_x': 0.1, 'center_y': 0.1} # NOTE this doesn't work
#pos_hint: {'center_x': 0.9, 'center_y': 0.9} # ...neither does this
#pos_hint: {'x': 0, 'y': 0} # ...or this
I figured it out so I'll share my solution.我想通了,所以我会分享我的解决方案。 There were two issues with my code.
我的代码有两个问题。 First, I realized that
kivy.uix.camera.Camera
is derived from kivy.uix.image.Image
, so I checked those docs and sure enough, they said:首先,我意识到
kivy.uix.camera.Camera
是从kivy.uix.image.Image
派生的,所以我检查了这些文档,果然,他们说:
By default, the image is centered and fits inside the widget bounding box.
默认情况下,图像居中并适合小部件边界框。 If you don't want that, you can set allow_stretch to True and keep_ratio to False.
如果您不想这样,可以将 allow_stretch 设置为 True 并将 keep_ratio 设置为 False。
This explains the widget resizing (ie the "imaginary box") but not the actual video feed.这解释了小部件调整大小(即“想象框”),而不是实际的视频源。 The other issue I had was that I was trying to set all the properties under the
CameraStream:
of <RootWidget>
, when I actually needed to set them under the Camera:
of <CameraStream>
since it is the Camera object I'm modifying, not the CameraStream FloatLayout that contains it.我遇到的另一个问题是我试图在
<RootWidget>
的CameraStream:
下设置所有属性,而实际上我需要在<CameraStream>
的Camera:
下设置它们,因为它是我正在修改的 Camera object,不是包含它的 CameraStream FloatLayout。
Resolving both of these issues made it work how I desired.解决这两个问题使它按我的意愿工作。 For completeness, here is my updated lifter.kv file (the main.py file was unchanged):
为了完整起见,这是我更新的 lifter.kv 文件(main.py 文件未更改):
#:kivy 1.11.1
<CameraStream>:
camera_stream: camera_stream_id
Camera:
id: camera_stream_id
resolution: (320, 240)
play: True
allow_stretch: True
keep_ratio: False
size_hint: (0.5, 0.5)
#size_hint: (0.1, 0.1)
#size_hint: (1, 1)
pos_hint: {'center_x': 0.1, 'center_y': 0.1}
#pos_hint: {'center_x': 0.9, 'center_y': 0.9}
#pos_hint: {'x': 0, 'y': 0}
<RootWidget>:
camera_stream: camera_stream_id
CameraStream:
id: camera_stream_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.