Home | History | Annotate | Download | only in util
      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.contacts.util;
     18 
     19 import android.graphics.Bitmap;
     20 import android.graphics.BitmapFactory;
     21 
     22 /**
     23  * Provides static functions to decode bitmaps at the optimal size
     24  */
     25 public class BitmapUtil {
     26     private BitmapUtil() {}
     27 
     28     /**
     29      * Returns Width or Height of the picture, depending on which size is smaller. Doesn't actually
     30      * decode the picture, so it is pretty efficient to run.
     31      */
     32     public static int getSmallerExtentFromBytes(byte[] bytes) {
     33         final BitmapFactory.Options options = new BitmapFactory.Options();
     34 
     35         // don't actually decode the picture, just return its bounds
     36         options.inJustDecodeBounds = true;
     37         BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
     38 
     39         // test what the best sample size is
     40         return Math.min(options.outWidth, options.outHeight);
     41     }
     42 
     43     /**
     44      * Finds the optimal sampleSize for loading the picture
     45      * @param originalSmallerExtent Width or height of the picture, whichever is smaller
     46      * @param targetExtent Width or height of the target view, whichever is bigger.
     47      *
     48      * If either one of the parameters is 0 or smaller, no sampling is applied
     49      */
     50     public static int findOptimalSampleSize(int originalSmallerExtent, int targetExtent) {
     51         // If we don't know sizes, we can't do sampling.
     52         if (targetExtent < 1) return 1;
     53         if (originalSmallerExtent < 1) return 1;
     54 
     55         // test what the best sample size is
     56         int extent = originalSmallerExtent;
     57         int sampleSize = 1;
     58         while ((extent >> 1) >= targetExtent) {
     59             sampleSize <<= 1;
     60             extent >>= 1;
     61         }
     62 
     63         return sampleSize;
     64     }
     65 
     66     /**
     67      * Decodes the bitmap with the given sample size
     68      */
     69     public static Bitmap decodeBitmapFromBytes(byte[] bytes, int sampleSize) {
     70         final BitmapFactory.Options options;
     71         if (sampleSize <= 1) {
     72             options = null;
     73         } else {
     74             options = new BitmapFactory.Options();
     75             options.inSampleSize = sampleSize;
     76         }
     77         return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
     78     }
     79 }
     80