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