以下是自定义相机遇到的问题:
1. 相机预览的方向
2. 相机预览时图像变形
3. 照片保存到本地的方向
最近刚好公司的业务需要开发遥控拍照的功能,最初想通过调用系统的相机来解决,不过最后始终没找到让系统相机自动拍照的方法, 故只能改用 自定义相机来开发。当然也就发现了相机开发中的坑。
1 解决相机预览方向的问题:
/**
* 保证预览方向正确
*
* @param activity
* @param cameraId
* @param camera
*/
public static void setCameraDisplayOrientation(Activity activity, int cameraId, Camera camera) {
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
2 解决相机预览变形的问题,相机在预览变形时肯定是因为 预览的宽高的比例 和 surfaceview设置的不一致
/**
* 设置Camera参数
*/
private void setCameraParameters() {
if (mCamera != null) {
mParameters = mCamera.getParameters();
List<Camera.Size> pictureSizeList = mParameters.getSupportedPictureSizes();
/* 从列表中选取合适的分辨率 */
Camera.Size picSize =