Pet*_*ter 11 camera android samsung-galaxy
我讨厌这家公司。所有这些设备都有很多错误。好的问题:我试图解决愚蠢的问题(据我所知存在超过 5 年)它从相机拍摄的照片 - 旋转了 90 度。我有两个设备:
Nexus 5p and Samsung j2
Nexus - work perfect. Everything fine.
Samsung - photo rotated
Run Code Online (Sandbox Code Playgroud)
例如 :
Photo size - nexus : Portrate : width 1000, height 1900. Landscape :
width 1900 , height 1000
Run Code Online (Sandbox Code Playgroud)
让我们看看三星设备:
Photo size - Portrate: width 1900(?????) height - 1000(????)
rotate to landscape : width 1900 height 1000
Run Code Online (Sandbox Code Playgroud)
经过一些测试:如果在三星设备上以横向模式拍摄照片 - 一切都好。照片未旋转
如果在 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);
Run Code Online (Sandbox Code Playgroud)
有什么帮助吗?我还添加了一个检查器,如果它的三星设备比旋转。但是旋转只有在我们以 portrate 模式创建照片时才好。在风景中一切都很好。所以我需要以某种方式检测创建的方向照片。有谁知道吗?
我找到了解决方法:嗯,这对我来说真的很愚蠢而且很难
第一步获取活动结果
@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);
Run Code Online (Sandbox Code Playgroud)
主要步骤是:在文件更改之前获取图像方向。
1) getImageOrientation (by path)
2) 重新保存文件(如果需要发送到服务器,如果你只需要预览我们可以跳过这一步)
3)从文件中获取正确的位图
预览足够只有第 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;
}
Run Code Online (Sandbox Code Playgroud)
获取图像方向
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;
}
Run Code Online (Sandbox Code Playgroud)
如果需要,并重新保存位图
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;
}
Run Code Online (Sandbox Code Playgroud)
这就是全部:) 一切正常