【发布时间】:2017-01-16 22:36:38
【问题描述】:
我有一个应用程序,它使用本机 Camera 捕获照片,然后将它们上传到服务器。我的问题是所有照片的 EXIF 方向值都是 0,这会弄乱其他地方的显示。
如何更改 EXIF 方向?我不是在寻找一种针对每种情况进行纠正的方法,只需将其更改为不同的值即可。
我使用的是三星 Galaxy Note 4
我尝试了这种在拍照前设置相机方向的解决方案:Setting Android Photo EXIF Orientation
Camera c = Camera.open();
c.setDisplayOrientation(90);
Camera.Parameters params = mCamera.getParameters();
params.setRotation(0); // tried 0, 90, 180
c.setParameters(params);
但它不会影响生成的 EXIF 数据,它仍然始终为 0
我还尝试了这些解决方案,在拍摄后旋转图像:EXIF orientation tag value always 0 for image taken with portrait camera app android
虽然这会旋转照片,但 EXIF 方向仍始终为 0。
我也试过直接设置EXIF数据:How to save Exif data after bitmap compression in Android
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
final File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE, "");
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
ExifInterface exif = new ExifInterface(pictureFile.toString());
exif.setAttribute(ExifInterface.TAG_ORIENTATION, "3");
exif.saveAttributes();
fos.write(data);
fos.close();
//upload photo..
}
}
}
但是上传后EXIF Orientation还是0。
我也看过这些解决方案:
Exif data TAG_ORIENTATION always 0
How to write exif data to image in Android?
How to get the Correct orientation of the image selected from the Default Image gallery
how to set camera Image orientation?
但它们都涉及通过旋转来纠正方向,这不会影响EXIF数据,或者直接设置EXIF数据似乎不起作用。
如何将文件的 EXIF 方向数据从 0 更改为 3?
更新:
这是我的上传代码:
Bitmap sBitmap = null;
final File sResizedFile = getOutputMediaFile(MEDIA_TYPE_IMAGE, "_2");
try {
sBitmap = BitmapFactory.decodeStream(new FileInputStream(pictureFile), null, options);
} catch (FileNotFoundException e) {
Log.e("App", "[MainActivity] unable to convert pictureFile to bitmap");
e.printStackTrace();
return;
}
// ... compute sw and sh int values
Bitmap sOut = Bitmap.createScaledBitmap(sBitmap, sw, sh, false);
Bitmap rotatedBitmap = rotateBitmap(sOut, 3);
FileOutputStream sfOut;
try {
sfOut = new FileOutputStream(sResizedFile);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 70, sfOut);
sfOut.flush();
sfOut.close();
sBitmap.recycle();
sOut.recycle();
rotatedBitmap.recycle();
} catch (Exception e) {
Log.e("App", "[MainActivity] unable to save thumbnail");
e.printStackTrace();
return;
}
// upload small thumbnail
TransferObserver sObserver = transferUtility.upload(
"stills/small", /* The bucket to upload to */
filename + ".jpg", /* The key for the uploaded object */
sResizedFile /* The file where the data to upload exists */
);
【问题讨论】:
-
您在哪个设备上尝试?
-
三星 Galaxy Note 4
-
@Cbas,您上传到在线存储桶的“EXIF方向值为0” 之前还是之后?我刚刚测试了亚马逊的 AWS 存储桶,它们上传后保留了 EXIF。您必须使用不同的基于存储桶的服务器(例如:Google Cloud),对吧?在您的第一个代码 sn-p 之后,您说 “它不会影响生成的 EXIF 数据,它仍然始终为 0” 所以这是代码问题?然后在你的第二个代码 sn-p 之后你说 “但是 EXIF 方向仍然是 0 上传后。” 所以这是一个上传/服务器问题?请确认 EXIF 值在上传前还是仅在上传后始终为 0?
-
@VC.One 上传前为 0,绝对是代码问题
标签: android image camera orientation exif