安卓在某些设备上发送捕获的图像会旋转90度

3

我正在开发一款Android应用程序。在我的应用程序中,我需要捕获一张图片并将该图片发送到服务器。在某些设备上,所捕获的图像在上传到服务器时会旋转90度。我在stackoverflow和其他一些网站上搜索了解决方法。我找到了一些解决方案..我尝试了它们,例如:

Uri selectedImage = data.getData();


File imageFile = new File(selectedImage.toString());
ExifInterface exif;
try {
exif = new ExifInterface(imageFile.getAbsolutePath());

int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:

rotate=90;

    break;
    case ExifInterface.ORIENTATION_ROTATE_180:

    rotate=180;
 break;
            }

不幸的是,在每个设备上我总是得到方向0。即使在90度旋转的图像设备中也是如此。

请帮助解决我的问题,朋友们。

3个回答

1

您使用:

int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

所以,您发送了defaultValue ExifInterface.ORIENTATION_NORMAL。也许您的exif没有属性TAG_ORIENTATION(或ORIENTATION_UNDEFINED),因此您得到了默认值?

尝试:

int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);

看看你会得到什么。


0
请检查您的图像是否旋转了270度 将此添加到您的switch语句中
switch(orientation) 
{
case ExifInterface.ORIENTATION_ROTATE_90:
rotate=90;

break;
case ExifInterface.ORIENTATION_ROTATE_180:

rotate=180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:

rotate=270;
break;
}

0
我通过使用以下代码解决了我的问题。
private int getImageOrientation(){
    final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION };
    final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
    Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            imageColumns, null, null, imageOrderBy);

    if(cursor.moveToFirst()){
        int orientation = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION));
        rotate=orientation;
        System.out.println("orientation==="+orientation);
        cursor.close();
        return orientation;
    } else {
        return 0;
    }
}

感谢你们的回复,亲爱的朋友们...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接