近期做了个拍照、摄像的应用。遇到了拍照、摄像的图像相对于现实。翻转了90度。原因:相机这个硬件的角度是横屏的角度,所以会出现都是横屏的。
1.照相、摄影预览图像的正确角度显 示:
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);
}
改方法动态获取手机是Landscape(横屏)或Portrait(竖屏)来更改预览图。
(后屏的cameraId是0 )
2.图像保存的正确角度(当我们拍下照片,发现角度又不正确)
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length);
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(0, info);
Bitmap bitmap = rotate(realImage, info.orientation); FileOutputStream fos = new FileOutputStream(pictureFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close(); // FileOutputStream fos = new FileOutputStream(pictureFile);
// fos.write(data);
// fos.close(); DBRecordAdapter.getInstance().insertRecord(bean);
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
3.录像回放的正确的方向:
mMediaRecorder.setOrientationHint(90);
该方法在prepare()之前调用。