检查设备是否有摄像头?

在我的应用程序中,我想使用相机,如果设备有一个。是否有任何运行安卓系统的设备,不要有一个摄像头?通过在我的清单中加入以下内容:

<uses-feature android:name="android.hardware.camera" android:required="false"/>

然后基本上就是说“如果有摄像头的话,我就用它,但不需要它来运行应用程序”。

在尝试使用 Camera 类之前,如何检查设备上是否存在摄像头?

56155 次浏览

I've not tried it, but:

private android.hardware.Camera mCameraDevice;


try {
mCameraDevice = android.hardware.Camera.open();
} catch (RuntimeException e) {
Log.e(TAG, "fail to connect Camera", e);
// Throw exception
}

May be what you need.

This is what I'm using

import android.content.pm.PackageManager;


PackageManager pm = context.getPackageManager();


if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
}

All sorts of other fun things to test for are available too - the compass, is location available, is there a front facing camera: http://developer.android.com/reference/android/content/pm/PackageManager.html

If you are using Android 2.3, there are some APIs that you can check your camera status, such as the number of cameras (front and back)

To find out how many cameras are available on your device, you can call:

import android.hardware.Camera;


int numCameras = Camera.getNumberOfCameras();
if (numCameras > 0) {
hasCamera = true;
}

Camera.getNumberOfCameras() is static, so it doesn't require actually connecting to a camera. This works since API 9.

Edit:

With the newer camera2 API, you can also call CameraManager.getCameraIdList(), which gives a list of the all the valid camera IDs, instead of just the count.

try this

For front camera

    Context context = this;
PackageManager packageManager = context.getPackageManager();
if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {


Utils.makeAlertDialog(context, "Has Front Camera ?", "YES");


} else {


Utils.makeAlertDialog(context, "Has Front Camera ?", "NO");
}

for back camera

    Context context = this;
PackageManager packageManager = context.getPackageManager();
if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {


Utils.makeAlertDialog(context, "Has back Camera ?", "YES");


} else {


Utils.makeAlertDialog(context, "Has back Camera ?", "NO");
}

you should use this to find camera in your device

public static boolean isCameraAvailable(Context context) {
return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}

Camera.getNumberOfCameras() is deprecated. You can use:

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public int getNumberOfCameras() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
return ((CameraManager) getSystemService(Context.CAMERA_SERVICE)).getCameraIdList().length;
} catch (CameraAccessException e) {
Log.e("", "", e);
}
}
return Camera.getNumberOfCameras();
}

Try this :

/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}

from : http://developer.android.com/guide/topics/media/camera.html

by following way we can check does device has camera or not.

/** Check if this device has a camera */
public static boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA))
{
return true;
}
else if(context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FRONT))
{
return true;
}
else {
return false;
}
}

Use the PackageManager.hasSystemFeature() method for checking Camera :

private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}

Source: https://developer.android.com/guide/topics/media/camera.html#custom-camera

As per the documentation, you have to use Package Manager to check if Camera is available on the device or not

In Java:

final boolean isCameraAvailable = getPackageManager().hasSystemFeature(FEATURE_CAMERA);

In Kotlin:

val isCameraAvailable = packageManager.hasSystemFeature(FEATURE_CAMERA)

I found in android tv boxes where you can plug and play usb camera a number of times. At some point of time, The camera service starts saying that it detected one camera in the system while no camera is connected to the system. This happens when you plug in/out the camera a number of times. To fix that, I found this solution working:

//under oncreate:
//cameraManager = ((CameraManager) getSystemService(Context.CAMERA_SERVICE));


public int getNumberOfCameras() {
int count_ = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
count_ = cameraManager.getCameraIdList().length;


if(count_==1)
{
try {
cameraManager.getCameraCharacteristics(cameraManager.getCameraIdList()[0]);
}catch (Exception e)
{
count_ = 0;
}
}


} catch (Exception e) {
//e.printStackTrace();
}
}
else {
count_ = Camera.getNumberOfCameras();
}


return count_;
}

One line solution:

public static boolean hasCamera(Context context) {
return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
}

Put this method in your Utils.java project class.

As per Android documentation :

/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}

Refer more about the camera API :
https://developer.android.com/guide/topics/media/camera.html#detect-camera

it is better to check ANY camera on the device since it could be external camera as well

packageManager.hasSystemFeature(FEATURE_CAMERA_ANY)

Documentation:

Feature for getSystemAvailableFeatures and hasSystemFeature: The device has at least one camera pointing in some direction, or can support an external camera being connected to it.