Android相机和纵向轮换

时间:2018-06-04 13:13:50

标签: android android-camera

我的Android应用程序中存在问题,我跟着this article在我的应用程序中拍照并且工作正常但不幸的是图片处于横向模式而不是纵向模式。我已经在SO上找到了几篇帖子,还有关于这个问题的网上文章,据我所知,这种行为不是Android行为,而是设备行为,它可能会从设备变为其他。

所以问题是:当我从意图中得到图片时,我怎么知道拍摄的图片是否需要旋转?

在这里你可以看到使用模拟器进行的捕获(我在Sony Xperia上有相同的行为):

enter image description here

然后当我在ImageView中显示它时:

enter image description here

1 个答案:

答案 0 :(得分: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。获取照片的方向

以下是获取方向的代码,它的灵感来自this article。请注意,这需要依赖: compile "com.android.support:exifinterface:27.1.1"

获取照片的方向取决于设备,可能无法在某些设备上运行(例如,它在我的Xperia上运行正常但在Android模拟器上无法正常工作)。

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