【发布时间】:2017-10-12 22:09:52
【问题描述】:
我从来没有像今天这样在图像上苦苦挣扎,我会很感激一些帮助 :D
所以,事情是,我正在使用内置摄像头拍摄照片,然后将其发送到后端以保存它,但Kit kat 和特别是Samsung 设备的方向搞砸了。我尝试使用大家建议的exif 界面,但我无法获得照片方向。
几分钟前我找到了一个与此相关的答案,说也许一个好的解决方案是在拍照时保存设备方向,这听起来很不错,但是,我不知道如何使用内置在相机中,因为使用Intent 打开相机时我没有完全控制权,如下所示:
mPathToTakenImage = ImageProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider",
newFile);
openCamera.putExtra(MediaStore.EXTRA_OUTPUT, mPathToTakenImage);
openCamera.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(openCamera, AlgebraNationConstants.TAKE_PHOTO_REQUEST_CODE);
那么,如何在拍摄图像时获取设备方向以便正确旋转图像?
这是旋转图像的代码,但是,我总是得到零:
final Bitmap finalImg;
final StringBuilder base64Image = new StringBuilder("data:image/jpeg;base64,");
final ExifInterface exifInterface;
try {
final String imagePath = params[0].getPath();
exifInterface = new ExifInterface(imagePath);
final int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
final Bitmap takenPhoto = MediaStore.Images.Media.getBitmap(mRelatedContext.getContentResolver(),
params[0]);
if (null == takenPhoto) {
base64Image.setLength(0);
} else {
finalImg = rotateBitmap(takenPhoto, orientation);
if (null == finalImg) {
base64Image.setLength(0);
} else {
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
finalImg.compress(Bitmap.CompressFormat.JPEG, 75, byteArrayOutputStream);
final byte[] byteArray = byteArrayOutputStream.toByteArray();
base64Image.append(Base64.encodeToString(byteArray, Base64.DEFAULT));
}
}
} catch (IOException e) {
e.printStackTrace();
}
return base64Image.length() == 0 ? null : base64Image.toString();
我快疯了,任何帮助都将不胜感激。
【问题讨论】:
-
final String imagePath = params[0].getPath();-- 这到底是什么?它应该是newFile(或者,因为它是String,所以newFile.getAbsolutePath()。 -
感谢您的回复。该代码在
Asynctask类中调用,其中params[0]是Uri。 -
Uri是从哪里来的?您已经知道图像应该在哪里 (newFile),而那不是Uri。除非Uri是您使用Uri.fromFile()创建的,否则getPath()毫无意义。 -
当图像来自相机时,
Uri是我在Intent中额外传递的那个,否则在onActivityResult中使用data.getData()
标签: android camera rotation exif