• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.messaging.util.exif;
18 
19 import android.util.Log;
20 import com.android.messaging.util.LogUtil;
21 
22 import java.io.IOException;
23 import java.io.InputStream;
24 
25 /**
26  * This class reads the EXIF header of a JPEG file and stores it in
27  * {@link ExifData}.
28  */
29 class ExifReader {
30     private static final String TAG = LogUtil.BUGLE_TAG;
31 
32     private final ExifInterface mInterface;
33 
ExifReader(ExifInterface iRef)34     ExifReader(ExifInterface iRef) {
35         mInterface = iRef;
36     }
37 
38     /**
39      * Parses the inputStream and and returns the EXIF data in an
40      * {@link ExifData}.
41      *
42      * @throws ExifInvalidFormatException
43      * @throws java.io.IOException
44      */
read(InputStream inputStream)45     protected ExifData read(InputStream inputStream) throws ExifInvalidFormatException,
46             IOException {
47         ExifParser parser = ExifParser.parse(inputStream, mInterface);
48         ExifData exifData = new ExifData(parser.getByteOrder());
49         ExifTag tag = null;
50 
51         int event = parser.next();
52         while (event != ExifParser.EVENT_END) {
53             switch (event) {
54                 case ExifParser.EVENT_START_OF_IFD:
55                     exifData.addIfdData(new IfdData(parser.getCurrentIfd()));
56                     break;
57                 case ExifParser.EVENT_NEW_TAG:
58                     tag = parser.getTag();
59                     if (!tag.hasValue()) {
60                         parser.registerForTagValue(tag);
61                     } else {
62                         exifData.getIfdData(tag.getIfd()).setTag(tag);
63                     }
64                     break;
65                 case ExifParser.EVENT_VALUE_OF_REGISTERED_TAG:
66                     tag = parser.getTag();
67                     if (tag.getDataType() == ExifTag.TYPE_UNDEFINED) {
68                         parser.readFullTagValue(tag);
69                     }
70                     exifData.getIfdData(tag.getIfd()).setTag(tag);
71                     break;
72                 case ExifParser.EVENT_COMPRESSED_IMAGE:
73                     byte buf[] = new byte[parser.getCompressedImageSize()];
74                     if (buf.length == parser.read(buf)) {
75                         exifData.setCompressedThumbnail(buf);
76                     } else {
77                         Log.w(TAG, "Failed to read the compressed thumbnail");
78                     }
79                     break;
80                 case ExifParser.EVENT_UNCOMPRESSED_STRIP:
81                     buf = new byte[parser.getStripSize()];
82                     if (buf.length == parser.read(buf)) {
83                         exifData.setStripBytes(parser.getStripIndex(), buf);
84                     } else {
85                         Log.w(TAG, "Failed to read the strip bytes");
86                     }
87                     break;
88             }
89             event = parser.next();
90         }
91         return exifData;
92     }
93 }
94