【问题标题】:When i take a photo,It gets rotated 90 degrees anti clockwise当我拍照时,它会逆时针旋转 90 度
【发布时间】:2019-08-14 09:23:39
【问题描述】:

我需要一种方法来在我捕获图片后自动旋转它。

它需要自动拾取旋转并纠正它。

当我拍照时,它会逆时针旋转 90 度。

private void CompressAndSetImage(Uri uri)
    {
        Bitmap thumbnail = null;
        try {
            thumbnail = MediaStore.Images.Media.getBitmap(getActivity().getApplicationContext().getContentResolver(), uri);
            int nh = (int) ( thumbnail.getHeight() * (1024.0 / thumbnail.getWidth()) );
            Bitmap scaled = Bitmap.createScaledBitmap(thumbnail, 1024, nh, true);
            Bitmap squareImage = cropToSquare(scaled);
            CurrImageURI =  getImageUri(getActivity().getApplicationContext(), squareImage);
            iv_child.setImageURI(CurrImageURI);
            isImageUploaded = true;

        } catch (IOException e) {
            Toast.makeText(getContext(), "Some error occured", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
private Bitmap cropToSquare(Bitmap bitmap){
        int width  = bitmap.getWidth();
        int height = bitmap.getHeight();
        int newWidth = (height > width) ? width : height;
        int newHeight = (height > width)? height - ( height - width) : height;
        int cropW = (width - height) / 2;
        cropW = (cropW < 0)? 0: cropW;
        int cropH = (height - width) / 2;
        cropH = (cropH < 0)? 0: cropH;
        Bitmap cropImg = Bitmap.createBitmap(bitmap, cropW, cropH, newWidth, newHeight);
        return cropImg;
    }

private Uri getImageUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    }

谢谢。

【问题讨论】:

  • 你的意思是,当从相机捕捉图像时,旋转错误。所以你想旋转它的正确位置?
  • 当我从相机拍摄时,它会显示正确的视图,但是当它进入我的图像视图时它会旋转

标签: java android android-studio


【解决方案1】:

如果您通过意图打开相机,解决旋转问题的最佳方法是使用带有旋转图标的临时片段。让用户自己旋转图像并将其发布到您的最终图像视图中。

您还可以使用 Android cameraX api 创建自定义相机,它是 Camera2 Api 的包装类,在 setTargetRotation 的帮助下,您可以解决相机旋转问题。

【讨论】:

  • 当我从相机拍摄时,它会显示正确的视图,但是当它进入我的图像视图时它会旋转
  • 你试过exif接口解决问题吗..?
  • 在我捕获图片后是否可以通过单击按钮来旋转
  • 我想创建一个临时片段来旋转图片,我该怎么做?
【解决方案2】:

ExifInterface

From official doc:

这是一个用于在 JPEG 文件或 RAW 图像文件。支持的格式有:JPEG、DNG、CR2、NEF、NRW、ARW、 RW2、ORF、PEF、SRW、RAF 和 HEIF。

图像旋转通常存储在照片的 Exif 数据中,作为图像文件的一部分。您可以使用 Android ExifInterface 读取图像的 Exif 元数据 您可以使用此类获取从默认图像库中选择的图像的正确方向。

在您的应用中应用此代码:

首先使用当前上下文和要修复的图像 URI 调用以下方法。

public static Bitmap handleSamplingAndRotationBitmap(Context context, Uri selectedImage)
            throws IOException {
        int MAX_HEIGHT = 1024;
        int MAX_WIDTH = 1024;

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        InputStream imageStream = context.getContentResolver().openInputStream(selectedImage);
        BitmapFactory.decodeStream(imageStream, null, options);
        imageStream.close();

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, MAX_WIDTH, MAX_HEIGHT);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        imageStream = context.getContentResolver().openInputStream(selectedImage);
        Bitmap img = BitmapFactory.decodeStream(imageStream, null, options);

        img = rotateImageIfRequired(img, selectedImage);
        return img;
    }

CalculateInSampleSize 方法:

private static int calculateInSampleSize(BitmapFactory.Options options,
                                         int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee a final image
        // with both dimensions larger than or equal to the requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;

        final float totalPixels = width * height;

        // Anything more than 2x the requested pixels we'll sample down further
        final float totalReqPixelsCap = reqWidth * reqHeight * 2;

        while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
            inSampleSize++;
        }
    }
    return inSampleSize;
}

然后是检查当前图像方向以确定旋转角度的方法:

private static Bitmap rotateImageIfRequired(Bitmap img, Uri selectedImage) throws IOException {

    ExifInterface ei = new ExifInterface(selectedImage.getPath());
    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            return rotateImage(img, 90);
        case ExifInterface.ORIENTATION_ROTATE_180:
            return rotateImage(img, 180);
        case ExifInterface.ORIENTATION_ROTATE_270:
            return rotateImage(img, 270);
        default:
            return img;
    }
}

最后是旋转方法本身:

private static Bitmap rotateImage(Bitmap img, int degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
img.recycle();
return rotatedImg;
}

此代码来源:doc

【讨论】:

  • 谢谢,我已经将它添加到我的应用程序中,我在哪里更改我的代码来实现这个
  • 当你从相机中获取图像时,你会得到图像的Uri。然后调用handleSamplingAndRotationBitmap()。这个方法返回rotate Bitmap。
  • 好吧,让我举个例子。只需在此处发布您的 onActivityResult() 结果代码。
  • public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == GALLERY_INT && resultCode == RESULT_OK) { Uri thisUri = data.getData(); CompressAndSetImage(thisUri); } else if (requestCode == CAMERA_INT && resultCode == RESULT_OK) { Uri thisUri = myUri; CompressAndSetImage(thisUri); } }
  • 更改代码:public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == GALLERY_INT && resultCode == RESULT_OK) { Uri thisUri = data.getData(); CompressAndSetImage(thisUri); } else if (requestCode == CAMERA_INT && resultCode == RESULT_OK) { Uri thisUri = myUri;位图 bitmapImage=handleSamplingAndRotationBitmap(getApplicationContext(),thisUri) //CompressAndSetImage(thisUri); } }
猜你喜欢
  • 2013-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多