Android EXIF数据始终为0,如何更改? [英] Android EXIF data always 0, how to change it?

查看:352
本文介绍了Android EXIF数据始终为0,如何更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,该应用程序使用本机Camera捕获照片,然后将其上传到服务器.我的问题是所有照片的EXIF方向值均为0,这会使其他地方的显示混乱.

I have an app that captures photos using the native Camera and then uploads them to a server. My problem is that all the photos have an EXIF orientation value of 0, and this messes up the display elsewhere.

如何更改EXIF方向?我不是在寻找一种针对每种情况进行更正的方法,只需将其更改为其他值即可.

How can I change the EXIF orientation? I'm not looking for a way to correct it for every circumstance, just change it to a different value.

我正在使用Samsung Galaxy Note 4

I'm using a Samsung Galaxy Note 4

我尝试了以下解决方案,该解决方案可以在拍照前设置相机方向:设置Android Photo EXIF方向

I tried this solution that sets the camera orientation before taking photos: 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

but it doesn't influence the resulting EXIF data, its still always 0

我还尝试了以下解决方案,在拍摄后旋转图像:

I also tried these solutions where the image is rotated after it is taken: EXIF orientation tag value always 0 for image taken with portrait camera app android

虽然这会旋转照片,但EXIF方向始终始终为0.

and while this rotates the photo, the EXIF orientation is still always 0.

我还尝试直接设置EXIF数据:在Android中对位图进行压缩后如何保存Exif数据

I also tried setting the EXIF data directly: 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方向仍然为0.

but EXIF Orientation is still 0 after uploading.

我也研究了以下解决方案:

I have also looked at these solutions:

Exif数据TAG_ORIENTATION始终为0

如何在Android中将exif数据写入图像? /a>

How to write exif data to image in Android?

如何以获得从默认图片库中选择的图片的正确方向

如何设置相机图像方向?

但是它们都涉及通过旋转来校正方向,这不会影响EXIF数据,或者直接设置似乎无效的EXIF数据.

but they all involve correcting the orientation by rotating, which doesn't influence the EXIF data, or setting the EXIF data directly which doesn't seem to work.

如何将文件的EXIF方向数据从0更改为3?

How can I change the file's EXIF orientation data from 0 to 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 */
);

推荐答案

原来我的代码能够设置EXIF数据,但是Android解释此数据的方式与iOS和Mac上的Chrome浏览器之间存在差异(其中我正在检查生成的文件)对其进行解释.

Turns out my code was able to set the EXIF data, but there is a discrepancy between how Android interprets this data and how iOS and Chrome on a Mac (where I was checking the resulting file) interprets it.

这是设置EXIF方向所需的唯一代码:

This is the only code needed to set EXIF orientation:

ExifInterface exif = new ExifInterface(pictureFile.toString());
exif.setAttribute(ExifInterface.TAG_ORIENTATION, "3");
exif.saveAttributes();

但是,设置3在iOS上显示为0,并且图像在Chrome中是横向的.

However, setting 3 shows up as 0 on iOS and the image is sideways in Chrome.

设置6在iOS上显示为3,并且图像在Chrome中显示正确.

setting 6 shows up as 3 on iOS and the image looks right in Chrome.

这篇关于Android EXIF数据始终为0,如何更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆