gpt4 book ai didi

java - 来自 ACTION_IMAGE_CAPTURE 的图像已旋转

转载 作者:太空宇宙 更新时间:2023-11-04 12:58:54 24 4
gpt4 key购买 nike

我试图简单地从手机摄像头获取图像。令人惊讶的是,它返回旋转。我在互联网上搜索了修复方法,并找到了许多使用 ExifInterface 的解决方案,但它只是有时有效。它的方向似乎是随机错误的,因为我只是重新编译并看到不同的结果。我发现有些人说这是类本身被窃听的错误。

我发现其他解决方案需要两个额外的库和更多的java文件来完成这项工作,但这看起来很荒谬(而且我正在避免额外的包)。为什么图像首先会被旋转(在存储中它们完全正常),解决这个问题有多难?另外 - 旋转 ImageView 也可以(并且似乎比字面上创建旋转图像容易得多),但我需要知道旋转 View 的多少

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

此处调用的 Intent :

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

最佳答案

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

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

我发布链接是因为我不想要所有的功劳。

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

我尝试了该代码,并在我的设备上工作,图像已旋转。

关于java - 来自 ACTION_IMAGE_CAPTURE 的图像已旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35109173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com