来自android相机的图像,右侧朝上

时间:2014-11-02 19:25:05

标签: java android image camera

我试图用正面向上显示来自相机的用户图像 - 不管他如何握住手机。

我的代码很简单:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
cameraIntent.putExtra("return-data", true);                 
startActivityForResult(cameraIntent, CAMERA_RESULT);

然后我将返回的图像附加到onActivityResult中的ImageView。

我知道如何旋转图像,但我怎么知道用户在拍照时如何拿着相机?

1 个答案:

答案 0 :(得分:1)

我几乎肯定你不能使用额外的“返回数据”来做到这一点。 你必须传递你想要创建图片的文件名,然后使用文件的Exif来检查方向。

String FOLDER = "YOUR_FOLDER";
String FILENAME = "YOUR_FILENAME";
int TAKE_PHOTO_REQUEST_CODE = 0;
File photoFile;

void TakePhoto()
{
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    photoFile = new File(FOLDER , FILENAME );
    Uri pictureFileUri = Uri.fromFile(photoFile);
    takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, pictureFileUri);
    startActivityForResult(takePictureIntent, TAKE_PHOTO_REQUEST_CODE);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data); 

    switch(requestCode) { 
    case TAKE_PHOTO_REQUEST_CODE:
        if(resultCode == RESULT_OK)
        {   
            int orientation = 0;
            try
            {
                ExifInterface exif = new ExifInterface(path);
                int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
                switch (exifOrientation)
                {
                case ExifInterface.ORIENTATION_NORMAL:
                    orientation = 0;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    orientation = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    orientation = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    orientation = -90;
                    break;
                }
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        // Here you can rotate the bitmap knowing the orientation of the camera when the photo was taken
    }
 }