1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.camera.exif;
18 
19 import com.android.camera.debug.Log;
20 
21 import java.io.IOException;
22 import java.io.InputStream;
23 
24 /**
25  * This class reads the EXIF header of a JPEG file and stores it in
26  * {@link ExifData}.
27  */
28 class ExifReader {
29     private static final Log.Tag TAG = new Log.Tag("ExifReader");
30 
31     private final ExifInterface mInterface;
32 
ExifReader(ExifInterface iRef)33     ExifReader(ExifInterface iRef) {
34         mInterface = iRef;
35     }
36 
37     /**
38      * Parses the inputStream and and returns the EXIF data in an
39      * {@link ExifData}.
40      *
41      * @throws ExifInvalidFormatException
42      * @throws IOException
43      */
read(InputStream inputStream)44     protected ExifData read(InputStream inputStream) throws ExifInvalidFormatException,
45             IOException {
46         ExifParser parser = ExifParser.parse(inputStream, mInterface);
47         ExifData exifData = new ExifData(parser.getByteOrder());
48         ExifTag tag = null;
49 
50         int event = parser.next();
51         while (event != ExifParser.EVENT_END) {
52             switch (event) {
53                 case ExifParser.EVENT_START_OF_IFD:
54                     exifData.addIfdData(new IfdData(parser.getCurrentIfd()));
55                     break;
56                 case ExifParser.EVENT_NEW_TAG:
57                     tag = parser.getTag();
58                     if (!tag.hasValue()) {
59                         parser.registerForTagValue(tag);
60                     } else {
61                         exifData.getIfdData(tag.getIfd()).setTag(tag);
62                     }
63                     break;
64                 case ExifParser.EVENT_VALUE_OF_REGISTERED_TAG:
65                     tag = parser.getTag();
66                     if (tag.getDataType() == ExifTag.TYPE_UNDEFINED) {
67                         parser.readFullTagValue(tag);
68                     }
69                     exifData.getIfdData(tag.getIfd()).setTag(tag);
70                     break;
71                 case ExifParser.EVENT_COMPRESSED_IMAGE:
72                     byte buf[] = new byte[parser.getCompressedImageSize()];
73                     if (buf.length == parser.read(buf)) {
74                         exifData.setCompressedThumbnail(buf);
75                     } else {
76                         Log.w(TAG, "Failed to read the compressed thumbnail");
77                     }
78                     break;
79                 case ExifParser.EVENT_UNCOMPRESSED_STRIP:
80                     buf = new byte[parser.getStripSize()];
81                     if (buf.length == parser.read(buf)) {
82                         exifData.setStripBytes(parser.getStripIndex(), buf);
83                     } else {
84                         Log.w(TAG, "Failed to read the strip bytes");
85                     }
86                     break;
87             }
88             event = parser.next();
89         }
90         return exifData;
91     }
92 }
93