图片来自ACTION_IMAGE_CAPTURE轮换

时间:2016-01-31 01:07:38

标签: java android image

我试图从手机的相机中获取图像。令人惊讶的是,它返回旋转。我已经在互联网上搜索了修复程序,并使用ExifInterface遇到了许多解决方案,但它只在某些情况下有效。由于我只是重新编译并看到不同的结果,因此它看起来似乎是随机的错误。我发现有些人说这是故障本身的错误。

我发现其他解决方案需要两个额外的库和更多的java文件来完成这项工作,但这看起来很荒谬(而且我正在避免使用其他软件包)。为什么图像首先被旋转(在存储中它们是完全正常的),以及解决问题可能有多难?此外 - 旋转图像视图也可以工作(并且看起来比创建旋转图像容易得多),但我需要通过多少来轮换视图。

编辑----我意识到如果使用后置摄像头,图像始终从拍摄图像的方向(意图内部)顺时针旋转270度,如果使用前置摄像头,则为90度。因此,我只需要一种方法来找出这种方向。

这里有意图:

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

    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = setUpPhotoFile();
            mCurrentPhotoPath = photoFile.getAbsolutePath();
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
        } catch (IOException ex) {
            // Error occurred while creating the File
            photoFile = null;
            mCurrentPhotoPath = null;
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

        } else {
            Toast noStorage = Toast.makeText(this, "Cannot access mounted storage.", Toast.LENGTH_SHORT);
            noStorage.show();
        }
    }
}

在此处创建的位图:

private void setPic() {
   /* Get the size of the ImageView */
    int targetW = mImageView.getWidth();
    int targetH = mImageView.getHeight();

    /* Get the size of the image */
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    /* Figure out which way needs to be reduced less */
    int scale = 1;
    if (photoH > targetH|| photoW > targetW) {
        scale = Math.max(
                (int)Math.pow(2, (int) Math.ceil(Math.log(targetW /
                (double) photoW)) / Math.log(0.5)),

                (int)Math.pow(2, (int) Math.ceil(Math.log(targetH /
                (double) photoH)) / Math.log(0.5)));
        ;
    }


    /* Set bitmap options to scale the image decode target */
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scale;

    /* Decode the JPEG file into a Bitmap */
    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

     /*-----------How should I rotate bitmap/mImageView to correct orientation?*/

    /* Associate the Bitmap to the ImageView */
    mImageView.setImageBitmap(bitmap);
    mImageView.setVisibility(View.VISIBLE);
}

1 个答案:

答案 0 :(得分:0)

我找到的最好的解决方案是:

https://www.samieltamawy.com/how-to-fix-the-camera-intent-rotated-image-in-android/

我发布链接是因为我不想获得所有荣誉。

Sami Eltamawy编写了一个功能,可以在需要旋转图像时旋转图像。

我尝试了代码,并且正在我的设备上处理图像旋转。