I\'m trying to create a simples app in kivy that have two screen and i need it load the a custom camera in each screen dont need do in same time.
I tryed load in g
I took source code of class Camera
https://github.com/kivy/kivy/blob/master/kivy/uix/camera.py
and created own class MyCamera
I create instance of CoreCamera
outside class MyCamera
and all instances of MyCamera
use the same one instance of CoreCamera
core_camera = CoreCamera(index=0, resolution=(640, 480), stopped=True)
class MyCamera(Image):
def _on_index(self, *largs):
# ...
self._camera = core_camera
instead of
class MyCamera(Image):
def _on_index(self, *largs):
# ...
self._camera = CoreCamera(index=self.index, resolution=self.resolution, stopped=True)
It has still some problems
CoreCamera
is set resolution and all MyClass
use the same resolution - but they still need resolution: (640, 480)
in gui.kv
(but they don't use this value)play
starts/stops animation for all MyClass
because CoreCamera
control it.I used example from
https://kivy.org/doc/stable/examples/gen__camera__main__py.html
to create example with two widgets displaying the same camera
# https://kivy.org/doc/stable/examples/gen__camera__main__py.html
# https://github.com/kivy/kivy/blob/master/kivy/uix/camera.py
from kivy.uix.image import Image
from kivy.core.camera import Camera as CoreCamera
from kivy.properties import NumericProperty, ListProperty, BooleanProperty
# access to camera
core_camera = CoreCamera(index=0, resolution=(640, 480), stopped=True)
# Widget to display camera
class MyCamera(Image):
'''Camera class. See module documentation for more information.
'''
play = BooleanProperty(True)
'''Boolean indicating whether the camera is playing or not.
You can start/stop the camera by setting this property::
# start the camera playing at creation (default)
cam = Camera(play=True)
# create the camera, and start later
cam = Camera(play=False)
# and later
cam.play = True
:attr:`play` is a :class:`~kivy.properties.BooleanProperty` and defaults to
True.
'''
index = NumericProperty(-1)
'''Index of the used camera, starting from 0.
:attr:`index` is a :class:`~kivy.properties.NumericProperty` and defaults
to -1 to allow auto selection.
'''
resolution = ListProperty([-1, -1])
'''Preferred resolution to use when invoking the camera. If you are using
[-1, -1], the resolution will be the default one::
# create a camera object with the best image available
cam = Camera()
# create a camera object with an image of 320x240 if possible
cam = Camera(resolution=(320, 240))
.. warning::
Depending on the implementation, the camera may not respect this
property.
:attr:`resolution` is a :class:`~kivy.properties.ListProperty` and defaults
to [-1, -1].
'''
def __init__(self, **kwargs):
self._camera = None
super(MyCamera, self).__init__(**kwargs) # `MyCamera` instead of `Camera`
if self.index == -1:
self.index = 0
on_index = self._on_index
fbind = self.fbind
fbind('index', on_index)
fbind('resolution', on_index)
on_index()
def on_tex(self, *l):
self.canvas.ask_update()
def _on_index(self, *largs):
self._camera = None
if self.index < 0:
return
if self.resolution[0] < 0 or self.resolution[1] < 0:
return
self._camera = core_camera # `core_camera` instead of `CoreCamera(index=self.index, resolution=self.resolution, stopped=True)`
self._camera.bind(on_load=self._camera_loaded)
if self.play:
self._camera.start()
self._camera.bind(on_texture=self.on_tex)
def _camera_loaded(self, *largs):
self.texture = self._camera.texture
self.texture_size = list(self.texture.size)
def on_play(self, instance, value):
if not self._camera:
return
if value:
self._camera.start()
else:
self._camera.stop()
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
import time
Builder.load_string('''
<CameraClick>:
orientation: 'vertical'
MyCamera:
id: camera1
resolution: (640, 480)
MyCamera:
id: camera2
resolution: (640, 480)
ToggleButton:
text: 'Play'
on_press: camera1.play = not camera1.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['camera1']
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()