android 自动相机 图片旋转问题,Android部分手机拍照后获取的图片被旋转问题的解决方法...
时间: 2024-03-10 12:45:07 浏览: 26
Android相机拍摄的图片可能会因为设备方向的不同而导致旋转。这是因为Android相机的预览和拍照方向并不一定相同。解决这个问题的方法如下:
1. 读取图片的 Exif 信息,获取图片的方向信息。Exif 信息是一些存储在图片中的元数据,包括拍摄地点、相机型号、拍摄时间等。其中也包括了图片的方向信息。
2. 根据 Exif 中的方向信息,将图片进行旋转。可以使用 Bitmap 类的 createBitmap() 方法创建一个新的 Bitmap,并将原始图片旋转后绘制到新的 Bitmap 上。
以下是示例代码:
```
public static Bitmap rotateBitmap(Bitmap bitmap, String path) {
int degree = getBitmapDegree(path);
if (degree != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return rotatedBitmap;
} else {
return bitmap;
}
}
public static int getBitmapDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
```
在使用相机拍照后获取图片的时候,可以调用该方法对图片进行旋转,以得到正确的方向。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)