我正在从我的肖像相机应用中拍照并发送到服务器。在发送时,我需要在必要时旋转图像。因此,我将图像保存在sdcard中并尝试获取我经历了与此相关的大多数帖子,但对我无效。
-
exifinterface
File imageFile = new File(uri.toString()); ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath()); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
这里的方向始终是任何图像
的0 cusor
int orientation = 0; final String[] projection = new String[] { MediaStore.Images.Media.ORIENTATION }; final Cursor cursor = getApplicationContext() .getContentResolver().query(uri, projection, null, null, null); if (cursor != null) { final int orientationColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION); if (cursor.moveToFirst()) { orientation = cursor.isNull(orientationColumnIndex) ? 0 : cursor.getInt(orientationColumnIndex); }
在这里,图像旋转也为零。如何找到图像的确切旋转?任何帮助将不胜感激。
请开始使用支持库中的exifinterface。它提供了一个支持输入流的构造函数。从URI获得路径是越野车。您需要此库:compile'com.android.support:exifterface:25.3.1'在您的build.gradle中。代码
private float getOrientation (InputStream image, Uri photoURI) {
//ExifInterface myExif = new ExifInterface(photoURI.getPath());
float photoRotation = 0;
boolean hasRotation = true;
try {
ExifInterface exif = new ExifInterface(image);
int exifRotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
switch (exifRotation) {
case ExifInterface.ORIENTATION_UNDEFINED: {
hasRotation = false;
}
case ExifInterface.ORIENTATION_ROTATE_90: {
photoRotation = 90.0f;
break;
}
case ExifInterface.ORIENTATION_ROTATE_180: {
photoRotation = 180.0f;
break;
}
case ExifInterface.ORIENTATION_ROTATE_270: {
photoRotation = 270.0f;
break;
}
}
} catch (IOException e) {
Log.d("ODDREAMS", e.getMessage());
}
if (!hasRotation) {
try {
String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.ORIENTATION};
Cursor cursor = getActivity().getContentResolver().query(photoURI, columns, null, null, null);
if (cursor == null) return 0;
if (cursor.moveToFirst()) {
int orientationColumnIndex = cursor.getColumnIndex(columns[1]);
photoRotation = cursor.getInt(orientationColumnIndex);
Log.d("ODDREAMS", "CURSOR " + photoRotation);
}
cursor.close();
}
catch (Exception e) {}
}
return photoRotation;
}