我讨厌这家公司。他们所有的设备都有很多错误。好的问题: 我试图解决愚蠢的问题(我知道存在超过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);
有任何帮助吗? 我还添加了一个检查器,如果它的三星设备比旋转。但只有在我们以portrate模式创建照片时,旋转才能正常。在景观一切都很好。所以我需要以某种方式检测出哪个方向照片被创建。有人知道吗?
答案 0 :(得分:4)
我找到了如何修复:它真的很愚蠢而且对我很难
第一步获取活动结果
@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。
1)getImageOrientation(按路径)
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;
}
getImageOrientation
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;
}
和resaveBitmap如果需要
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;
}
多数民众赞成:)并且一切正常
答案 1 :(得分:1)
如果要旋转图像但只有字节数组,可以使用:
private byte[] rotationFromCamera(byte[] data) {
int rotation = Exif.getOrientation(data);
if(rotation == 0) return data;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, null);
Matrix matrix = new Matrix();
matrix.postRotate(rotation);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}
这个exif util来自answer
如果图像很大,这有点慢。
答案 2 :(得分:1)
找到了这个here,对我来说是三星S8。
static final String[] CONTENT_ORIENTATION = new String[] {
MediaStore.Images.ImageColumns.ORIENTATION
};
public static int getExifOrientation(Context context, Uri uri) {
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = null;
try {
String id = DocumentsContract.getDocumentId(uri);
id = id.split(":")[1];
cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
CONTENT_ORIENTATION, MediaStore.Images.Media._ID + " = ?", new String[] { id }, null);
if (cursor == null || !cursor.moveToFirst()) {
return 0;
}
return cursor.getInt(0);
} catch (RuntimeException ignored) {
// If the orientation column doesn't exist, assume no rotation.
return 0;
} finally {
if (cursor != null) {
cursor.close();
}
}
}
当我从图库Intent.ACTION_GET_CONTENT
获取图像时,此方法有效。
用MediaStore.ACTION_IMAGE_CAPTURE
测试了它,并返回0。但是也许我做错了。
答案 3 :(得分:0)
您是否自己使用相机API拍照? 如果是这样,您可以获得以下方向: Camera1 API:
let story = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
UIView.beginAnimations("", context: nil)
UIView.setAnimationDuration(1.0)
UIView.setAnimationCurve(UIViewAnimationCurve.easeInOut)
UIView.setAnimationTransition(UIViewAnimationTransition.flipFromRight, for: (self.navigationController?.view)!, cache: false)
self.navigationController?.pushViewController(story, animated: true)
UIView.commitAnimations()
Camera2 API:
CameraInfo cameraInfo = new CameraInfo();
int sensorOrientation = cameraInfo.orientation;
答案 4 :(得分:-1)
使用Glide lib可以解决问题
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_PICK_PHOTO_FOR_PROFILE && resultCode == RESULT_OK && data != null && data.getData() != null) {
try {
Glide.with(this).load(data.getData()).into(mProfileFragment.mProfileImage);
} catch (Exception e) {
Utils.handleException(e);
}
}