旋转从相机或画廊拍摄的图像 [英] Rotate image taken from camera or gallery
本文介绍了旋转从相机或画廊拍摄的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从相机捕捉图像并从图库中选择图像。在三星设备中,图像在捕获后会旋转。
我想将图像旋转成直线。
<我尝试这样做但是没有用。尽管图像旋转,但我的EXIF方向始终为0。
private void onCaptureImageResult(Intent data){
try {
位图缩略图=(位图)data.getExtras()。get(data);
Uri tempUri = getImageUri(getApplicationContext(),thumbnail);
//调用此方法获取实际路径
文件finalFile = new File(getRealPathFromURI(tempUri));
ExifInterface ei = new ExifInterface(String.valueOf(finalFile));
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Bitmap bitmap = BitmapFactory.decodeFile(String.valueOf(finalFile));
Bitmap rotateBitmap = null;
开关(方向){
案例ExifInterface.ORIENTATION_ROTATE_90:
rotateBitmap = rotateImage(bitmap,90);
休息;
case ExifInterface.ORIENTATION_ROTATE_180:
rotateBitmap = rotateImage(bitmap,180);
休息;
case ExifInterface.ORIENTATION_ROTATE_270:
rotatingBitmap = rotateImage(bitmap,270);
休息;
case ExifInterface.ORIENTATION_NORMAL:
默认值:
rotateBitmap = bitmap;
休息;
}
if(rotateBitmap!= null)
{
profile_image.setImageBitmap(rotatingBitmap);
thumbnail = rotatingBitmap;
}
}
catch(IOException ex){}
public Uri getImageUri(Context inContext,Bitmap inImage){
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.PNG,100,bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(),inImage,Title,null);
返回Uri.parse(路径);
}
public String getRealPathFromURI(Uri uri){
Cursor cursor = getContentResolver()。query(uri,null,null,null,null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
返回cursor.getString(idx);
}
public static Bitmap rotateImage(Bitmap source,float angle){
Matrix matrix = new Matrix();
matrix.postRotate(angle);
返回Bitmap.createBitmap(source,0,0,source.getWidth(),source.getHeight(),matrix,
true);
}
这里出了什么问题?请帮忙..谢谢..
解决方案
替换
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_UNDEFINED);
使用
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);
告诉它是否有效
I am capturing image from camera and selecting image from gallery. In samsung devices the images gets rotate after captured.
I want to rotate image to straight if they are rotated.
I tried to do it but its not working. I am getting EXIF orientation as 0 always though the image is rotated.
private void onCaptureImageResult(Intent data) {
try {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
Uri tempUri = getImageUri(getApplicationContext(), thumbnail);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
ExifInterface ei = new ExifInterface(String.valueOf(finalFile));
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Bitmap bitmap = BitmapFactory.decodeFile(String.valueOf(finalFile));
Bitmap rotatedBitmap = null;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotatedBitmap = rotateImage(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotatedBitmap = rotateImage(bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotatedBitmap = rotateImage(bitmap, 270);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
rotatedBitmap = bitmap;
break;
}
if(rotatedBitmap != null)
{
profile_image.setImageBitmap(rotatedBitmap);
thumbnail = rotatedBitmap;
}
}
catch (IOException ex) { }
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix,
true);
}
What is going wrong here? Please help.. Thank you..
解决方案
Replace
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
With
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
And tell if it works
这篇关于旋转从相机或画廊拍摄的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文