1 /*
2  * Copyright (C) 2010 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.common;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.net.Uri;
22 import android.util.Log;
23 
24 import com.android.gallery3d.exif.ExifInterface;
25 
26 import java.io.BufferedInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 
30 public class BitmapUtils {
31 
32     private static final String TAG = "BitmapUtils";
33 
34     // Find the min x that 1 / x >= scale
computeSampleSizeLarger(float scale)35     public static int computeSampleSizeLarger(float scale) {
36         int initialSize = (int) Math.floor(1f / scale);
37         if (initialSize <= 1) return 1;
38 
39         return initialSize <= 8
40                 ? Utils.prevPowerOf2(initialSize)
41                 : initialSize / 8 * 8;
42     }
43 
getRotationFromExif(Context context, Uri uri)44     public static int getRotationFromExif(Context context, Uri uri) {
45         return BitmapUtils.getRotationFromExifHelper(null, 0, context, uri);
46     }
47 
getRotationFromExif(Resources res, int resId)48     public static int getRotationFromExif(Resources res, int resId) {
49         return BitmapUtils.getRotationFromExifHelper(res, resId, null, null);
50     }
51 
getRotationFromExifHelper(Resources res, int resId, Context context, Uri uri)52     private static int getRotationFromExifHelper(Resources res, int resId, Context context, Uri uri) {
53         ExifInterface ei = new ExifInterface();
54         InputStream is = null;
55         BufferedInputStream bis = null;
56         try {
57             if (uri != null) {
58                 is = context.getContentResolver().openInputStream(uri);
59                 bis = new BufferedInputStream(is);
60                 ei.readExif(bis);
61             } else {
62                 is = res.openRawResource(resId);
63                 bis = new BufferedInputStream(is);
64                 ei.readExif(bis);
65             }
66             Integer ori = ei.getTagIntValue(ExifInterface.TAG_ORIENTATION);
67             if (ori != null) {
68                 return ExifInterface.getRotationForOrientationValue(ori.shortValue());
69             }
70         } catch (IOException e) {
71             Log.w(TAG, "Getting exif data failed", e);
72         } catch (NullPointerException e) {
73             // Sometimes the ExifInterface has an internal NPE if Exif data isn't valid
74             Log.w(TAG, "Getting exif data failed", e);
75         } finally {
76             Utils.closeSilently(bis);
77             Utils.closeSilently(is);
78         }
79         return 0;
80     }
81 }
82