以编程方式查找相机的分辨率

时间:2017-09-08 07:24:41

标签: android camera android-camera resolution

我想在我的应用程序中弄清楚Android手机的分辨率 我正在使用

public float getBackCameraResolutionInMp() {

    try {
        int noOfCameras = Camera.getNumberOfCameras();
        float maxResolution = -1;
        long pixelCount = -1;
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        Camera.getCameraInfo(BACK_CAMERA_ID, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
            try {
                releaseCameraAndPreview();
                if (camera == null) {
                    camera = Camera.open(BACK_CAMERA_ID);
                }
                Camera.Parameters cameraParams = camera.getParameters();
                for (int j = 0; j < cameraParams.getSupportedPictureSizes().size(); j++) {
                    long pixelCountTemp = cameraParams.getSupportedPictureSizes().get(j).width * cameraParams.getSupportedPictureSizes().get(j).height; // Just changed i to j in this loop
                    if (pixelCountTemp > pixelCount) {
                        pixelCount = pixelCountTemp;
                        maxResolution = ((float) pixelCountTemp) / (1024000.0f);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return maxResolution;
    } catch (Exception e) {
        logException(e, "CameraInfoFragment_getBackCameraResolutionInMp()");
        return -1;
    }

}

但是它给我的大致分辨率并不准确。就像分辨率是16MP一样,它返回15.55 MP。你能帮我解决一下如何确定相机的精确分辨率吗?

1 个答案:

答案 0 :(得分:0)

我认为你不应该用1024000除以1000000。在这里,我们讨论Mega Pixels而非Mega Bytes使用等同1 Megabyte = 1,048,576 Bytes1 Megapixel = 1,000,000 Pixels。加1024000错误,应该是10485762^20。除以1000000将使您的数字更接近16MP。