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

前端 未结 7 1029
孤独总比滥情好
孤独总比滥情好 2020-12-06 10:55

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

相关标签:
7条回答
  • 2020-12-06 11:32

    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
                        }
                    }
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题