android - 如何在kotlin中旋转捕获的图像
问题描述
我在使用意图捕获图像时遇到问题。拍摄的图像会自动旋转。如何预防问题。有人可以帮忙吗?
解决方案
I hope my solution would still help,
byte[] data = null;
Bitmap bitmap = BitmapFactory.decodeByteArray(fileBytes, 0, fileBytes.length);
ByteArrayOutputStream outputStream = null;
try {
bitmap = ImageResizer.rotateImageIfRequired(bitmap, context, uri);
outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
data = outputStream.toByteArray();
} catch (IOException e) {
Timber.e(e.getMessage());
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
// Intentionally blank
}
}
return data;
}
public static Bitmap rotateImageIfRequired(Bitmap img, Context context, Uri selectedImage) throws IOException {
if (selectedImage.getScheme().equals("content")) {
String[] projection = { MediaStore.Images.ImageColumns.ORIENTATION };
Cursor c = context.getContentResolver().query(selectedImage, projection, null, null, null);
if (c.moveToFirst()) {
final int rotation = c.getInt(0);
c.close();
return rotateImage(img, rotation);
}
return img;
} else {
ExifInterface ei = new ExifInterface(selectedImage.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Timber.d("orientation: %s", orientation);
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);
return rotatedImg;
}
推荐阅读
- javascript - 如何将 Calandly 小部件嵌入到 Angular 应用程序中?
- react-testing-library - 材料ui文本输入的react-testing-library
- google-analytics - Google Analytics API:报告维度和指标为空
- azure-devops - Azure DevOps - 授予管道权限以使用 REST API 访问 git 存储库
- python - 如何从 json.dumps (python) 获取“对象数组”
- python - 如何使用 if-else 和 jinja2 模板在烧瓶中传递表单的可变动作?
- zsh - 带有前导 - (破折号)的文件的基本名称
- amazon-ec2 - AWS F1 上的 AES 或其他加密实施
- blender - 有没有办法迭代 .blend 文件目录中的所有子文件?
- haskell - .. 在哪里定义?