三星手机拍照旋转问题 调用相机 拍照出现横竖屏切换?
拍照获取图片时,当应用是竖屏时,在部分手机上,如:三星note3上,图片不能正常显示,会旋转90°。
=_=
大概的思路就是拍照后获取图片旋转的角度,然后再回转同样的角度,一般均为0
1.调用拍照后,读取临时存储的图片,temp为自定义的路径
调用拍照
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//下面这句指定调用相机拍照后的照片存储的路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
.fromFile(new File(Environment
.getExternalStorageDirectory(),
SQConstants.tempImgFile)));// SQConstants.tempImgFile文件名public static final String tempImgFile = "/avatar.png";
//intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
//intent.putExtra("return-data", true);
startActivityForResult(intent, SQConstants.AVATAR_CAMERA);//SQConstants.AVATAR_CAMERA为常量值,在返回activity时使用,可自行定义
读取图片Bitmap bm = BitmapsUtil.decodeFile(temp, 100);
private static final int DEFAULT_REQUIRED_SIZE = 70;
public static Bitmap decodeFile(File f, int size) {
try {
BitmapFactory.Options option = new BitmapFactory.Options();
/**
inJustDecodeBounds如果将其设为true的话,在decode时将会返回null。
通过此设置可以去查询一个bitmap的属性,比如bitmap的长和宽,而不占用内存大小.同时可避免OOM
*/
option.inJustDecodeBounds = true;
FileInputStream stream1 = new FileInputStream(f);
BitmapFactory.decodeStream(stream1, null, option);
stream1.close();
final int REQUIRED_SIZE = size > 0 ? size : DEFAULT_REQUIRED_SIZE;
int width_tmp = option.outWidth, height_tmp = option.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
if (scale >= 2) {
scale /= 2;
}
BitmapFactory.Options option2 = new BitmapFactory.Options();
option2.inSampleSize = scale;
FileInputStream stream2 = new FileInputStream(f);
Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, option2);
stream2.close();
return bitmap;
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
2.获取图片旋转的角度,然后给它旋转回来
int degree = BitmapsUtil.readPictureDegree(temp.getAbsolutePath());
/**
* 获取图片信息
*
* @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;
}
3.根据指定旋转度数进行图片旋转
Bitmap bitmap = BitmapsUtil.rotaingImageView(degree, bm);
/**
* 图片旋转
*
* @param angle
* @param bitmap
* @return
*/
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
// 旋转图片 动作
Matrix matrix = new Matrix();
matrix.postRotate(angle);
System.out.println("angle=" + angle);
// 创建新的图片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizedBitmap;
}
4.存储旋转后图片
compressHeadPhoto(bitmap);
private File rotateFile;
private void compressHeadPhoto(final Bitmap bm) {
rotateFile = new File(Environment.getExternalStorageDirectory(),
"rotate.png");
try {
bm.compress(Bitmap.CompressFormat.PNG, 70, new FileOutputStream(
rotateFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
5.调用图片裁切
startPhotoZoom(Uri.fromFile(rotateFile));
/**
* 裁剪图片方法实现
* @param uri
*/
public void s