Android+Camera+拍照

本次只說拍攝照片,不涉及視頻

先從這裏</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-facing
			result = (info.orientation - degrees + 360) % 360;
		}
		camera.setDisplayOrientation(result);
	}

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


在保存圖片時,需要再按照上述方法計算出需要旋轉的角度,手動將得到的圖片旋轉

另外,使用前置攝像頭拍攝的照片,旋轉照片的角度不能直接使用預覽時算出的角度

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章