Android photo, camera direction rotation problem code specific explanation

Source: Internet
Author: User
Tags getmessage

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.