简体   繁体   English

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

[英]Image from android camera with right side up

I'm trying to show to the user image from camera with right side up - no regard to how he holds the phone. 我试图以右上方的方式向用户显示来自摄像头的图像-不管他如何握住手机。

My code is simple: 我的代码很简单:

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

Then I attach returned image to ImageView in onActivityResult. 然后,我将返回的图像附加到onActivityResult中的ImageView上。

I know how to rotate the image, but how can I know how user hold the camera when he took a picture? 我知道如何旋转图像,但是我如何知道用户拍照时如何握住相机?

I'm almost sure you can't do that using the "return-data" extra. 我几乎可以肯定,您不能使用额外的“返回数据”来做到这一点。 You'll have to pass the filename of where you wan't the picture be created, and then use the Exif of the file to check the orientation. 您必须传递不需要创建图片的文件名,然后使用文件的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
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM