Android使用相机

这里介绍在Android相机相关开发中可能遇到的一些问题,和完整代码分享

  1. 设置预览和保存图像的方向
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//STEP #1: Get rotation degrees
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info);
int rotation = context.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break; //Natural orientation
case Surface.ROTATION_90: degrees = 90; break; //Landscape left
case Surface.ROTATION_180: degrees = 180; break;//Upside down
case Surface.ROTATION_270: degrees = 270; break;//Landscape right
}
int rotate = (info.orientation - degrees + 360) % 360;
//STEP #2: Set the 'rotation' parameter
Camera.Parameters params = camera.getParameters();
params.setRotation(rotate); //保存时的图像竖着
camera.setDisplayOrientation(rotate);//预览图像竖着
camera.setParameters(params);