Check if device has a camera?

前端 未结 14 1184
粉色の甜心
粉色の甜心 2020-11-30 21:59

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 m

相关标签:
14条回答
  • 2020-11-30 22:15

    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.

    0 讨论(0)
  • 2020-11-30 22:17

    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_;
        }
    
    0 讨论(0)
  • 2020-11-30 22:22

    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");
              }
    
    0 讨论(0)
  • 2020-11-30 22:27

    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

    0 讨论(0)
  • 2020-11-30 22:29

    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();
    }
    
    0 讨论(0)
  • 2020-11-30 22:30

    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

    0 讨论(0)
提交回复
热议问题