开发者

Check if device has a camera?

In my app, I'd like to use the camera, if the device has one. Are there any devices running android that do not have a camera? By including the following into my manifest:

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

then 开发者_Python百科it's basically saying "I'll use a camera if one exists, but don't need one to run the app".

How could I check if a camera exists on the device, before attempting to use the Camera class?


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


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.


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();
}


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


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;
        }
    }


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");
          }


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


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


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)


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜