Android Development Practice: Mastering the camera's preview direction and photo orientation

Source: Internet
Author: User

Android Camera related application development, there is a need to clear the knowledge point is the camera's preview direction and the direction of photography, this article focused on this issue.


The sensor direction of the image : The image data of the mobile camera is from the camera hardware image sensor, the sensor is pinned to the phone after a default framing direction, as shown in this direction, The coordinate origin is located in the upper-left corner of the phone when it is horizontal:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/57/40/wKiom1SWRlPS8A37AABKPmT4jEM721.jpg "title=" 2.png " alt= "Wkiom1swrlps8a37aabkpmt4jem721.jpg"/>


Camera Preview Direction : Since the phone screen can be rotated 360 degrees, in order to ensure that the user can see the "correct" preview screen no matter how the phone is rotated (this "correct" means that the screen displayed in the UI preview interface is consistent with what you see in front of your eyes), Android system bottom based on the current phone screen direction of the image sensor data collected by the rotation processing, and then sent to the display system, so after you open the camera app, no matter how to rotate the phone, you can see the "correct" picture, The Android system provides an API to manually set the camera's preview direction, called Setdisplayorientation, which, by default, is 0, consistent with the image sensor direction, so for a horizontal screen application, You do not need to change this camera preview direction. However, if your application is a vertical screen, you must rotate the camera's preview direction by 90 through this API to match the screen orientation of your phone so that you get the correct preview screen.


Camera Orientation : When you click the Take photo button, the direction of the picture is not necessarily the same as the preview in the picture, because the photo taken is stored directly on the sdcard of the image sensor, so Camera orientation is the same as the image sensor of the camera above.


To demonstrate the problem, I took two photos of the same scene with my phone's camera, the first one was taken sideways, and the second was taken by a cellphone. Then use the picture that is opened on the computer (the cup in the actual scene is vertical), the effect is as follows:


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/57/3F/wKiom1SWJRTi0PqOAAG0VYlk6aQ451.jpg "title=" 1.png " alt= "Wkiom1swjrti0pqoaag0vylk6aq451.jpg"/>


Thus, if the horizontal take the mobile phone photo, because it is exactly the same direction as the camera, so the picture is "correct", and the portrait of a mobile phone camera, the camera's image sensor is still described above the angle in the collection of images and stored on the SDcard, So the picture is the right image, because the vertical to take the phone is exactly the same as the direction of the image sensor 90 degrees. As a result, you should understand why the photos we take with our mobile phones often need to rotate 90 degrees to see the "right" picture, right?


I think the above introduction should have already explained this problem clearly, below I also want to go deeper, introduce the camera preview direction of the API (Setdisplayorientation).


It says that there is no additional setting for horizontal screen applications, but for portrait applications you need to call Setdisplayorientation (90) to ensure that the camera's preview direction is the same as the activity's direction. So does setting up this function really affect the results of camera photography? According to the above analysis, the theory should not affect, because the picture is taken in the direction of the image sensor is consistent with the direction, of course, we can use the official Android API annotated document to verify this conjecture, The following is an annotated document for Camera.setdisplayorientation:


/** * set the clockwise rotation of preview display in  degrees. this affects * the preview frames and the picture  Displayed after snapshot. this method * is useful for portrait  mode applications. note that preview display of * front-facing  cameras is flipped horizontally before the rotation, that *  is, the image is reflected along the central vertical axis  of the camera * sensor. So the users can see  themselves as looking into a mirror. * * <p>this does  not affect the order of byte array passed in {@link  *  previewcallback#onpreviewframe},  jpeg pictures, or recorded videos. this * method is not  allowed to be called during preview.      */  Public native final void setdisplayorientation (int degrees);


Focus on these two words:


This affects the preview frames and the picture displayed after snapshot. This does isn't affect the order of byte array passed in {@link * previewcallback#onpreviewframe}, JPEG pictures, or Recorde D videos.


As a result, this API modifies only the camera's preview direction, and does not affect the direction of the Previewcallback callback, the resulting JPEG image, and the video file, which will still be in the same direction as the image sensor.


About the preview of the Android camera and the orientation of the photo is here, I hope that the camera application development of the Novice can be helpful, have any questions welcome message or letter [email protected] exchange.


This article is from the "Shadow Three People" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/1592267

Android Development Practice: Mastering the camera's preview direction and photo orientation

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.