用Android应用程序轮换拍照失败 - 总是在横向布局中

时间:2014-01-30 06:42:04

标签: android image image-processing

当我在手机上拍照时,使用Android应用程序时图像没有正确旋转,无论我做什么,它总是处于横向状态。

这是我如何调用相机应用程序的代码:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mPictureFileUri);
startActivityForResult(intent, REQUEST_PHOTO);

这是on ActivitiResult方法:

public void onActivityResult(int requestCode, int resultCode, Intent data){
    if(resultCode != Activity.RESULT_OK)
        return;
    if(requestCode == REQUEST_PHOTO){
        //here I show picture, but beside shove it, I need also rotate it.
        //data are here null
    }
}

1 个答案:

答案 0 :(得分:1)

您可以阅读图片文件的exif并以编程方式正确旋转:

Bitmap image = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());

Matrix matrix = new Matrix();

ExifInterface exifInterface = new ExifInterface(pictureFile.getAbsolutePath());

int rotation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
        ExifInterface.ORIENTATION_NORMAL);
switch(rotation){
    case ExifInterface.ORIENTATION_NORMAL:{
    }break;
    case ExifInterface.ORIENTATION_ROTATE_90:{
        matrix.postRotate(90);
        image = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), 
                matrix, true);
    }break;
    case ExifInterface.ORIENTATION_ROTATE_180:{
        matrix.postRotate(180);
        image = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(),
                matrix, true);
    }break;
    case ExifInterface.ORIENTATION_ROTATE_270:{
        matrix.postRotate(270);
        image = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), 
                matrix, true);
    }break;
}

// convert bitmap to jpeg with 50% compression
image.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(pictureFile));