如何在Android中对位图进行压缩后保存Exif数据

12

我需要从SD卡中获取图像,创建、旋转并保存更改后的图像。我尝试使用以下代码

Bitmap original = BitmapFactory.decodeFile(file.getAbsolutePath());

    ExifInterface originalExif = new ExifInterface(file.getAbsolutePath());
    int orientation = originalExif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

    Matrix matrix = new Matrix();
    int rotate = 90;
    if(orientation == ExifInterface.ORIENTATION_ROTATE_90){
        rotate = 180;
    }else if(orientation == ExifInterface.ORIENTATION_ROTATE_180){
        rotate = 270;
    }else if(orientation == ExifInterface.ORIENTATION_ROTATE_270){
        rotate = 0;
    }

    matrix.postRotate(rotate);

    Bitmap bitmap = Bitmap.createBitmap(original, 0, 0, original.getWidth(), original.getHeight(), matrix, true);

    try {
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        original.recycle();
        bitmap.recycle();
    }


    ExifInterface newExif = new ExifInterface(file.getAbsolutePath());

    newExif.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(ExifInterface.ORIENTATION_ROTATE_90));

    newExif.saveAttributes();

但是我无法在ExifInterface中保存更改。这只会清除所有标签。


你需要什么?基于EXIF数据的图像旋转还是你想将数据保存在EXIF中? - user1140237
这个看起来是个好的例子:https://github.com/apache/commons-imaging/blob/master/src/test/java/org/apache/commons/imaging/examples/WriteExifMetadataExample.java#L221 - Minhaz
1个回答

1

saveAttributes方法仅将标签数据保存到JPEG文件中。

请查看此链接。

http://developer.android.com/reference/android/media/ExifInterface.html#saveAttributes()

所以如果你改变了你的代码,这样:

bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);

到这里

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);

它将保存您的Exif标签数据。

希望这可以帮到您。

如果有其他问题,请告诉我。


1
@couldDog 如果我的回答对你有帮助,我很高兴。请接受我的答案,这样其他人也可以从这里得到帮助。谢谢! - Sandy
1
@couldDog是我的答案,如果你还没有接受我的答案,那么你是否尝试过其他可行的方法呢? - Sandy
我不知道我必须接受回答一个问题。现在我知道了,我已经点击了检查。谢谢。 - couldDog
24
不,它不会保存EXIF信息。 - Leon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接