Android 自定义相机 Camera 图片方向问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lamphogani/article/details/79197015

相机预览方向问题


对于相机的预览方向我们可以通过如下API进行设置

camera.setDisplayOrientation(0);
但是,该API影响的是相机的预览方向,对于照片的保存方向并没有什么影响,最终照片保存的方向还是由Camera的图像Sensor决定的。


照片保存方向问题


第一种解决办法就是对拍照后的图片先进行旋转再进行保存,如下

public static Bitmap rotateBitmapByDegree(Bitmap bmp, int degree) {
    if (degree == 0 || null == bmp) return bmp;
    Bitmap returnBm = null;
    // 根据旋转角度,生成旋转矩阵
    Matrix matrix = new Matrix();
    matrix.postRotate(degree);
    // 将原始图片按照旋转矩阵进行旋转,并得到新的图片
    returnBm = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
    if (returnBm == null) {
        returnBm = bmp;
    }
    if (returnBm != bmp && !bmp.isRecycled()) {
        bmp.recycle();
        bmp = null;
    }
    return returnBm;
}
但是这种方法比较耗时,尤其图片较大的时候,不建议使用。


第二种方法就是通过ExifInterface对图片进行旋转,如下

public static void setPictureDegreeZero(String path) {
    try {
        ExifInterface exifInterface = new ExifInterface(path);
        // 修正图片的旋转角度,设置其不旋转。这里也可以设置其旋转的角度,可以传值过去,
        // 例如旋转90度,传值ExifInterface.ORIENTATION_ROTATE_90,需要将这个值转换为String类型的
        exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(ExifInterface.ORIENTATION_ROTATE_90))
        exifInterface.saveAttributes();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
这种方法一定要在保存完图片之后再使用,否则不会生效。最终的效果就是你拍照的时候什么方向,保存到本地的照片就是什么方向。


第三种方法就是通过OrientationEventListener实现对图片保存方向的纠正,如下

public class IOrientationEventListener extends OrientationEventListener {

	public IOrientationEventListener(Context context) {
		super(context);
	}

	@Override
	public void onOrientationChanged(int orientation) {
		if (ORIENTATION_UNKNOWN == orientation) {
			return;
		}
		Camera.CameraInfo info = new Camera.CameraInfo();
		Camera.getCameraInfo(mCameraId, info);
		orientation = (orientation + 45) / 90 * 90;
		int rotation = 0;
		if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
			rotation = (info.orientation - orientation + 360) % 360;
		} else {
			rotation = (info.orientation + orientation) % 360;
		}
        Log.e("TAG","orientation: " + orientation);
		if (null != mCamera) {
			Camera.Parameters parameters = mCamera.getParameters();
			parameters.setRotation(rotation);
			mCamera.setParameters(parameters);
		}
	}
}

备注:后置摄像头cameraId=0;


final IOrientationEventListener orientationEventListener = new IOrientationEventListener(this);
surfaceView.getHolder().setKeepScreenOn(true);//屏幕常亮
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
	//当SurfaceHolder被创建的时候回调
	@Override
	public void surfaceCreated(SurfaceHolder surfaceHolder) {
		orientationEventListener.enable();
		......
	}

	//当SurfaceHolder的尺寸发生变化的时候被回调
	@Override
	public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
		......
	}

	//当SurfaceHolder被销毁的时候回调
	@Override
	public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
		orientationEventListener.disable();
		......
	}
});
这种方法最终的效果就是不管你拍照的时候是什么方向,最终保存到本地的照片都是0度方向的,跟系统相机拍照后一个效果。

猜你喜欢

转载自blog.csdn.net/Lamphogani/article/details/79197015