以编程方式查找相机的分辨率
问题描述:
我想弄清楚我的应用程序中的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。你能帮我解决相机的确切分辨率吗?
答
我认为你不应该除以1024000
而是1000000
。这里我们谈的是Mega Pixels
而不是Mega Bytes
使用等于1 Megabyte = 1,048,576 Bytes
而是1 Megapixel = 1,000,000 Pixels
。加1024000
是错的,它应该是1048576
或2^20
。除以1000000
将给你一个更接近16MP的数字。
可能重复的[如何获得真正的相机最大百万像素的设备?](https://stackoverflow.com/questions/25590721/how-to-get-real-camera-max-megapixels-of-a-设备) –