4

I have an issue in my Android App, I followed this article to take picture in my application and it works fine but unfortunately the picture is in landscape mode instead of portrait mode. I have found several posts on SO and also articles on the net concerning this issue, from what I understand this behavior is not Android behavior but device behavior, and it may change from a device to an other.

So the question is : when I get the picture from the intent, how do I know if the taken picture needs to be rotated or not?

Here you can see the capture taken with the emulator (I have same behavior on my Sony Xperia) :

enter image description here

And then when I display it in an ImageView :

enter image description here

4

1 回答 1

0

由于我没有找到我的问题的任何完整答案,我发布了我的解决方案。

1.从相机中获取照片

protected File pictureFromCamera;
protected Uri photoUri;
...
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
{
    // Create the File where the photo should go
    pictureFromCamera = generateFile();
    if (pictureFromCamera != null)
    {
        String authority = BuildConfig.APPLICATION_ID + ".provider";
        photoUri = FileProvider.getUriForFile(DermatoPhotoCollectionActivity.this, authority, pictureFromCamera);
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
        startActivityForResult(takePictureIntent, REQUEST_PHOTO_FROM_CAMERA);
    }
}

2.获取照片的方向

这是获取方向的代码,它受到本文的启发。请注意,这需要依赖: compile "com.android.support:exifinterface:27.1.1"

获取照片的方向取决于设备,可能无法在某些设备上运行(例如,它在我的 Xperia 上运行良好,但在 Android Emulator 上运行良好)。

public static int getOrientation(Context context, Uri photoUri)
{
    InputStream in = null;
    int orientation = ExifInterface.ORIENTATION_NORMAL;
    try
    {
        in = context.getContentResolver().openInputStream(photoUri);
        if (in != null)
        {
            android.support.media.ExifInterface exifInterface = new android.support.media.ExifInterface(in);
            orientation = exifInterface.getAttributeInt(android.support.media.ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if (in != null)
        {
            try
            {
                in.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    return orientation;
}

3.根据需要旋转图片

我使用 asyncTask 来旋转照片

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK)
    {
        final String pathPhotoFromCamera = pictureFromCamera.getAbsolutePath();

        final ProgressDialog progressDialog = createProgressDialog();
        progressDialog.show();

        int orientation = getOrientation(this, photoUri);
        int rotationAngle = 0;

        if (orientation != ExifInterface.ORIENTATION_NORMAL)
        {
            switch (orientation)
            {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotationAngle = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotationAngle = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotationAngle = 270;
                    break;
            }
        }

        AsyncTaskRotatePicture.AsyncTaskParams params = new AsyncTaskRotatePicture.AsyncTaskParams(pathPhotoFromCamera, rotationAngle);

        AsyncTaskRotatePicture taskRotatePicture = new AsyncTaskRotatePicture(new CallBack()
        {
            @Override
            public void onPostExecute()
            {
                progressDialog.dismiss();
            }
        });

        taskRotatePicture.execute(params);
    }
}

4.最后是旋转照片的代码

该函数会覆盖初始照片。

public static void rotatePhoto(String photoFilePath, int rotationAngle)
{
    Bitmap bm = BitmapFactory.decodeFile(photoFilePath);
    Matrix matrix = new Matrix();
    matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
    byte[] extractedBitmap = extractByteFromBitmap(Bitmap.createBitmap(bm, 0, 0, bm.getWidth() - 1, bm.getHeight() - 1, matrix, true));
    saveBitmapOnSdcard(extractedBitmap, photoFilePath);
}
于 2018-06-05T14:56:27.937 回答