如何在Android中位圖壓縮後保存Exif數據 (How to save Exif data after bitmap coppression in Android)


問題描述

如何在Android中位圖壓縮後保存Exif數據 (How to save Exif data after bitmap coppression in Android)

我需要從 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 中保存更改。這只是清除所有標籤。


參考解法

方法 1:

saveAttributes method only Save the tag data into the JPEG file.

check this link

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

So if you change your code this

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

to this

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

it will save you exif tag data

Hope this help

Let me know in case of any other issue

(by couldDogSandy)

參考文件

  1. How to save Exif data after bitmap coppression in Android (CC BY‑SA 2.5/3.0/4.0)

#bitmap #Android






相關問題

在 WPF 4.0 中:如何渲染到(PNG)位圖和窗口一樣好? (In WPF 4.0: How to render to a (PNG) bitmap as good as to the window?)

Android 使用手勢移動和縮放 (Android using hand gestures to move and zoom)

Android轉換為位圖崩潰 (Android Converting to bitmap crash)

本機堆不斷增加 (Native heap keeps increasing)

如何為自定義 NSImageRep 子類實現 -draw (How to implement -draw for custom NSImageRep subclass)

為什麼 getBitmap 方法不起作用? (Why is getBitmap method not working?)

這個 3x3 均值過濾器我做錯了什麼? (What am I doing wrong with this 3x3 Mean filter?)

用於顯示位圖和處理按鈕按下的簡單框架 (Simple Frameworks for Displaying Bitmaps and Handling Button Presses)

如何在Android中位圖壓縮後保存Exif數據 (How to save Exif data after bitmap coppression in Android)

運行幾幀後位圖動畫停止工作 (Bitmap animation stops working, after running a few frames)

Silverlight PrtScr 一些控件 (Silverlight PrtScr some controls)

如何使用 PdfiumViewer 將 PDF 轉換為位圖圖像? (How to convert a PDF to a Bitmap image using PdfiumViewer?)







留言討論