-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
142 lines (119 loc) · 4.5 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
Camera Example
==============
https://kivy.org/doc/stable/examples/gen__camera__main__py.html
This example demonstrates a simple use of the camera. It shows a window with
a buttoned labelled 'play' to turn the camera on and off. Note that
not finding a camera, perhaps because gstreamer is not installed, will
throw an exception during the kv language processing.
"""
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
import time
from android.permissions import request_permissions, Permission
import readPunchCard
# Uncomment these lines to see all the messages
# from kivy.logger import Logger
# import logging
# Logger.setLevel(logging.__ALL__)
Window.maximize()
Builder.load_string('''
#:import Clipboard kivy.core.clipboard.Clipboard
<CameraClick>:
result: result
start: start
copy: copy
quit: quit
orientation: 'horizontal'
BoxLayout:
orientation: 'vertical'
size_hint: 0.9, 1
Camera:
id: camera
size_hint: 1, 0.9
#resolution: (900, 500) # distorted
#resolution: (800, 400) # ok
# 4032, 2268 = Galaxy S7, S8 = 9,1MP = 16:9
#resolution: (4032, 2268) # S7 ok, S8 crash
resolution: (1920, 1080) # S8 ok
allow_stretch: True
keep_ratio: True
play: True
Label:
id: result
#multiline: False
size_hint: 1, 0.1
height: '40dp'
text: ''
markup: True
background_color: 0.91, 0.83, 0.78, 1
# Default the background color for this label
# to r 0, g 0, b 0, a 0
BoxLayout:
orientation: 'vertical'
size_hint: 0.1, 1
height: '40dp'
Button:
id: quit
text: 'Quit'
background_color: .8, .0, .0, 1
on_press: app.stop()
# Copy to clipboard
# https://stackoverflow.com/questions/63790475/how-to-program-copy-to-paste-from-clipboard-buttons-on-app-developed-by-kivy
Button:
id: copy
text: 'Copy'
background_color: 0, .0, .8, 1
on_press: Clipboard.copy(root.plainText)
Button:
id: start
text: 'Scan'
background_color: 0, .8, .0, 1
on_press: root.capture()
''')
class CameraClick(BoxLayout):
resultText = ''
plainText = ''
def capture(self):
"""
Function to capture the images and give them the names
according to their captured time and date.
"""
self.result.text = "Running"
camera = self.ids['camera']
fn = "/sdcard/CARD_{}.png".format(time.strftime("%Y%m%d_%H%M%S"))
# camera.export_to_png(fn)
camera.texture.save(fn) # https://github.com/kivy/kivy/issues/7872
print(f"Captured {fn}")
self.result.text = "Running"
self.resultText, self.plainText = readPunchCard.run(file=fn, contrast=1.3)
self.result.text = self.resultText
# the following is not used, but might be an idea for direct sharing
# https://gist.github.com/kived/0f450e738bf79c003253
# https://stackoverflow.com/questions/38983649/kivy-android-share-image
# https://stackoverflow.com/questions/63322944/how-to-use-share-button-to-share-content-of-my-app
def share_text(self):
from jnius import autoclass
PythonActivity = autoclass('org.kivy.android.PythonActivity')
Intent = autoclass('android.content.Intent')
String = autoclass('java.lang.String')
shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.setType('text/plain')
shareIntent.putExtra(Intent.EXTRA_TEXT, self.plainText)
chooser = Intent.createChooser(shareIntent, String('Share Punch Card Text'))
PythonActivity.mActivity.startActivity(chooser)
class CardCamera(App):
def build(self):
request_permissions([
Permission.CAMERA,
Permission.WRITE_EXTERNAL_STORAGE,
Permission.READ_EXTERNAL_STORAGE
])
# https://www.albertgao.xyz/2017/05/22/how-to-get-current-app-folder-in-kivy/
userDataDir = getattr(self, 'user_data_dir')
print(f"user_data_dir={userDataDir}")
# Galaxy S7 JK/Card/Android/data/com.keinert.cardreader/files
return CameraClick()
CardCamera().run()