Skip to content
This repository was archived by the owner on Nov 14, 2023. It is now read-only.

Commit 7b0aae3

Browse files
committed
Update preview_camerax.py
1 parent 729e9fd commit 7b0aae3

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

src/camera4kivy/preview_camerax.py

+37-8
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def connect_camera(self,
7272
self._name_pipe = []
7373
self.texture_size = []
7474
self.rotation = 0
75+
self.capture_in_progress = False
7576

7677
# uniform case
7778
self.flash_state = default_flash.lower()
@@ -128,14 +129,25 @@ def connect_camera(self,
128129
self._configure_camera(True)
129130

130131
# destroy camera
131-
@run_on_ui_thread
132132
def disconnect_camera(self):
133133
self._deschedule_pipeline()
134134
if self._camera:
135-
self._camera.unbind_camera()
136-
self._camera = None
137-
return self.texture_size
138-
135+
if not self.capture_in_progress:
136+
self.do_disconnect_camera()
137+
else:
138+
self._disconnect_ev = Clock.schedule_interval(
139+
self.can_disconnect_camera, 1 / 30)
140+
141+
def can_disconnect_camera(self,dt):
142+
if not self.capture_in_progress:
143+
self.do_disconnect_camera()
144+
Clock.unschedule(self._disconnect_ev)
145+
146+
@run_on_ui_thread
147+
def do_disconnect_camera(self):
148+
self._camera.unbind_camera()
149+
self._camera = None
150+
139151
# configure camera
140152
def _configure_camera(self, start):
141153
self.configure_viewport()
@@ -188,6 +200,7 @@ def cg_scale(self, touch0, touch1, scale, x, y):
188200

189201
def capture_photo(self, location = '', subdir = '', name = ''):
190202
if self._camera:
203+
self.capture_in_progress = True
191204
self._set_location(location)
192205
subdir = self._default_subdir_android(subdir)
193206
name = self._default_file_name(name, '.jpg')
@@ -197,6 +210,7 @@ def capture_photo(self, location = '', subdir = '', name = ''):
197210

198211
def capture_video(self, location = '', subdir = '', name = ''):
199212
if self._camera:
213+
self.capture_in_progress = True
200214
self._set_location(location)
201215
subdir = self._default_subdir_android(subdir)
202216
name = self._default_file_name(name,'.mp4')
@@ -248,7 +262,6 @@ def capture_screenshot(self, location = '.', subdir = '', name = ''):
248262

249263

250264
# Select back, front camera
251-
@run_on_ui_thread
252265
def select_camera(self, facing):
253266
facing = facing.lower()
254267
if self._camera:
@@ -261,9 +274,24 @@ def select_camera(self, facing):
261274
self.facing = 'front'
262275
else:
263276
self.facing = 'back'
264-
self._camera.select_camera(self.facing)
277+
278+
if not self.capture_in_progress:
279+
self.do_select_camera()
280+
else:
281+
self.stop_capture_video()
282+
self._facing_ev = Clock.schedule_interval(
283+
self.can_select_camera, 1 / 30)
265284
return self.facing
266285

286+
def can_select_camera(self,dt):
287+
if not self.capture_in_progress:
288+
self.do_select_camera()
289+
Clock.unschedule(self._facing_ev)
290+
291+
@run_on_ui_thread
292+
def do_select_camera(self):
293+
self._camera.select_camera(self.facing)
294+
267295
# Sequence flash : off, on, auto, ...
268296
def flash(self):
269297
if self._camera:
@@ -493,8 +521,9 @@ def _filename_callback(self, file_id):
493521
if self._name_pipe:
494522
file_id = self._name_pipe[0]
495523
self._name_pipe = self._name_pipe[1:]
524+
self.capture_in_progress = False
496525
if self.callback:
497-
self.callback(file_id)
526+
self.callback(str(file_id))
498527

499528
def _analyze_texture(self):
500529
if not self.enable_data and self._analyze_callback:

0 commit comments

Comments
 (0)