-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathandroid_camera_api.py
More file actions
84 lines (65 loc) · 2.99 KB
/
android_camera_api.py
File metadata and controls
84 lines (65 loc) · 2.99 KB
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
from kivy.logger import Logger
from jnius import JavaException, PythonJavaClass, autoclass, java_method # pylint: disable=import-error # type: ignore
Camera = autoclass("android.hardware.Camera")
AndroidActivityInfo = autoclass("android.content.pm.ActivityInfo")
AndroidPythonActivity = autoclass("org.kivy.android.PythonActivity")
class ShutterCallback(PythonJavaClass):
__javainterfaces__ = ("android.hardware.Camera$ShutterCallback",)
@java_method("()V")
def onShutter(self):
# apparently, it is enough to have an empty shutter callback to play
# the standard shutter sound. If you pass None instead of shutter_cb
# below, the standard sound doesn't play O_o
pass
class PictureCallback(PythonJavaClass):
__javainterfaces__ = ("android.hardware.Camera$PictureCallback",)
def __init__(self, filename, on_success):
super(PictureCallback, self).__init__()
self.filename = filename
self.on_success = on_success
@java_method("([BLandroid/hardware/Camera;)V")
def onPictureTaken(self, data, camera):
s = data.tostring()
with open(self.filename, "wb") as f:
f.write(s)
Logger.info("xcamera: picture saved to %s", self.filename)
camera.startPreview()
self.on_success(self.filename)
class AutoFocusCallback(PythonJavaClass):
__javainterfaces__ = ("android.hardware.Camera$AutoFocusCallback",)
def __init__(self, filename, on_success):
super(AutoFocusCallback, self).__init__()
self.filename = filename
self.on_success = on_success
@java_method("(ZLandroid/hardware/Camera;)V")
def onAutoFocus(self, success, camera):
if success:
Logger.info("xcamera: autofocus succeeded, taking picture...")
shutter_cb = ShutterCallback()
picture_cb = PictureCallback(self.filename, self.on_success)
camera.takePicture(shutter_cb, None, picture_cb)
else:
Logger.info("xcamera: autofocus failed")
def take_picture(camera_widget, on_success, filename):
# to call the android API, we need access to the underlying
# android.hardware.Camera instance. However, there is no official way to
# retrieve it from the camera widget, so we need to dig into internal
# attributes :-( This works at least on kivy 1.9.1, but it might break any
# time soon.
camera = camera_widget._camera._android_camera
params = camera.getParameters()
params.setFocusMode("auto")
params.setRotation(90)
camera.setParameters(params)
cb = AutoFocusCallback(filename, on_success)
Logger.info("xcamera: starting autofocus...")
try:
camera.autoFocus(cb)
except JavaException as e:
Logger.info("Error when calling autofocus: {}".format(e))
def set_orientation(value):
previous = get_orientation()
AndroidPythonActivity.mActivity.setRequestedOrientation(value)
return previous
def get_orientation():
return AndroidPythonActivity.mActivity.getRequestedOrientation()