Title, it is believed that many developers, when they call the system camera to receive the photos taken, found that the photos were rotated 90 degrees without reason. The problem is reflected in most of the Samsung phones, and of course the other machines have not yet been fully tested, in short there are problems.
The following solutions are thought to be:
- Identify the model, get the phone model, and then determine if you need to rotate;
- Determine if rotation is required by obtaining the width and height of the photo;
- Write a camera activity yourself to get camera data, save photos;
- Analyze the rotated photo data, dynamically determine if rotation is required, and rotate the correct angle.
The first three methods are not ideal solutions.
First of all say 1: This is similar to the poor way, there will always be forgotten "corner", maintenance of these models would be very cumbersome;
Then say 2: If so, if just the user to horizontal screen photos, or will occur rotation;
Finally say 3: This method is I wait "lazy" programmer can do? (PS: In fact, the demand is limited, their own photo app, time-consuming + energy + easy to make a bug. The benefit is that users will feel they are always in the same app, not jump. )
So how does the fourth approach work?
First through analysis, usually a picture contains many EXIF information. This information includes the equipment to be photographed, the exposure, and whether or not the Flash is used in various contents. This includes the angle, which is the crux of the problem of rotation.
The phone's own photo viewer (some models) analyzes this information when it is displayed, and automatically rotates the photo to the appropriate angle when the information is analyzed. The more common angles are 90, 180, 270. Most Samsung phones take pictures with a 90-degree angle at the vertical screen and 0 degrees across the screen. Therefore, the solution of the idea is to follow the EXIF angle information to rotate. Mainly used in the Android API matrix. The key code can refer to the following code slice:
ExifInterface exifInterface = new ExifInterface(APPCFG.IMAGE_FILE); int orientationDegree = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); Matrix matrix = new Matrix(); matrix.reset(); if (orientationDegree == ExifInterface.ORIENTATION_ROTATE_90) { matrix.postRotate(90); } if (orientationDegree == ExifInterface.ORIENTATION_ROTATE_180) { matrix.postRotate(180); } if (orientationDegree == ExifInterface.ORIENTATION_ROTATE_270) { matrix.postRotate(270); } bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, true);
In the above code, bitmap is the bitmap object, I need to show in the back here, so there is no recycle (), remind everyone to avoid memory overflow.
In this way, it is possible to solve the problem of photo rotation by using this universal method, whether it is Samsung or any other model.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Repair of photo rotation problems caused by calling system camera