This repository has been archived on 2025-02-05. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2025-02-05/src/bitmessagekivy/baseclass/scan_screen.py

106 lines
3.3 KiB
Python
Raw Normal View History

2022-06-16 17:14:45 +02:00
# pylint: disable=no-member, too-many-arguments, too-few-public-methods
# pylint: disable=no-name-in-module, unused-argument, arguments-differ
"""
QR code Scan Screen used in message composer to get recipient address
"""
import os
2022-08-26 14:17:43 +02:00
import logging
2022-06-16 17:14:45 +02:00
import cv2
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import (
BooleanProperty,
ObjectProperty,
StringProperty
)
from kivy.uix.screenmanager import Screen
2022-08-25 08:44:33 +02:00
from pybitmessage.bitmessagekivy.get_platform import platform
2022-06-16 17:14:45 +02:00
2022-08-26 14:17:43 +02:00
logger = logging.getLogger('default')
2022-06-16 17:14:45 +02:00
class ScanScreen(Screen):
"""ScanScreen is for scaning Qr code"""
# pylint: disable=W0212
2022-08-26 14:17:43 +02:00
camera_available = BooleanProperty(False)
2022-06-16 17:14:45 +02:00
previous_open_screen = StringProperty()
pop_up_instance = ObjectProperty()
def __init__(self, *args, **kwargs):
"""Getting AddressBook Details"""
super(ScanScreen, self).__init__(*args, **kwargs)
self.check_camera()
def check_camera(self):
"""This method is used for checking camera avaibility"""
if platform != "android":
cap = cv2.VideoCapture(0)
is_cam_open = cap.isOpened()
while is_cam_open:
logger.debug('Camera is available!')
2022-08-26 14:17:43 +02:00
self.camera_available = True
2022-06-16 17:14:45 +02:00
break
else:
logger.debug("Camera is not available!")
2022-08-26 14:17:43 +02:00
self.camera_available = False
2022-06-16 17:14:45 +02:00
else:
2022-08-26 14:17:43 +02:00
self.camera_available = True
2022-06-16 17:14:45 +02:00
def get_screen(self, screen_name, instance=None):
"""This method is used for getting previous screen name"""
self.previous_open_screen = screen_name
if screen_name != 'composer':
self.pop_up_instance = instance
def on_pre_enter(self):
"""
on_pre_enter works little better on android
It affects screen transition on linux
"""
if not self.children:
tmp = Builder.load_file(
os.path.join(
os.path.dirname(os.path.dirname(__file__)), "kv", "{}.kv").format("scanner")
)
self.add_widget(tmp)
if platform == "android":
Clock.schedule_once(self.start_camera, 0)
def on_enter(self):
"""
on_enter works better on linux
It creates a black screen on android until camera gets loaded
"""
if platform != "android":
Clock.schedule_once(self.start_camera, 0)
def on_leave(self):
"""This method will call on leave"""
Clock.schedule_once(self.stop_camera, 0)
def start_camera(self, *args):
"""Its used for starting camera for scanning qrcode"""
# pylint: disable=attribute-defined-outside-init
self.xcam = self.children[0].ids.zbarcam.ids.xcamera
if platform == "android":
self.xcam.play = True
else:
Clock.schedule_once(self.open_cam, 0)
def stop_camera(self, *args):
"""Its used for stop the camera"""
self.xcam.play = False
if platform != "android":
self.xcam._camera._device.release()
def open_cam(self, *args):
"""It will open up the camera"""
if not self.xcam._camera._device.isOpened():
self.xcam._camera._device.open(self.xcam._camera._index)
self.xcam.play = True