Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/kivy_garden/zbarcam/zbarcam.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import threading
import queue
from collections import namedtuple

import PIL
Expand Down Expand Up @@ -49,6 +51,9 @@ def _on_camera_ready(self, xcamera):
Starts binding when the `xcamera._camera` instance is ready.
"""
xcamera._camera.bind(on_texture=self._on_texture)
self._decoding_frame = threading.Event()
self._symbols_queue = queue.Queue()
Clock.schedule_interval(self._update_symbols, 0)

def _remove_shoot_button(self):
"""
Expand All @@ -59,13 +64,26 @@ def _remove_shoot_button(self):
shoot_button = xcamera.children[0]
xcamera.remove_widget(shoot_button)

def _on_texture(self, instance):
self.symbols = self._detect_qrcode_frame(
texture=instance.texture, code_types=self.code_types)
def _on_texture(self, xcamera):
if not self._decoding_frame.is_set():
self._decoding_frame.set()
threading.Thread(
target=self._detect_qrcode_frame,
args=(
xcamera.texture,
xcamera.texture.pixels,
self.code_types,
),
).start()

@classmethod
def _detect_qrcode_frame(cls, texture, code_types):
image_data = texture.pixels
def _update_symbols(self, *args):
try:
self.symbols = self._symbols_queue.get_nowait()
except queue.Empty:
return

def _detect_qrcode_frame(self, texture, pixels, code_types):
image_data = pixels
size = texture.size
# Fix for mode mismatch between texture.colorfmt and data returned by
# texture.pixels. texture.pixels always returns RGBA, so that should
Expand All @@ -76,10 +94,13 @@ def _detect_qrcode_frame(cls, texture, code_types):
pil_image = fix_android_image(pil_image)
symbols = []
codes = pyzbar.decode(pil_image, symbols=code_types)

for code in codes:
symbol = ZBarCam.Symbol(type=code.type, data=code.data)
symbols.append(symbol)
return symbols

self._symbols_queue.put(symbols)
self._decoding_frame.clear()

@property
def xcamera(self):
Expand Down