I've integrated Camera within my app. The below codes are working fine when I'm running on emulator and I've selected webcam as my camera in ADB. But when I'm running on Actual device like Nexus 7 then my app is not able to detect camera. camera is working fine in this tablet independently but not within my app.
CameraActivity.java
//I've imported hardware camera class
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
// Some activity code
if (!getPackageManager().
hasSystemFeature(PackageManager.FEATURE_CAMERA)) { // Issue is coming from here.
Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)
.show();
} else {
cameraId = findFrontFacingCamera();
if (cameraId < 0) {
Toast.makeText(this, "No front facing camera found.",
Toast.LENGTH_LONG).show();
} else {
camera = Camera.open(cameraId);
}
}
Mainfest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Make changes here
try to check:
hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)
Instead:
hasSystemFeature(PackageManager.FEATURE_CAMERA)
Nexus 7 having only front-facing camera.
Android camera selection algorithm defaults to the rear camera.
Related
I'm trying to create C++ Android native camera wrapper using the NDK camera2 API (from abi level 24). I created some snippet code using an example I found and compile it for target API level 24 and run it on Android 7.1 phone:
ACameraManager *cameraManager = ACameraManager_create();
VB(cameraManager!=nullptr, "Could not create CameraManager.");
camera_status = ACameraManager_getCameraIdList(cameraManager, &m_camera_id_list);
if (camera_status != ACAMERA_OK) {
LOGE("Failed to get camera id list (reason: %d)\n", camera_status);
return ERR_CAMERAAPI_UNKNOWN_ERROR;
}
if (m_camera_id_list->numCameras < 1) {
LOGE("No camera device detected.\n");
return ERR_CAMERAAPI_UNKNOWN_ERROR;
}
When I run this naive code on Xiaomi mi4c Android 7.1 phone I get an empty camera list.
I also tried to run on the same phone a snippet created with Java camera2 API that does the same thing:
import android.hardware.camera2.CameraDevice;
Activity activity = getActivity();
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
String[] cameraIds = manager.getCameraIdList()
manager.openCamera(cameraIds[0], mStateCallback, mBackgroundHandler);
This time I see in the logical that it actually finds two cameras and print their resolutions.
My manifest of course contains these lines:
<uses-sdk android:minSdkVersion="24" />
<uses-feature android:name="android.hardware.camera2" android:required="true" />
<uses-feature android:name="android.hardware.sensor.gyroscope" android:required="false" />
<uses-permission android:name="android.permission.CAMERA"/>
And I approve the permissions requests.
Does anyone knows why it finds the phone cameras when using the Java camera2 API but does not find them when using the NDK camera2 API?
The NDK camera2 support does not work if
CameraCharacteristics.get(INFO_SUPPORTED_HARDWARE_LEVEL) == INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY
This is probably the case of Xiaomi mi4c.
I'm trying to access the front facing camera through the Camera2 API using an actual device
the problem occurs when trying to
cameraManager.openCamera(..,..,..)
try {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraID);
if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Time out waiting to lock camera opening.");
}
manager.openCamera(mCameraID, mStateCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
Log.e("mr", "OpenCamera - Camera Access Exception");
} catch (IllegalArgumentException e) {
Log.e("mr", "OpenCamera - Illegal Argument Exception");
} catch (SecurityException e) {
e.printStackTrace();
Log.e("mr", "OpenCamera - Security Exception:");
} catch (InterruptedException e) {
Log.e("mr", "OpenCamera - Interrupted Exception");
}
the Cameramanager says
I/CameraManagerGlobal: Connecting to camera service
which is coming from this CameraManager.java function
private void connectCameraServiceLocked() {
// Only reconnect if necessary
if (mCameraService != null) return;
Log.i(TAG, "Connecting to camera service");
Then my GLSurfaceView remains blank
AndroidMainfest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
P.S:
It works with the back camera
It works with the nexus 6 API 23 emulator
Permissions are requested at runtime for Android M
I'm using GLSurfaceView as a viewfinder
The front camera was working perfectly on the same device using the normal CameraAPI with a TextureView
Tried to Uninstall/Reboot/Clear Cache
the device I'm using is a Sony Z5 premium
I'm using this example Render camera preview using OpenGL ES 2.0 on Android API 21 or higher
If you're just copying that example, I noticed it has the following line:
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
The front camera probably doesn't support that, since it likely has no flash. You need to check what AE modes are actually supported - the same goes for the various other settings the sample sets, where it isn't checking what the device actually supports.
That said, you should be getting an error if the value is bad, but it might be coming in on the CameraCaptureSession.CaptureListener.onCaptureFailed, and the sample is not listening to those. There's probably also some logcat from the camera service, if you look at all logging, not just your own app's logs.
But try removing that line, and see if it works.
I've ended with changing the Sony Xperia Z5 premium to another device and it worked perfectly.
it looks like that Sony is facing some problems with the new API as i saw in the following link:
Sony Camera2 API limitations
there might be a work around but anyway i'm making an application for specific purpose i don't want it to be working on all devices
First get Camera service like this
CameraManager cameraManager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
Here is what i have tried.
I have implemented Zbar Scanner in android application in which I can scan barocde and get result.
I have implemented this in my android project. now I want to implement scanner which scans images(of course Bar code images) from gallery. I know this can be possible anyhow. check this link. It has barcode image scanning.
I have tried to search it out but failed. Please Help me out.
This is possible now with the new Barcode Scanning Apis available from Google Play Services 7.8 version. It has method to detect barcode passed as a bitmap.
Get path of image from gallery and convert it to bitmap and pass it like below:
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context)
.build();
if(barcode.isOperational()){
SparseArray<Barcode> sparseArray = barcodeDetector.detect(frame);
if(sparseArray != null && sparseArray.size() > 0){
for (int i = 0; i < sparseArray.size(); i++){
Log.d(LOG_TAG, "Value: " + sparseArray.valueAt(i).rawValue + "----" + sparseArray.valueAt(i).displayValue);
Toast.makeText(LOG_TAG, sparseArray.valueAt(i).rawValue, Toast.LENGTH_SHORT).show();
}
}else {
Log.e(LOG_TAG,"SparseArray null or empty");
}
}else{
Log.e(LOG_TAG, "Detector dependencies are not yet downloaded");
}
In your build.gradle file, include the following under dependencies section:
compile 'com.google.android.gms:play-services:7.8.+'
Following Manifest permissions are must:
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Meta data for google play services:
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
Meta data for first time install/run time dependencies to be downloaded for getting barcode detector operational.
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="barcode" />
For detailed usage of this api, Refer Github Sample, follow Code Lab, Documentation.
I want to access extern usb cameras via v4l on android.
I tried SimpleWebCam. After some slight modifications of the original source codes, i achieved to make it work on a rooted android device. However, on unrooted devices, it keeps complaining about "not have permission to access "/dev/video*". I checked the permission of /dev/video* with "ls -l /dev/video*", and got
crw-rw---- system camera 81, 0 2015-08-18 18:31 video0
I understand that it means /dev/video* are owned by system, and are readable/writable to users in group "camera". So I think if i add
<uses-permission android:name="android.permission.CAMERA" />
in the manifest of my app, the user id of my app will be added to the group "camera", then my app will be allowed to read data from /dev/video*.
But, it still complains about "not have permission to access /dev/video*" now.
i also tried
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
, but still not working.
Do i miss somthing or misunderstand somthing. Any help or discussion will be appreciated.
The codes i used to open device are
int opendevice(int i)
{
struct stat st;
sprintf(dev_name,"/dev/video%d",i);
if (-1 == stat (dev_name, &st)) {
LOGE("Cannot identify '%s': %d, %s", dev_name, errno, strerror (errno));
return ERROR_LOCAL;
}
if (!S_ISCHR (st.st_mode)) {
LOGE("%s is no device", dev_name);
return ERROR_LOCAL;
}
fd = open (dev_name, O_RDWR);// | O_NONBLOCK, 0);
if (-1 == fd) {
LOGE("Cannot open '%s': %d, %s", dev_name, errno, strerror (errno));
return ERROR_LOCAL;
}
return SUCCESS_LOCAL;
}
The return value of open is always -1, with logcat:
Cannot open '/dev/video3': 13, Permission denied
I finally achieve to read images from the external usb camera on unrooted android devices using an opensource project named uvccamera.
Here is the link, https://github.com/saki4510t/UVCCamera
Try to add also
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
in your manifest file:
http://developer.android.com/reference/android/hardware/Camera.html
I am pretty confused where to add the
uses-feature
tag in the manifest.
I am using the camera in my app. I added permission but I'm confused where to add features in order to use front facing camera. Can you help?
Add this under <manifest> tag, like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lalllala">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-feature android:name="android.hardware.camera" />
<application android:icon="#drawable/icon" android:label="lalla" android:debuggable="true">
</application>
</manifest>
<uses-feature> - Declares a single hardware or software feature that is used by the application.
The purpose of a declaration is to inform any external entity of the set of hardware and software features on which your application depends. The element offers a required attribute that lets you specify whether your application requires and cannot function without the declared feature, or whether it prefers to have the feature but can function without it. Because feature support can vary across Android devices, the element serves an important role in letting an application describe the device-variable features that it uses.
read for more
Below is sample code to access Device Front Camera
public Camera openFrontFacingCamera() {
int cameraCount = 0;
Camera ffCam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
// Find the total number of cameras available
cameraCount = Camera.getNumberOfCameras();
// Find the ID of the CAMERA_FACING_FRONT & open it
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
ffCam = Camera.open(camIdx);
} catch (RuntimeException e) {
Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
return ffCam;
}
Need following permissions
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
For more please read Google android developer API doc Camera, Camera.CameraInfo
Add this under manifest tag:
<!-- Request the camera permission -->
<uses-permission
android:name="android.permission.CAMERA" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
write tags order like this
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application>
</application>
</manifest>