Android: How can I check if a device has Camera2 api features implemented or not?

45,959

Solution 1

Actually, checking for API version 21+ will work. The camera2 API, including CameraManager is part of the system, not dependent on the hardware present. So you can always ask the CameraManager for a list of CameraDevices, which you can then query individually.

However, I think what you actually mean is "how can I tell if I can set photographic parameters manually using the camera2 API?", which is dependent on the device you have. It depends on what control you need, but the information you need can be gleaned by getting the REQUEST_AVAILABLE_CAPABILITIES metadata field. Hint: look for MANUAL_SENSOR.

Solution 2

Indeed, the camera2 api is only supported from API level 21. But only this check is not enough. There are devices with API level 21, but that support the camera 2 only partially. To check this, you should check the value of CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL. It can be FULL, LEGACY or LIMITED. Check here for details: https://developer.android.com/reference/android/hardware/camera2/CameraCharacteristics.html

Here is how to get it:

CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);

for (String cameraId : manager.getCameraIdList()) {
                    CameraCharacteristics characteristics
                            = manager.getCameraCharacteristics(cameraId);


    Log.d("Img", "INFO_SUPPORTED_HARDWARE_LEVEL " + characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL));
 }

Solution 3

I also needed this for another project, so I wrote a small app that probes all camera2 features and shows which ones are available on the phone: https://play.google.com/store/apps/details?id=de.weis.camera2probe

You can email this report in-app. I list all reports that I received here: https://github.com/TobiasWeis/android-camera2probe/wiki (The code of the app is also available there, in case someone needs to integrate into their own project)

Solution 4

In cases if someone needs full snippet of how to determine which camera in the device has Camera2 API support (at least limited support):

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public boolean allowCamera2Support(int cameraId) {
        CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
        try {
            String cameraIdS = manager.getCameraIdList()[cameraId];
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraIdS);
            int support = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);

                if( support == CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY )
                    Log.d(TAG, "Camera " + cameraId + " has LEGACY Camera2 support");
                else if( support == CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED )
                    Log.d(TAG, "Camera " + cameraId + " has LIMITED Camera2 support");
                else if( support == CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_FULL )
                    Log.d(TAG, "Camera " + cameraId + " has FULL Camera2 support");
                else
                    Log.d(TAG, "Camera " + cameraId + " has unknown Camera2 support?!");

            return support == CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED || support == CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_FULL;
        }
        catch (CameraAccessException e) {
            e.printStackTrace();
        }
        return false;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void checkCamera2Support() {
        if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
            int numberOfCameras = 0;
            CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);

            try {
                numberOfCameras =  manager.getCameraIdList().length;
            } catch (CameraAccessException e) {
                e.printStackTrace();
            } catch(AssertionError e) {
                e.printStackTrace();
            }

            if( numberOfCameras == 0 ) {
                    Log.d(TAG, "0 cameras");
            }else {
                for (int i = 0; i < numberOfCameras; i++) {
                    if (!allowCamera2Support(i)) {
                        Log.d(TAG, "camera " + i + " doesn't have limited or full support for Camera2 API");
                    }else{
                        // here you can get ids of cameras that have limited or full support for Camera2 API
                    }
                }
            }
        }
    }
Share:
45,959
Vivek Sasidharan
Author by

Vivek Sasidharan

Software engineer at CareStack.

Updated on June 15, 2020

Comments

  • Vivek Sasidharan
    Vivek Sasidharan about 4 years

    Well, how can I check if an android device has Camera2 api features implemented or not? There are many new features in camera2 api such as manual controls. So how can I know whether, which Camera2 api features are implemented or not, programmatically?

  • Vivek Sasidharan
    Vivek Sasidharan about 9 years
    What I meant is a way to check whether a lollipop device support Camera2 api or not, since there are lollipop devices without Camera2 api implemented. So calling CameraManager on such a device having Camera2 api not implemented might cause crashes. You see, I am not talking about hardware support, it's whether the device has Camera2 api implemented or not. I think my question redirects you to hardware part so I am editing it. Sorry for the mistake.
  • rcsumner
    rcsumner about 9 years
    In that case, the first sentence of my answer is all you should need.
  • Vivek Sasidharan
    Vivek Sasidharan about 9 years
    But is CameraManager part of Camera2 api? Then how can I use it on a device which does not have the api implemented?
  • rcsumner
    rcsumner about 9 years
    True, CameraManager is part of camera2. But camera2 is a part of Android Lollipop, API 21 and newer. Checking the existence of API 21+ effectively checks for the existence of camera2 which effectively checks for the existence of CameraManager. So all you have to check is the API level.
  • Vivek Sasidharan
    Vivek Sasidharan about 9 years
    But I have heard about lollipop devices that does not have camera2 api implemented. so checking for API level would be inefficient, is it?
  • Vivek Sasidharan
    Vivek Sasidharan about 9 years
  • rcsumner
    rcsumner about 9 years
    This Reddit thread is addressing the second issue I described above, that not many manufacturers actually enable manual control. However, EVERY API 21+ device does implement the software level calls to camera2 classes. Trust me on this, they aren't saying in that thread that an exception will be raised by an app for trying to call CameraManager (as long as it is lollipop, of course).
  • Vivek Sasidharan
    Vivek Sasidharan about 9 years
    Ok now I get it! It was a bit confusing at first.But, Thanks for helping me out :)
  • Vivek Sasidharan
    Vivek Sasidharan about 9 years
    I have edited the question to more align to your answer and avoid confusion. Thanks again for helping me out :)
  • TobiasWeis
    TobiasWeis over 7 years
    This app does not seem to really probe each displayed features availability. On my S5 Neo it shows no manual control at all, but I am able to set fixed focus and white balance through the camera2 api.
  • Akeshwar Jha
    Akeshwar Jha over 7 years
    Using the code above, we get some numeric value. So, for the reference, the numeric values for LEGACY, LIMITED, FULL, and LEVEL_3 are 2, 0, 1, and 3, respectively.
  • Scampbell
    Scampbell almost 7 years
    There are a ton of other reviews complaining about the same problem with that app. It seems not trustworthy.
  • JCutting8
    JCutting8 over 5 years
    I get different results for the front and back camera. I get FULL for front camera, and LIMITED for back camera. Not sure what to do with this information really because I want to use Camera2 for newer phones that support it, but default to the old Camera API for phones that don't support it.