summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/gallery3d/exif/ExifXmlReader.java
blob: 8e2ad3f30f97d3961377bbf13ed1902249b17947 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.exif;

import android.content.Context;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExifXmlReader {
    private static final String TAG_EXIF = "exif";
    private static final String TAG_TAG = "tag";

    private static final String IFD0 = "IFD0";
    private static final String EXIF_IFD = "ExifIFD";
    private static final String GPS_IFD = "GPS";
    private static final String IFD1 = "IFD1";
    private static final String PREFIX_INTEROP_IFD = "InteropIFD";

    private static final String ATTR_ID = "id";
    private static final String ATTR_IFD = "ifd";

    /**
     * This function read the ground truth XML.
     *
     * @throws XmlPullParserException
     * @throws IOException
     */
    static public List<Map<Short, String>> readXml(Context context, int xmlResId)
            throws XmlPullParserException, IOException {
        XmlPullParser parser = context.getResources().getXml(xmlResId);

        List<Map<Short, String>> exifData =
                new ArrayList<Map<Short, String>>(IfdId.TYPE_IFD_COUNT);
        for (int i = 0; i < IfdId.TYPE_IFD_COUNT; i++) {
            exifData.add(new HashMap<Short, String>());
        }

        while (parser.next() != XmlPullParser.END_DOCUMENT) {
            if (parser.getEventType() == XmlPullParser.START_TAG) {
                break;
            }
        }
        parser.require(XmlPullParser.START_TAG, null, TAG_EXIF);

        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }

            parser.require(XmlPullParser.START_TAG, null, TAG_TAG);

            int ifdId = getIfdIdFromString(parser.getAttributeValue(null, ATTR_IFD));
            short id = Integer.decode(parser.getAttributeValue(null, ATTR_ID)).shortValue();

            String value = "";
            if (parser.next() == XmlPullParser.TEXT) {
                value = parser.getText();
                parser.next();
            }

            exifData.get(ifdId).put(id, value);

            parser.require(XmlPullParser.END_TAG, null, null);
        }
        return exifData;
    }

    static private int getIfdIdFromString(String prefix) {
        if (IFD0.equals(prefix)) {
            return IfdId.TYPE_IFD_0;
        } else if (EXIF_IFD.equals(prefix)) {
            return IfdId.TYPE_IFD_EXIF;
        } else if (GPS_IFD.equals(prefix)) {
            return IfdId.TYPE_IFD_GPS;
        } else if (IFD1.equals(prefix)) {
            return IfdId.TYPE_IFD_1;
        } else if (PREFIX_INTEROP_IFD.equals(prefix)) {
            return IfdId.TYPE_IFD_INTEROPERABILITY;
        } else {
            assert(false);
            return -1;
        }
    }
}