Commit 64a05ce5 by amateur

图片旋转问题

1 parent 11e980c0
...@@ -4,6 +4,8 @@ import android.content.ContentResolver; ...@@ -4,6 +4,8 @@ import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri; import android.net.Uri;
import android.util.Base64; import android.util.Base64;
...@@ -13,6 +15,7 @@ import java.io.FileNotFoundException; ...@@ -13,6 +15,7 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Objects;
/** /**
* Bitmap 工具类 * Bitmap 工具类
...@@ -65,7 +68,7 @@ public class BitmapUtils { ...@@ -65,7 +68,7 @@ public class BitmapUtils {
File file = new File(filepath); File file = new File(filepath);
String newName = "temp_" + file.getName(); String newName = "temp_" + file.getName();
String newPath = new File(file.getParent(), newName).getAbsolutePath(); String newPath = new File(file.getParent(), newName).getAbsolutePath();
return compressBitmap(filepath, newPath,95); return compressBitmap(filepath, newPath, 95);
} }
public static File compressBitmap(String filepath, String newPath) { public static File compressBitmap(String filepath, String newPath) {
...@@ -86,7 +89,9 @@ public class BitmapUtils { ...@@ -86,7 +89,9 @@ public class BitmapUtils {
} }
File newFile = new File(newPath); File newFile = new File(newPath);
if (null != newFile.getParentFile()) {
newFile.getParentFile().mkdirs(); newFile.getParentFile().mkdirs();
}
if (newFile.exists()) { if (newFile.exists()) {
newFile.delete(); newFile.delete();
} }
...@@ -120,9 +125,9 @@ public class BitmapUtils { ...@@ -120,9 +125,9 @@ public class BitmapUtils {
if (scale <= 1 && scale > 0.5625) { if (scale <= 1 && scale > 0.5625) {
if (longSide < 1664) { if (longSide < 1664) {
return 1; return 1;
} else if (longSide >= 1664 && longSide < 4990) { } else if (longSide < 4990) {
return 2; return 2;
} else if (longSide > 4990 && longSide < 10240) { } else if (longSide < 10240) {
return 4; return 4;
} else { } else {
return longSide / 1280 == 0 ? 1 : longSide / 1280; return longSide / 1280 == 0 ? 1 : longSide / 1280;
...@@ -152,6 +157,7 @@ public class BitmapUtils { ...@@ -152,6 +157,7 @@ public class BitmapUtils {
* @param quality 质量 * @param quality 质量
*/ */
public static void compress(String filePath, String cacheFilePath, int sampleSize, int quality) { public static void compress(String filePath, String cacheFilePath, int sampleSize, int quality) {
int degree = readPictureDegree(filePath);
//获取bitmap数据 //获取bitmap数据
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565; bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
...@@ -159,6 +165,13 @@ public class BitmapUtils { ...@@ -159,6 +165,13 @@ public class BitmapUtils {
bitmapOptions.inSampleSize = sampleSize; bitmapOptions.inSampleSize = sampleSize;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, bitmapOptions); Bitmap bitmap = BitmapFactory.decodeFile(filePath, bitmapOptions);
if (bitmap != null) { if (bitmap != null) {
if (degree > 0) {
Bitmap tempBitmap = bitmap;
Matrix matrix = new Matrix();
matrix.postRotate(degree);
bitmap = Bitmap.createBitmap(tempBitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
tempBitmap.recycle();
}
writeToFile(bitmap, cacheFilePath, Bitmap.CompressFormat.JPEG, quality); writeToFile(bitmap, cacheFilePath, Bitmap.CompressFormat.JPEG, quality);
bitmap.recycle(); bitmap.recycle();
} }
...@@ -183,7 +196,7 @@ public class BitmapUtils { ...@@ -183,7 +196,7 @@ public class BitmapUtils {
File dir = new File(cacheFilePath); File dir = new File(cacheFilePath);
File parent = dir.getParentFile(); File parent = dir.getParentFile();
LogUtil.i(TAG, "尝试将图片写入文件:" + dir); LogUtil.i(TAG, "尝试将图片写入文件:" + dir);
if (!parent.exists()) { if (null != parent && !parent.exists()) {
boolean succeed = parent.mkdirs(); boolean succeed = parent.mkdirs();
LogUtil.i(TAG, "文件上级目录不存在,准备创建成功:" + succeed); LogUtil.i(TAG, "文件上级目录不存在,准备创建成功:" + succeed);
} }
...@@ -476,4 +489,32 @@ public class BitmapUtils { ...@@ -476,4 +489,32 @@ public class BitmapUtils {
return bitmap; return bitmap;
} }
/**
* 读取照片旋转角度
*
* @param path 照片路径
* @return 角度
*/
public static int readPictureDegree(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;
}
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!