Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Android应用程序旋转拍照失败-始终处于横向布局_Android_Image_Image Processing - Fatal编程技术网

使用Android应用程序旋转拍照失败-始终处于横向布局

使用Android应用程序旋转拍照失败-始终处于横向布局,android,image,image-processing,Android,Image,Image Processing,当我在手机上拍照时,使用Android应用程序,图像没有正确旋转,无论我做什么,它总是处于横向 以下是我如何调用camera app的代码: Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, mPictureFileUri); startActivityForResult(intent, REQUEST_PHOTO); 以下是关于Activi

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

以下是我如何调用camera app的代码:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mPictureFileUri);
startActivityForResult(intent, REQUEST_PHOTO);
以下是关于ActivityResult的方法:

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
    }
}

您可以通过编程方式读取图片文件的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));