Android Studio 0.4.6
Hello,
I am trying to use the camera and I have set the permissions like this:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-feature android:name="android.hardware.camera"/>
I have imported the correct package:
import android.hardware.Camera;
And I try and open the camera like this:
Camera camera = Camera. there is no open method;
All I get are the following:
Parameters
Class
Size
I have follow the developer documentation, I can't see I have done anything wrong.
many thanks for any suggestions,
Just try to paste camera = Camera.open(); or type it in manually. Sometimes the "clever" thing glitches.
By the way, did the tool suggest to import android.hardware.Camera; or you typed it manually? I mean, is it an Android project? (Typically you start with the automatically generated hello world example and then add extra functionality to it, if you did something else, it's easier to re-do it from the very beginning.)
For anyone in the future facing the same issue
import android.hardware.*;
Related
I am pretty new to Appium, using UIAutomator2. Following instructions on the links:
[https://discuss.appium.io/t/click-back-button-on-android-device-in-java/6817/3][1]
[https://discuss.appium.io/t/click-back-button-twice-in-android-7-using-appium-uiautomator2/20368][2]
[https://stackoverflow.com/questions/30801879/how-to-automate-the-android-phone-back-button-using-appium][3]
I have tried all the options offered in these articles:
driver.pressKeyCode(4);
helper.driver.pressKeyCode(187);
driver.back();
driver.navigate().back();
If I add the imports
import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;
and I try
driver.pressKeyCode(AndroidKeyCode.BACK);
Eclipse tells me that the method is undefined...
But none of them have worked. Is there anything I need to import or any additional classes I need to create? My driver only has methods for testing, such as close(), equals(), execute(), findElement()... but nothing like back(), or pressKeyCode() or navigate()(this one, probably, because it is not webView)... So, when I try to just type any of those methods, Eclipse either suggests to add a cast, or to create a class...
Please, Can anyone give me some more details on how to do it?
The driver should be an Appium driver. And try to run on real device than emulators.
driver = new AppiumDriver<>(url, desiredCapabilities);
I am trying to navigate to the geolocation settings page, however, I get an error "ACTION_LOCATION_SOURCE_SETTINGS cannot be resolved or is not a field" in my code. I have searched everywhere, but couldn't find anything different from what I have done already.-
startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 1);
I am running Android 7.0. Is there any other way to do this on my android version?
I think you are using wrong import
Use the below import and remove the previous one
import android.provider.Settings;
Or you can also try like below.
Intent settingintent= new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(settingintent);
Please i am looking for a work around to get access Android camera through kivy, or a library that i can integrate with kivy in order to access the Camera.
I am developing an application for android but using python-kivy for the UI,
anything will be really appreciated,
thanks alot.
Here's my sample code, that works on Android. Just import that file https://github.com/kivy/plyer/blob/master/plyer/platforms/android/camera.py
Also, don't forget to add CAMERA permissions to manifest.
main.py:
__version__ = '1.0'
import kivy
# importing file from https://github.com/kivy/plyer/blob/master/plyer/platforms/android/camera.py
# I downloaded it and saved it in the same directory:
from camera import AndroidCamera
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty, StringProperty
import base64
class MyCamera(AndroidCamera):
pass
class BoxLayoutW(BoxLayout):
my_camera = ObjectProperty(None)
# /sdcard means internal mobile storage for that case:
image_path = StringProperty('/sdcard/my_test_photo.png')
def __init__(self, **kwargs):
super(BoxLayoutW, self).__init__()
self.my_camera = MyCamera()
def take_shot(self):
self.my_camera._take_picture(self.on_success_shot, self.image_path)
def on_success_shot(self, loaded_image_path):
# converting saved image to a base64 string:
image_str = self.image_convert_base64
return True
#converting image to a base64, if you want to send it, for example, via POST:
def image_convert_base64(self):
with open(self.image_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
if not encoded_string:
encoded_string = ''
return encoded_string
if __name__ == '__main__':
class CameraApp(App):
def build(self):
main_window = BoxLayoutW()
return main_window
CameraApp().run()
camera.kv:
<BoxLayoutW>:
Button:
text: 'shot'
on_release: root.take_shot()
Kivy has some native support for calling the camera. Check out this page from the new programming guide for a core provider or this page from the new programming guide for a uix widget. In theory, the core should be able to adapt between platforms and the widget should then be able to use the camera.
This links to a discution where a custom implementation can be found. It is based on PyJNIus's automatic wrapping of the android API's Camera class.
Didn't try myself but you can give it a try...
thanks to this post i was able to solve a critical problem in my app thanks a lot guys here is my code that i used i hope that you guys can use it somewhere.
I made a screen and used the plyer camera function
from os import getcwd
from os.path import exists
from os.path import splitext
import kivy
kivy.require('1.8.0')
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
from kivy.logger import Logger
from plyer import camera
i also used some other imports for the screens and the labels and popups etc which you can definatly look into depending upon your requirment
class ScreenFive(Screen): #camera screen
def gg1back(self):
self.parent.current = 'First'
def do_capture(self):
filepath = 'IMG_1.jpg'
ext = splitext(filepath)[-1].lower()
try:
camera.take_picture(self.camera_callback,filepath)
except NotImplementedError:
popup = MsgPopup(
"The Face_rec_image feature has not yet \n been implemented for this platform :(")
popup.open()
def camera_callback(self, filepath):
if(exists(filepath)):
popup = MsgPopup("Picture saved!")
popup.open()
else:
popup = MsgPopup("Could not save your picture!")
popup.open()
As it was hard for me to find the answer how to use camera on android I thought I'll share my journey to the answer to save next person's time.
I couldn't find the way to make work Camera class straight from Kivy:
https://kivy.org/docs/examples/gen__camera__main__py.html
finally I found the solution posted above, and after wasting some time implementing it in my app it turned out it was impossible for me to return to the app after photo being taken - the app was terminated, so I couldn't go back to the app to make use of the picture (I was using Kivy Launcher).
Just recently I found out this way of accessing Camera was abandoned (https://github.com/kivy/plyer/issues/16#issuecomment-54094174 )
But then I found the solution below and by just running the example code it looks like I will be able to get results I want (it just needs a little tweaking not to crash when android camera is canceled/no photo has been taken)
https://github.com/kivy/kivy/tree/master/examples/android/takepicture
EDIT:
appears my app was terminated because I didn't implement on_pause: return True in topmost widget. Nevertheless the text above still might be helpful
Some years later, the Android API has changed as to how it deals with permissions and storage providers.
I have a full working example for the Android camera through Kivy here. It basically requires some tweaking of the compiled manifest file in python-for-android, as well as working directly with the FileProvider.
I must be the first to face this problem because I can't find even a single thread about it.
Today I wanted to start on the camera aspect of my application needs.
I read up some documentation
my manifest looks like this:
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
I wrote this in the manifest ABOVE <application> and underneath <manifest>
What im doing is; I have created a new class.
using eclipse as my IDE.
I then declare a field:
Camera _camera;
In the constructor(just to test)
I tried to do:
_camera = Camera.open();
I got an error.
I use my real phone to test the app, because I have no webcam or anytihng for the simulator to use. And the simulator gives me a memory error when I tell it to have a camera.
Anyway, upon finding out why I can't use Camera.open (I included the package: android.graphics.Camera;) Because that is what eclipse included for me when i used to organize imports function.
I looked into the android.jar that eclipse attached for me. Contained in a folder thingy called Android 2.2 -> android.jar
I searched for android.graphics and took a peek in the content of Camera.class
It turns out that my Camera class only has these methods:
Camera()
applyToCanvas()
dotWithNormal()
finalize()
getMatrix()
restore()
rotateX()
rotateY()
rotateZ()
save()
translate()
I intentionally let the parameters out because they are of no importance.
To get to the actual question: Why?
Why is there no open() method, no release() method? and whatever else im missing.. '
Thanks for reading.
Todays lesson: Don't be a smart **s. I was indeed 950% sure I included that specific package. But it was the wrong package. derp. Thanks fo notifying me. Issue is solved.
You are using the wrong Camera.
android.graphics.Camera - A camera instance can be used to compute 3D transformations and generate a matrix that can be applied, for instance, on a Canvas.
android.hardware.Camera - The Camera class is used to set image capture settings, start/stop preview, snap pictures, and retrieve frames for encoding for video. This class is a client for the Camera service, which manages the actual camera hardware.
You are using a wrong Camera class. Use this one http://developer.android.com/reference/android/hardware/Camera.html
You are using Camera from android.graphics.Camera I suppose you need the one from android.hardware.Camera
(yes im 950% sure I have included the package:
android.graphics.Camera;)
You are looking for android.hardware.Camera.
Want to switch light on/off and not succeeding. Probably not getting the most from th documentation. In any event, this is what I have tried.
Amongst the imports I have
import android.hardware.Camera;
Within the body
Camera myCamera;
.......
myCamera = Camera.open();
.......
Camera.Parameters myCameraParameters = myCamera.getParameters();
myCameraParameters.setFlashMode(FLASH_MODE_TORCH);
The above line brings up an error 'FLASH_MODE_TORCH cannot be resolved to a variable' I am assuming that I am missing an import which defines FLASH_MODE_TORCH.
Anyine point me in the right direction?
IF I am missing an import, where should I go in the documentation to find out what imports are needed for what statements, constants .....
Regards,
Oliver
I believe it should be Camera.Parameters.FLASH_MODE_TORCH. I was having some difficulty getting visibility to this parameter, but you should take a look at this questions They seem to have it figured out with an example.
This code might help comeone else
Camera.Parameters myCameraParameters = myCamera.getParameters();
String stringFlashMode;
stringFlashMode = myCameraParameters.getFlashMode();
if (stringFlashMode.equals("torch"))
myCameraParameters.setFlashMode("on"); // Light is set off, flash is set to normal 'on' mode
else
myCameraParameters.setFlashMode("torch"); // This turns the light on
myCamera.setParameters(myCameraParameters);
Regards,
Oliver