[英]why a captured image bitmap from Android camera is rotated
我正在使用 IMAGE_CAPTURE 相機意圖來捕捉和成像,並使用 MediaStore.EXTRA_OUTPUT 將其存儲在文件中。 當我在 onActivityResult 中收到圖像時,旋轉 bitmap。
請就如何解決此問題提出任何建議。
意圖並將 uri 傳遞到我想要存儲文件的位置
private fun capturePhoto() {
val capturedImage = File(this.requireContext().externalCacheDir, "utility_bill.jpg")
if (capturedImage.exists()) {
capturedImage.delete()
}
capturedImage.createNewFile()
mUri = if (Build.VERSION.SDK_INT >= 24) {
FileProvider.getUriForFile(
this.requireContext(),
this.requireContext().applicationContext.packageName,
capturedImage
)
} else {
Uri.fromFile(capturedImage)
}
val intent = Intent("android.media.action.IMAGE_CAPTURE")
intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri)
startActivityForResult(intent, CAMERA_REQUEST_CODE)
}
在 onActivityResult bitmap 中旋轉。
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
hideScreen.visibility = View.GONE
if (resultCode == Activity.RESULT_OK && requestCode == CAMERA_REQUEST_CODE) {
uploadedImageCount++
val bitmap = BitmapFactory.decodeStream(
this.requireContext().contentResolver.openInputStream(mUri!!)
)
}}
當我調試代碼中收到的 bitmap 時,它被旋轉了
請就我如何解決這個問題提出任何建議
提前致謝 R
我在開發應用程序時遇到了同樣的問題,三星、HTC 和某些設備等設備就是這種情況....這是因為 Exif 接口與上面提到的設備不同所以單獨制作一個 Java class ExifUtil .java代碼如下所示注意此代碼位於 Java
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Build;
public class ExifUtil {
/**
// * @see https://sylvana.net/jpegcrop/exif_orientation.html
*/
public static Bitmap rotateBitmap(String src, Bitmap bitmap) {
try {
int orientation = getExifOrientation(src);
if (orientation == 1) {
return bitmap;
}
Matrix matrix = new Matrix();
switch (orientation) {
case 2:
matrix.setScale(-1, 1);
break;
case 3:
matrix.setRotate(180);
break;
case 4:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case 5:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case 6:
matrix.setRotate(90);
break;
case 7:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case 8:
matrix.setRotate(-90);
break;
default:
return bitmap;
}
try {
Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return oriented;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return bitmap;
}
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
private static int getExifOrientation(String src) throws IOException {
int orientation = 1;
try {
/**
* if your are targeting only api level >= 5
* ExifInterface exif = new ExifInterface(src);
* orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
*/
if (Build.VERSION.SDK_INT >= 5) {
Class<?> exifClass = Class.forName("android.media.ExifInterface");
Constructor<?> exifConstructor = exifClass.getConstructor(new Class[] { String.class });
Object exifInstance = exifConstructor.newInstance(new Object[] { src });
Method getAttributeInt = exifClass.getMethod("getAttributeInt", new Class[] { String.class, int.class });
Field tagOrientationField = exifClass.getField("TAG_ORIENTATION");
String tagOrientation = (String) tagOrientationField.get(null);
orientation = (Integer) getAttributeInt.invoke(exifInstance, new Object[] { tagOrientation, 1});
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return orientation;
}
}
和 OnActivityResult 像這樣保存它 Bitmap orientedBitmap = ExifUtil.rotateBitmap(img_path, myBitmap);
this will automatically determine the orientation of the mage and rotate it to Potrait...!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.