blob: fea8ff19a18910e82000ee07a7ce1d5d92446846 [file] [log] [blame]
Hung-ying Tyan2523f432012-10-19 20:50:50 +08001/*
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
ztenghuia16e7b52013-08-23 11:47:56 -070017package com.android.camera.exif;
Hung-ying Tyan2523f432012-10-19 20:50:50 +080018
Angus Kong2bca2102014-03-11 16:27:30 -070019import com.android.camera.debug.Log;
Earl Ou04903342012-10-19 23:27:44 +080020
Hung-ying Tyan2523f432012-10-19 20:50:50 +080021import java.io.IOException;
22import java.io.InputStream;
23
24/**
Ruben Brunkc274ded2013-03-11 19:00:12 -070025 * This class reads the EXIF header of a JPEG file and stores it in
26 * {@link ExifData}.
Hung-ying Tyan2523f432012-10-19 20:50:50 +080027 */
Ruben Brunkc274ded2013-03-11 19:00:12 -070028class ExifReader {
Angus Kong2bca2102014-03-11 16:27:30 -070029 private static final Log.Tag TAG = new Log.Tag("ExifReader");
Ruben Brunkc274ded2013-03-11 19:00:12 -070030
31 private final ExifInterface mInterface;
32
33 ExifReader(ExifInterface iRef) {
34 mInterface = iRef;
35 }
36
Hung-ying Tyan2523f432012-10-19 20:50:50 +080037 /**
Ruben Brunkc274ded2013-03-11 19:00:12 -070038 * Parses the inputStream and and returns the EXIF data in an
39 * {@link ExifData}.
40 *
Hung-ying Tyan2523f432012-10-19 20:50:50 +080041 * @throws ExifInvalidFormatException
42 * @throws IOException
43 */
Ruben Brunkc274ded2013-03-11 19:00:12 -070044 protected ExifData read(InputStream inputStream) throws ExifInvalidFormatException,
Hung-ying Tyan2523f432012-10-19 20:50:50 +080045 IOException {
Ruben Brunkc274ded2013-03-11 19:00:12 -070046 ExifParser parser = ExifParser.parse(inputStream, mInterface);
Hung-ying Tyan2523f432012-10-19 20:50:50 +080047 ExifData exifData = new ExifData(parser.getByteOrder());
Ruben Brunkc274ded2013-03-11 19:00:12 -070048 ExifTag tag = null;
Hung-ying Tyan2523f432012-10-19 20:50:50 +080049
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:
Ruben Brunkc274ded2013-03-11 19:00:12 -070057 tag = parser.getTag();
Hung-ying Tyan2523f432012-10-19 20:50:50 +080058 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) {
Earl Ou04903342012-10-19 23:27:44 +080067 parser.readFullTagValue(tag);
Hung-ying Tyan2523f432012-10-19 20:50:50 +080068 }
69 exifData.getIfdData(tag.getIfd()).setTag(tag);
70 break;
71 case ExifParser.EVENT_COMPRESSED_IMAGE:
72 byte buf[] = new byte[parser.getCompressedImageSize()];
Earl Ou04903342012-10-19 23:27:44 +080073 if (buf.length == parser.read(buf)) {
74 exifData.setCompressedThumbnail(buf);
75 } else {
76 Log.w(TAG, "Failed to read the compressed thumbnail");
77 }
Hung-ying Tyan2523f432012-10-19 20:50:50 +080078 break;
79 case ExifParser.EVENT_UNCOMPRESSED_STRIP:
80 buf = new byte[parser.getStripSize()];
Earl Ou04903342012-10-19 23:27:44 +080081 if (buf.length == parser.read(buf)) {
82 exifData.setStripBytes(parser.getStripIndex(), buf);
Ruben Brunkc274ded2013-03-11 19:00:12 -070083 } else {
Earl Ou04903342012-10-19 23:27:44 +080084 Log.w(TAG, "Failed to read the strip bytes");
85 }
Hung-ying Tyan2523f432012-10-19 20:50:50 +080086 break;
87 }
88 event = parser.next();
89 }
90 return exifData;
91 }
Earl Ou04903342012-10-19 23:27:44 +080092}