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