简体   繁体   中英

Get android device camera resolution

I am trying to get device camera maximum resolution using CameraManager in android.hardware.camera2 with StreamConfigurationMap getOutputSizes(ImageFormat.RAW_SENSOR)[0]. It is working on API's 22 and 24 but not above 25+. What is the way to get maximum camera resolution on newer android versions?

CameraManager manager = (CameraManager) getActivity().getSystemService(CAMERA_SERVICE);

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

                    StreamConfigurationMap streamConfigurationMap = chars.get(
                            CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
                    //Log.d(TAG, "onCreate: " + (streamConfigurationMap.getOutputSizes(ImageFormat.RAW_SENSOR)[0]));
                    String mp = streamConfigurationMap.getOutputSizes(ImageFormat.RAW_SENSOR)[0].toString();
                    String[] mpX = mp.split("x");
                    float x = Float.parseFloat(mpX[0]);
                    float y = Float.parseFloat(mpX[1]);
                    double xy = (double) (x * y) / 1000000;

               Log.d(TAG, "onCreate:" + Math.round(xy * 100.0) / 100.0); // here i get resolution in megapixels
                }
            } catch (CameraAccessException e) {
                e.printStackTrace();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }

If I use

ImageFormat.JPG

it gives me some values but they are not camera resolutions.

Try like this.

CameraManager cameraManager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);
for (final String cameraId : cameraManager.getCameraIdList()) {

    CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
    StreamConfigurationMap streamConfigurationMap = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
    Size[] sizes = streamConfigurationMap.getOutputSizes(ImageFormat.RAW_SENSOR);
    Log.d(TAG, "onCreate:" + getCameraMP(sizes);

}

private String getCameraMP(Size[] size) {
    String finalCameraRes = getMP(size[0], 1);
    int maxSize = size[0].getHeight() * size[0].getWidth();
    for (Size camSize : size) {
        int tempMax = camSize.getHeight() * camSize.getWidth();
        if (tempMax > maxSize) {
            maxSize = tempMax;
            finalCameraRes = getMP(camSize, 1);
        }
    }
    return finalCameraRes;
}

private String getMP(Size size, int decimalPlaces) {
    float mp = (size.getWidth() * size.getHeight()) / 1000000f;
    if (decimalPlaces == 1) {
        return String.format(Locale.US, "%.1f", mp) + " MP";
    } else if (decimalPlaces == 2) {
        return String.format(Locale.US, "%.2f", mp) + " MP";
    } else {
        return String.format(Locale.US, "%.2f", mp) + " MP";
    }
}

This will work on almost all Android 5.0+ devices even if the camera permission is not granted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM