OSDN Git Service

add help function to decode user comment in ExifData
authorEarl Ou <shunhsingou@google.com>
Fri, 7 Dec 2012 08:56:10 +0000 (16:56 +0800)
committerAndroid (Google) Code Review <android-gerrit@google.com>
Wed, 19 Dec 2012 13:28:18 +0000 (05:28 -0800)
Change-Id: Ida19ce45bfc8b00972e751b652d99f6b19798116

gallerycommon/src/com/android/gallery3d/exif/ExifData.java

index 88593e6..adaceb5 100644 (file)
 
 package com.android.gallery3d.exif;
 
+import android.util.Log;
+
+import java.io.UnsupportedEncodingException;
+import java.lang.reflect.Array;
 import java.nio.ByteOrder;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
@@ -31,10 +35,19 @@ import java.util.TimeZone;
  *  @see IfdData
  */
 public class ExifData {
-
+    private static final String TAG = "ExifData";
     private static final String GPS_DATE_FORMAT_STR = "yyyy:MM:dd";
     private static final String DATETIME_FORMAT_STR = "yyyy:MM:dd kk:mm:ss";
 
+    private static final byte[] USER_COMMENT_ASCII = {
+            0x41, 0x53, 0x43, 0x49, 0x49, 0x00, 0x00, 0x00};
+    private static final byte[] USER_COMMENT_JIS = {
+            0x4A, 0x49, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00};
+    private static final byte[] USER_COMMENT_UNICODE = {
+            0x55, 0x4E, 0x49, 0x43, 0x4F, 0x44, 0x45, 0x00};
+    private static final byte[] USER_COMMENT_UNDEFINED = {
+            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+
     private final DateFormat mDateTimeStampFormat =
             new SimpleDateFormat(DATETIME_FORMAT_STR);
     private final DateFormat mGPSDateStampFormat =
@@ -323,4 +336,38 @@ public class ExifData {
         mStripBytes.clear();
         mIfdDatas[IfdId.TYPE_IFD_1] = null;
     }
+
+    /**
+     * Decodes the user comment tag into string as specified in the EXIF standard.
+     * Returns null if decoding failed.
+     */
+    public String decodeUserComment() {
+        IfdData ifdData = mIfdDatas[IfdId.TYPE_IFD_0];
+        if (ifdData == null) return null;
+        ExifTag tag = ifdData.getTag(ExifTag.TAG_USER_COMMENT);
+        if (tag == null) return null;
+        if (tag.getComponentCount() < 8) return null;
+
+        byte[] buf = new byte[tag.getComponentCount()];
+        tag.getBytes(buf);
+
+        byte[] code = new byte[8];
+        System.arraycopy(buf, 0, code, 0, 8);
+
+        try {
+            if (Arrays.equals(code, USER_COMMENT_ASCII)) {
+                return new String(buf, 8, buf.length - 8, "US-ASCII");
+            } else if (Arrays.equals(code, USER_COMMENT_JIS)) {
+                return new String(buf, 8, buf.length - 8, "EUC-JP");
+            } else if (Arrays.equals(code, USER_COMMENT_UNICODE)) {
+                return new String(buf, 8, buf.length - 8, "UTF-16");
+            } else {
+                return null;
+            }
+        } catch (UnsupportedEncodingException e) {
+            Log.w(TAG, "Failed to decode the user comment");
+            return null;
+        }
+
+    }
 }