2 回答
TA贡献1833条经验 获得超4个赞
大约有 20 亿台 Android 设备,分布在大约 20,000 种设备型号中。这些设备型号中预装了数十个(如果不是数百个)相机应用程序。用户可以下载和安装许多其他相机应用程序。
您的代码可能会启动其中任何一个。
在相机意图中,我的华为 P20 Pro 上的图像预览始终处于纵向模式
这就是数百个相机应用程序的行为。
在另一台测试设备上,预览图像(您可以决定是否要重新拍摄图像的图像)也卡在“初始”旋转中,看起来很难看。
这就是数百个相机应用程序的行为。
相机应用程序不需要以这种方式运行。当然,相机应用程序根本不需要预览图像。
有解决方案吗?
如果你想使用ACTION_IMAGE_CAPTURE,不。这数百个相机应用程序的行为取决于这些相机应用程序的开发人员,而不是您。
拍照还有其他选项,例如直接使用相机 API 或使用 Fotoapparat 或 CameraKit-Android 等第三方库。
TA贡献1793条经验 获得超6个赞
用于ExifInterface在解码时检查图像的方向。然后旋转图像以获得正确方向的所需图像。
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
Bitmap decoded = BitmapFactory.decodeFile(filePath, options);
if (decoded == null) return null;
try {
ExifInterface exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
int rotation = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
rotation = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
rotation = 270;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
rotation = 180;
}
if (rotation != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(rotation);
Bitmap newBitmap = Bitmap.createBitmap(decoded, 0, 0, decoded.getWidth(),
decoded.getHeight(), matrix, true);
decoded.recycle();
Runtime.getRuntime().gc();
decoded = newBitmap;
}
} catch (IOException e) {
e.printStackTrace();
}
如果您想使用支持库来支持 API 级别较低的设备,请使用以下依赖项:
implementation 'com.android.support:exifinterface:27.1.1'
并导入 android.support.media.ExifInterface
添加回答
举报
