gpt4 book ai didi

android - ExifInterface 始终返回 0 方向

转载 作者:搜寻专家 更新时间:2023-11-01 09:45:22 27 4
gpt4 key购买 nike

我看到很多关于此的问题,但我没有找到我的问题的答案。

我尝试显示可以用相机拍摄或从存储中加载的个人资料图像。

我使用 ExifInterface 来确定加载图像的 Picasso 的正确旋转。

我不明白为什么所有图片的方向 = 0

enter image description here

在我的代码下面,非常简单:

 private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);


File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");

FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


try {
Picasso.with(getBaseContext()).load("file:" + destination.getPath()).rotate(MyTools.getFileExifRotation("file:" + destination.getPath())).into(avatar);
} catch (IOException e) {
e.printStackTrace();
}
}

@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {

Bitmap bm=null;
Uri uri=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
uri=data.getData();

} catch (IOException e) {
e.printStackTrace();
}
}



try {
Picasso.with(getBaseContext()).load("file:" + uri.getPath()).rotate(MyTools.getFileExifRotation("file:" + uri.getPath())).into(avatar);
} catch (IOException e) {
e.printStackTrace();
}
}


public static int getFileExifRotation(String path) throws IOException {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return 90;
case ExifInterface.ORIENTATION_ROTATE_180:
return 180;
case ExifInterface.ORIENTATION_ROTATE_270:
return 270;
default:
return 0;
}
}

我在 LG G4 手机上进行测试。

最佳答案

你有一个位图。位图不包含方向。之后,您将位图压缩为 .jpg。这不会添加方向或 exif header 。

所以那个.jpg用ExifInterface是没用的。或者尝试获取方向。

你知道你在保存缩略图吗!?请改用原始版本。

关于android - ExifInterface 始终返回 0 方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38309622/

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