| 1 |
import android |
| 2 |
import android.activity |
| 3 |
from os import remove |
| 4 |
from jnius import autoclass, cast |
| 5 |
from plyer.facades import Camera |
| 6 |
from plyer.platforms.android import activity |
| 7 |
|
| 8 |
Intent = autoclass('android.content.Intent') |
| 9 |
PythonActivity = autoclass('org.kivy.android.PythonActivity') |
| 10 |
MediaStore = autoclass('android.provider.MediaStore') |
| 11 |
Uri = autoclass('android.net.Uri') |
| 12 |
|
| 13 |
|
| 14 |
class AndroidCamera(Camera): |
| 15 |
|
| 16 |
def _take_picture(self, on_complete, filename=None): |
| 17 |
assert on_complete is not None |
| 18 |
self.on_complete = on_complete |
| 19 |
self.filename = filename |
| 20 |
android.activity.unbind(on_activity_result=self._on_activity_result) |
| 21 |
android.activity.bind(on_activity_result=self._on_activity_result) |
| 22 |
intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) |
| 23 |
uri = Uri.parse('file://' + filename) |
| 24 |
parcelable = cast('android.os.Parcelable', uri) |
| 25 |
intent.putExtra(MediaStore.EXTRA_OUTPUT, parcelable) |
| 26 |
activity.startActivityForResult(intent, 0x123) |
| 27 |
|
| 28 |
def _take_video(self, on_complete, filename=None): |
| 29 |
assert on_complete is not None |
| 30 |
self.on_complete = on_complete |
| 31 |
self.filename = filename |
| 32 |
android.activity.unbind(on_activity_result=self._on_activity_result) |
| 33 |
android.activity.bind(on_activity_result=self._on_activity_result) |
| 34 |
intent = Intent(MediaStore.ACTION_VIDEO_CAPTURE) |
| 35 |
uri = Uri.parse('file://' + filename) |
| 36 |
parcelable = cast('android.os.Parcelable', uri) |
| 37 |
intent.putExtra(MediaStore.EXTRA_OUTPUT, parcelable) |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1) |
| 42 |
activity.startActivityForResult(intent, 0x123) |
| 43 |
|
| 44 |
def _on_activity_result(self, requestCode, resultCode, intent): |
| 45 |
if requestCode != 0x123: |
| 46 |
return |
| 47 |
android.activity.unbind(on_activity_result=self._on_activity_result) |
| 48 |
if self.on_complete(self.filename): |
| 49 |
self._remove(self.filename) |
| 50 |
|
| 51 |
def _remove(self, fn): |
| 52 |
try: |
| 53 |
remove(fn) |
| 54 |
except OSError: |
| 55 |
pass |
| 56 |
|
| 57 |
|
| 58 |
def instance(): |
| 59 |
return AndroidCamera() |