Check If Device Has a Camera

How do you detect if an Android device has a camera?

After some research, it appears that this is a known bug, which has been unresolved for 10+ years now.

See: Emulator does not honour Camera support flag

It seems that the only know work-around is to detect the number of cameras using the Camera.getNumberOfCameras() method, which has been deprecated since Android 5.0.

boolean hasCamera = Camera.getNumberOfCameras() > 0;
Log.i(LOG_TAG, "hasCamera = " + hasCamera);

The above method seems to work on my emulator, despite the warning that it is deprecated.


Now according to the docs for Camera.getNumberOfCameras():

"The return value of [getNumberOfCameras()] might change dynamically if the device supports external cameras and an external camera is connected or disconnected."

So perhaps hasSystemFeature() always returns true, because the device might later have an external camera attached?

How to detect if front camera is available in android?

Use this method to detect whether devices has a front camera or not.

private boolean hasFrontCamera() {
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
return true;
}
}
return false;
}

Android/Java: detect if device has a BACK Camera

For API >= 9 : you can use Camera.getCameraInfo with something like this :

int backCameraId = -1;
for(int i=0;i<Camera.getNumberOfCameras();i++){
CameraInfo cameraInfo = new CameraInfo();
Camera.getCameraInfo(i,cameraInfo);
if(cameraInfo.facing==CameraInfo.CAMERA_FACING_BACK) {
backCameraId = i;
break;
}
}
Log.d(TAG, "back camera exists ? "+(backCameraId>-1));
Log.d(TAG, "back camera id :"+backCameraId);

For API >= 21, you are advised to use the Camera2 API :

String backCameraId = null;
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
for(String cameraId:manager.getCameraIdList()){
CameraCharacteristics cameraCharacteristics = manager.getCameraCharacteristics(cameraId);
Integer facing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
if(facing==CameraMetadata.LENS_FACING_BACK) {
backCameraId = cameraId;
break;
}
}
Log.d(TAG, "back camera exists ? "+(backCameraId!=null));
Log.d(TAG, "back camera id :"+backCameraId);

Check if device has camera in cross platform way

the Media plugin has this API

CrossMedia.Current.IsCameraAvailable

Best way to check if Camera exists on Android device?

Try this:

    import android.content.pm.PackageManager;
PackageManager pm = context.getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
bool device_has_camera = True
}

Is it possible to check if the user has a camera and microphone and if the permissions have been granted with Javascript?

Live Demo:

  • https://www.webrtc-experiment.com/DetectRTC/

If user didn't allow webcam and/or microphone, then media-devices will be having "NULL" value for the "label" attribute. Above page will show this message: "Please invoke getUserMedia once."

PS. You can type "DetectRTC.MediaDevices" in the Chrome Console developers tool.

Note: It works only in Chrome. Firefox isn't supporting similar API yet. (Updated: Firefox supports as well)

Updated at Dec 16, 2015

Note: Following code snippet works both in Chrome and Firefox.

if (navigator.mediaDevices && navigator.mediaDevices.enumerateDevices) {
// Firefox 38+ seems having support of enumerateDevicesx
navigator.enumerateDevices = function(callback) {
navigator.mediaDevices.enumerateDevices().then(callback);
};
}

var MediaDevices = [];
var isHTTPs = location.protocol === 'https:';
var canEnumerate = false;

if (typeof MediaStreamTrack !== 'undefined' && 'getSources' in MediaStreamTrack) {
canEnumerate = true;
} else if (navigator.mediaDevices && !!navigator.mediaDevices.enumerateDevices) {
canEnumerate = true;
}

var hasMicrophone = false;
var hasSpeakers = false;
var hasWebcam = false;

var isMicrophoneAlreadyCaptured = false;
var isWebcamAlreadyCaptured = false;

function checkDeviceSupport(callback) {
if (!canEnumerate) {
return;
}

if (!navigator.enumerateDevices && window.MediaStreamTrack && window.MediaStreamTrack.getSources) {
navigator.enumerateDevices = window.MediaStreamTrack.getSources.bind(window.MediaStreamTrack);
}

if (!navigator.enumerateDevices && navigator.enumerateDevices) {
navigator.enumerateDevices = navigator.enumerateDevices.bind(navigator);
}

if (!navigator.enumerateDevices) {
if (callback) {
callback();
}
return;
}

MediaDevices = [];
navigator.enumerateDevices(function(devices) {
devices.forEach(function(_device) {
var device = {};
for (var d in _device) {
device[d] = _device[d];
}

if (device.kind === 'audio') {
device.kind = 'audioinput';
}

if (device.kind === 'video') {
device.kind = 'videoinput';
}

var skip;
MediaDevices.forEach(function(d) {
if (d.id === device.id && d.kind === device.kind) {
skip = true;
}
});

if (skip) {
return;
}

if (!device.deviceId) {
device.deviceId = device.id;
}

if (!device.id) {
device.id = device.deviceId;
}

if (!device.label) {
device.label = 'Please invoke getUserMedia once.';
if (!isHTTPs) {
device.label = 'HTTPs is required to get label of this ' + device.kind + ' device.';
}
} else {
if (device.kind === 'videoinput' && !isWebcamAlreadyCaptured) {
isWebcamAlreadyCaptured = true;
}

if (device.kind === 'audioinput' && !isMicrophoneAlreadyCaptured) {
isMicrophoneAlreadyCaptured = true;
}
}

if (device.kind === 'audioinput') {
hasMicrophone = true;
}

if (device.kind === 'audiooutput') {
hasSpeakers = true;
}

if (device.kind === 'videoinput') {
hasWebcam = true;
}

// there is no 'videoouput' in the spec.

MediaDevices.push(device);
});

if (callback) {
callback();
}
});
}

// check for microphone/camera support!
checkDeviceSupport(function() {
document.write('hasWebCam: ', hasWebcam, '<br>');
document.write('hasMicrophone: ', hasMicrophone, '<br>');
document.write('isMicrophoneAlreadyCaptured: ', isMicrophoneAlreadyCaptured, '<br>');
document.write('isWebcamAlreadyCaptured: ', isWebcamAlreadyCaptured, '<br>');
});


Related Topics



Leave a reply



Submit