community
community copied to clipboard
Android front camera setParameters failed
Versions
- Python: 3.6.9
- OS: Ubuntu 18.04
- Kivy: 1.11.1
- Kivy installation method: pip
Description
Camera Example at kivy/examples/camera/main.py crashes after deploying and running on my Android device. The only change I make to the script is adding index=1 to Camera parameters in order to use the front camera. The logs have the following line: JavaException: JVM exception occurred: setParameters failed.
The way I see it, the issue is associated to the following line in camera_android.py: params.setFocusMode('continuous-picture').
The front camera focus mode is 'fixed', therefore, it cannot be set to 'continuous-picture'. It would be great to add getFocusMode method to the module and a condition to set the proper focus mode.
Code and Logs
'''
Camera Example
==============
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.
'''
# Uncomment these lines to see all the messages
# from kivy.logger import Logger
# import logging
# Logger.setLevel(logging.TRACE)
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
import time
Builder.load_string('''
<CameraClick>:
orientation: 'vertical'
Camera:
index: 1
id: camera
resolution: (640, 480)
play: False
ToggleButton:
text: 'Play'
on_press: camera.play = not camera.play
size_hint_y: None
height: '48dp'
Button:
text: 'Capture'
size_hint_y: None
height: '48dp'
on_press: root.capture()
''')
class CameraClick(BoxLayout):
def capture(self):
'''
Function to capture the images and give them the names
according to their captured time and date.
'''
camera = self.ids['camera']
timestr = time.strftime("%Y%m%d_%H%M%S")
camera.export_to_png("IMG_{}.png".format(timestr))
print("Captured")
class TestCamera(App):
def build(self):
return CameraClick()
TestCamera().run()
01-24 11:26:26.105 32090 32425 I python : kivy.lang.builder.BuilderException: Parser: File "<inline>", line 7:
01-24 11:26:26.106 32090 32425 I python : ...
01-24 11:26:26.111 32090 32425 I python : 5: index: 1
01-24 11:26:26.112 32090 32425 I python : 6: id: camera
01-24 11:26:26.113 32090 32425 I python : >> 7: resolution: (640, 480)
01-24 11:26:26.113 32090 32425 I python : 8: play: False
01-24 11:26:26.114 32090 32425 I python : 9: ToggleButton:
01-24 11:26:26.114 32090 32425 I python : ...
01-24 11:26:26.115 32090 32425 I python : JavaException: JVM exception occurred: setParameters failed
01-24 11:26:26.115 32090 32425 I python : File "/mnt/Data/KIVY/test/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/superhero/kivy/lang/builder.py", line 700, in _apply_rule
01-24 11:26:26.116 32090 32425 I python : File "kivy/weakproxy.pyx", line 35, in kivy.weakproxy.WeakProxy.__setattr__
01-24 11:26:26.116 32090 32425 I python : File "kivy/properties.pyx", line 497, in kivy.properties.Property.__set__
01-24 11:26:26.117 32090 32425 I python : File "kivy/properties.pyx", line 839, in kivy.properties.ListProperty.set
01-24 11:26:26.118 32090 32425 I python : File "kivy/properties.pyx", line 544, in kivy.properties.Property.set
01-24 11:26:26.118 32090 32425 I python : File "kivy/properties.pyx", line 599, in kivy.properties.Property.dispatch
01-24 11:26:26.118 32090 32425 I python : File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
01-24 11:26:26.119 32090 32425 I python : File "kivy/_event.pyx", line 1120, in kivy._event.EventObservers._dispatch
01-24 11:26:26.119 32090 32425 I python : File "/mnt/Data/KIVY/test/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/superhero/kivy/uix/camera.py", line 103, in _on_index
01-24 11:26:26.120 32090 32425 I python : File "/mnt/Data/KIVY/test/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/superhero/kivy/core/camera/camera_android.py", line 42, in __init__
01-24 11:26:26.121 32090 32425 I python : File "/mnt/Data/KIVY/test/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/superhero/kivy/core/camera/__init__.py", line 70, in __init__
01-24 11:26:26.121 32090 32425 I python : File "/mnt/Data/KIVY/test/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/superhero/kivy/core/camera/camera_android.py", line 54, in init_camera
01-24 11:26:26.122 32090 32425 I python : File "jnius/jnius_export_class.pxi", line 766, in jnius.jnius.JavaMethod.__call__
01-24 11:26:26.157 32090 32425 I python : File "jnius/jnius_export_class.pxi", line 860, in jnius.jnius.JavaMethod.call_method
01-24 11:26:26.157 32090 32425 I python : File "jnius/jnius_utils.pxi", line 91, in jnius.jnius.check_exception
01-24 11:26:26.158 32090 32425 I python :
01-24 11:26:26.158 32090 32425 I python : Python for android ended.```
Any fix? Have just run into this problem myself
Any fix? Have just run into this problem myself
I did not find anything better than modify directly camera_android.py The path to the file in my project directory is the following: ./.buildozer/android/platform/build-armeabi-v7a/build/other_builds/kivy/armeabi-v7a__ndk_target_21/kivy/kivy/core/camera/camera_android.py Simply replace params.setFocusMode('continuous-picture') with params.setFocusMode('fixed'). Works like a charm!
@Auskas answer didn't work for me initially. It turns out that it's necessary to additionally ensure that index: 1
is the first parameter. If it is after resolution
then the camera service will not connect.
@Auskas How can I apply this fix inside kivy.core.camera? Kivy is downloaded from buildozer/p4a from .spec file.
Maybe you can try to use the param
# (str) Custom source folders for requirements
# Sets custom source for any requirements with recipes
# requirements.source.kivy = ../../kivy
in the spec file.
still no update?
still no update?
I'm going to open a pull request to fix the issue. I've never done it before but it's better late than never. Like I said, if your android device camera does not support the 'continuous-picture' focus mode, the initialization of the camera will fail. Therefore, the fix I plan is going to implement the following approach: getting the supported focus modes and set the focus mode to 'continuous-picture' only if it is supported. Stay tuned.
@Auskas thanks. but from which branch do you plan on branching out from? the camera doesn't work at all in the master branch 🤣 🤣 🤣 🤣 🤣 🤣 🤣 🤣 🤣
@Auskas thanks. but from which branch do you plan on branching out from? the camera doesn't work at all in the master branch 🤣 🤣 🤣 🤣 🤣 🤣 🤣 🤣 🤣
There must've been some misunderstanding: the issue has been transferred from kivy/kivy to kivy/python-for-android, but it has nothing to do with python-for-android. The root cause is in kivy/kivy/core/camera/camera_android.py in the master branch.
Hi @Auskas , i tried your fix but i'm still having problems getting the from camera to work. I place the index property as the first parameter on the camera, and edited the file you mentioned but i'm stil getting the same problem.
`... 07-13 10:08:59.259 23525 23553 I python : 15: index: 1 07-13 10:08:59.259 23525 23553 I python : 16: id: camera 07-13 10:08:59.259 23525 23553 I python : >> 17: resolution: (640, 480) 07-13 10:08:59.259 23525 23553 I python : 19: play: True 07-13 10:08:59.259 23525 23553 I python : ...
params.setFocusMode('fixed') ` is there anything else i can do to get it working?
@patrickarsenio , comment out the resolution: (640, 480)
line
this is still occuring ,