Android+Camera+拍照

来源:互联网 发布:淘宝如何提高权重 编辑:程序博客网 时间:2024/04/24 21:36

本次只说拍摄照片,不涉及视频

先从这里</sdk/docs/guide/topics/media/camera.html>根据guide中的代码,配置权限,并打开摄像头,在手机上成功实现预览。

预览时可能会发现预览窗口中的景象与实际方向不符(Camera.CameraInfo.orientation=270),原因是摄像头默认使用水平朝向。

通过这个方法来实现摄像头方向与手机方向一致

public static void setCameraDisplayOrientation(Activity activity,int cameraId, android.hardware.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-facingresult = (info.orientation - degrees + 360) % 360;}camera.setDisplayOrientation(result);}

记得在Activity onPause事件中release掉Camera。


在保存图片时,需要再按照上述方法计算出需要旋转的角度,手动将得到的图片旋转

另外,使用前置摄像头拍摄的照片,旋转照片的角度不能直接使用预览时算出的角度

0 0