1 /*
2  * Copyright (C) 2011 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.gallery3d.data;
18 
19 import android.util.Log;
20 
21 import com.android.gallery3d.exif.ExifInterface;
22 
23 import java.io.IOException;
24 import java.io.InputStream;
25 
26 public class Exif {
27     private static final String TAG = "GalleryExif";
28 
29     /**
30      * Returns the degrees in clockwise. Values are 0, 90, 180, or 270.
31      */
getOrientation(InputStream is)32     public static int getOrientation(InputStream is) {
33         if (is == null) {
34             return 0;
35         }
36         ExifInterface exif = new ExifInterface();
37         try {
38             exif.readExif(is);
39             Integer val = exif.getTagIntValue(ExifInterface.TAG_ORIENTATION);
40             if (val == null) {
41                 return 0;
42             } else {
43                 return ExifInterface.getRotationForOrientationValue(val.shortValue());
44             }
45         } catch (IOException e) {
46             Log.w(TAG, "Failed to read EXIF orientation", e);
47             return 0;
48         }
49     }
50 
51     /**
52      * Returns an exif interface instance for the given JPEG image.
53      *
54      * @param jpegData a valid JPEG image containing EXIF data
55      */
getExif(byte[] jpegData)56     public static ExifInterface getExif(byte[] jpegData) {
57         ExifInterface exif = new ExifInterface();
58         try {
59             exif.readExif(jpegData);
60         } catch (IOException e) {
61             Log.w(TAG, "Failed to read EXIF data", e);
62         }
63         return exif;
64     }
65 
66     /**
67      * Returns the degrees in clockwise. Values are 0, 90, 180, or 270.
68      */
getOrientation(ExifInterface exif)69     public static int getOrientation(ExifInterface exif) {
70         Integer val = exif.getTagIntValue(ExifInterface.TAG_ORIENTATION);
71         if (val == null) {
72             return 0;
73         } else {
74             return ExifInterface.getRotationForOrientationValue(val.shortValue());
75         }
76     }
77 
78     /**
79      * See {@link #getOrientation(byte[])}, but using the picture bytes instead.
80      */
getOrientation(byte[] jpegData)81     public static int getOrientation(byte[] jpegData) {
82         if (jpegData == null)
83             return 0;
84 
85         ExifInterface exif = getExif(jpegData);
86         return getOrientation(exif);
87     }
88 }
89