Recently did a photo, camera application. Encountered the photo, camera image relative to the reality. Flipped 90 degrees. Reason: Camera This hardware angle is horizontal screen angle, so will appear are horizontal screen.
1. The correct angle of photographic and photographic preview image is displayed:
public static void setcameradisplayorientation (activity activity, int cameraid, Android.hardware.Camera cam ERA) {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; 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); }
The method of dynamic acquisition is landscape (horizontal screen) or portrait (vertical screen) to change the preview map.
(The cameraid of the rear screen is 0)
2. The correct angle of image preservation (when we take the photo, we find that the angle is not correct)
@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, N, FOS); Fos.close (); FileOutputStream fos = new FileOutputStream (picturefile);//Fos.write (data);//Fos.clo SE (); 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. The correct orientation of the video playback:
Mmediarecorder.setorientationhint (90);
This method is called before prepare ().
Android photo, camera direction rotation problem code specific explanation