我讨厌这家公司。所有这些设备都有很多错误。好的问题: 我试图解决愚蠢的问题(据我所知存在超过 5 年) 它从相机拍摄的照片 - 旋转 90 度。 我有两个设备:
Nexus 5p and Samsung j2
Nexus - work perfect. Everything fine.
Samsung - photo rotated
例如:
Photo size - nexus : Portrate : width 1000, height 1900. Landscape :
width 1900 , height 1000
让我们在三星设备上看看:
Photo size - Portrate: width 1900(?????) height - 1000(????)
rotate to landscape : width 1900 height 1000
经过一些测试:如果在三星设备上以横向模式拍摄照片 - 一切正常。照片未旋转
如果在 PORTRATE 中制作照片 - 照片旋转 90 度。 (但照片的大小与风景一样(这怎么可能)?
有人知道如何修复这个愚蠢的错误吗?也许有人能告诉我如何检测相机的方向?我对照片使用 IntentActivity :
String _path = Environment.getExternalStorageDirectory()
+ File.separator + "camera_img.jpg";
File file = new File(_path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAMERA_REQUEST);
有什么帮助吗? 我还添加了一个检查器,如果它的三星设备不是旋转的话。但只有当我们以纵向模式创建照片时,旋转才有用。在景观中一切都很好。所以我需要以某种方式检测照片是在哪个方向创建的。有谁知道吗?
最佳答案
UPD 29.08.2018 我发现此方法不适用于基于 Android 8+ 的三星设备。我没有三星 s8(例如),不明白为什么这又在那里不起作用。如果有人可以测试并检查为什么这不起作用 - 让我们一起尝试解决这个问题。
我找到了解决方法:好吧,这对我来说真的很愚蠢而且很难。
第一步获取 Activity 结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
String _path = Environment.getExternalStorageDirectory() + File.separator + "TakenFromCamera.jpg";
String p1 = Environment.getExternalStorageDirectory().toString();
String fName = "/TakenFromCamera.jpg";
final int rotation = getImageOrientation(_path);
File file = resaveBitmap(p1, fName, rotation);
Bitmap mBitmap = BitmapFactory.decodeFile(_path);
文件更改前的主要步骤是getImageOrientation
。
getImageOrientation
(按路径)- 重新保存文件(如果需要发送到服务器,如果你只需要预览我们可以跳过这一步)
- 从文件中获取正确的位图
对于预览,只需执行第 1 步和第 3 步,并使用此功能 - 只需旋转位图即可。
private Bitmap checkRotationFromCamera(Bitmap bitmap, String pathToFile, int rotate) {
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return rotatedBitmap;
}
获取图像方向
public static int getImageOrientation(String imagePath) {
int rotate = 0;
try {
ExifInterface exif = new ExifInterface(imagePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return rotate;
}
并在需要时重新保存位图
private File resaveBitmap(String path, String filename, int rotation) { //help for fix landscape photos
String extStorageDirectory = path;
OutputStream outStream = null;
File file = new File(filename);
if (file.exists()) {
file.delete();
file = new File(extStorageDirectory, filename);
}
try {
// make a new bitmap from your file
Bitmap bitmap = BitmapFactory.decodeFile(path + filename);
bitmap = checkRotationFromCamera(bitmap, path + filename, rotation);
bitmap = Bitmap.createScaledBitmap(bitmap, (int) ((float) bitmap.getWidth() * 0.3f), (int) ((float) bitmap.getHeight() * 0.3f), false);
bitmap = Utils.getCircleImage(bitmap);
outStream = new FileOutputStream(path + filename);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
关于android - 从相机旋转的照片(三星设备),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47261434/