gpt4 book ai didi

Android相机结果图像应在捕获后旋转?

转载 作者:IT老高 更新时间:2023-10-28 22:17:02 25 4
gpt4 key购买 nike

我正在编写一个使用相机的 Android 应用程序。我将相机显示方向设置为 90,我的 Activity 是纵向的:

camera.setDisplayOrientation(90);

我得到了一个方向良好的预览图片,但生成的图片被旋转到 -90 度(逆时针)并且

exif.getAttribute(ExifInterface.TAG_ORIENTATION)

返回 ORIENTATION_NORMAL
这是预期的行为吗?我应该在捕获后旋转结果图像吗?

设备 - Nexus S,API - 10

最佳答案

试试这个

try {
File f = new File(imagePath);
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);

int angle = 0;

if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
angle = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
angle = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
angle = 270;
}

Matrix mat = new Matrix();
mat.postRotate(angle);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;

Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f),
null, options);
bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), mat, true);
ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100,
outstudentstreamOutputStream);
imageView.setImageBitmap(bitmap);

} catch (IOException e) {
Log.w("TAG", "-- Error in setting image");
} catch (OutOfMemoryError oom) {
Log.w("TAG", "-- OOM Error in setting image");
}

会有用的

关于Android相机结果图像应在捕获后旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8346955/

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