9
0
mirror of https://gitee.com/shuto/customCamera.git synced 2026-05-02 00:07:24 +08:00

Set the best resolution for the picture taken.

This commit is contained in:
Christophe BOUCAUT
2015-07-09 14:29:33 +02:00
parent 08773e2c24
commit 6a723fe9d5
2 changed files with 46 additions and 0 deletions
@@ -1017,6 +1017,20 @@ public class CameraActivity extends Activity {
Camera.Parameters camParameters = customCamera.getParameters();
camParameters.setPreviewSize(optimalSize.width, optimalSize.height);
customCamera.setParameters(camParameters);
setPictureSizeFromPreviewSize(optimalSize);
}
/**
* To set the picture size optimized on the preview size.
* @param Size previewSize
*/
private void setPictureSizeFromPreviewSize(Size previewSize) {
Size optimalPictureSize = ManagerCamera.getOptimalPictureSize(previewSize.width, previewSize.height);
if (optimalPictureSize != null) {
Camera.Parameters camParameters = customCamera.getParameters();
camParameters.setPictureSize(optimalPictureSize.width, optimalPictureSize.height);
customCamera.setParameters(camParameters);
}
}
/**
@@ -240,6 +240,38 @@ public class ManagerCamera {
}
}
}
return optimalSize;
}
/**
* Get the optimal picture size.
*
* @param int w Width of the wanted picture.
* @param int h Height of the wanted picture.
*
* @return Camera.Size Optimal resolution.
*/
public static Camera.Size getOptimalPictureSize(int w, int h) {
List<Camera.Size> pictureSizes = ManagerCamera.mCamera.getParameters().getSupportedPictureSizes();
if (pictureSizes == null) {
return null;
}
Camera.Size optimalSizePicture = null;
for (Camera.Size size : pictureSizes) {
if (
((float)size.width / (float)size.height) == ((float)w / (float)h) &&
(
optimalSizePicture == null ||
optimalSizePicture.width < size.width
)
) {
optimalSizePicture = size;
}
}
return optimalSizePicture;
}
}