三星拍照后,图片竟然是旋转的,众多手机唯有三星拍照是旋转的,但是你又不能不处理。
首先你要先判断这张图片有木有旋转,图片都是自带这些信息的,方法如下:
/**
* 读取图片属性:旋转的角度
*
* @param path 图片绝对路径
* @return degree旋转的角度
*/
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;
}
当然三星手机拍照一般都是:
degree = 90;
如果degree != 0 ,就说明图片是旋转的。假设我们需要发送一张图片,不可能对原来路径下的图片进行修改,所以需要把要处理的图片在复制一份再进行处理;方法如下:
/**
* 复制文件
*
* @param oldPath
* @param newPath
*/
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
最后再对相应的图片做旋转处理,先贴上图片旋转的处理方法:
/**
* 旋转图片
*
* @param angle
* @param bitmap
* @return Bitmap
*/
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
// 旋转图片 动作
Matrix matrix = new Matrix();
matrix.postRotate(angle);
// 创建新的图片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizedBitmap;
}
这里传入相应的角度和bitmap就行,最后贴上图片旋转处理的全部代码:
/**
* 旋转的图片生成正的
*
* @param path
*/
public static void rotaingDegreeImage(String path) {
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
//获取宽高度
bitmapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, bitmapOptions);
/**
* 计算sampleSize最优值
*/
bitmapOptions.inSampleSize = computeSampleSize(bitmapOptions, -1, 2048 * 1536);
bitmapOptions.inJustDecodeBounds = false;
int degree = readPictureDegree(path);
Bitmap cameraBitmap = BitmapFactory.decodeFile(path, bitmapOptions);
/**
* 把图片旋转为正的方向
*/
cameraBitmap = rotaingImageView(degree, cameraBitmap);
FileOutputStream out;
try {
out = new FileOutputStream(path);
cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这里还用到:
computeSampleSize()
/**
* 计算sampleSize最优值
*
* @param options
* @param minSideLength
* @param maxNumOfPixels
* @return
*/
public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
中间调用:
private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
有了这几个方法。一切OK。