1 /*
2  * Copyright (C) 2014 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 package com.example.android.mediabrowserservice.utils;
17 
18 import android.graphics.Bitmap;
19 import android.graphics.BitmapFactory;
20 
21 import java.io.BufferedInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.HttpURLConnection;
25 import java.net.URL;
26 
27 public class BitmapHelper {
28     private static final String TAG = LogHelper.makeLogTag(BitmapHelper.class);
29 
30     // Max read limit that we allow our input stream to mark/reset.
31     private static final int MAX_READ_LIMIT_PER_IMG = 1024 * 1024;
32 
scaleBitmap(Bitmap src, int maxWidth, int maxHeight)33     public static Bitmap scaleBitmap(Bitmap src, int maxWidth, int maxHeight) {
34        double scaleFactor = Math.min(
35            ((double) maxWidth)/src.getWidth(), ((double) maxHeight)/src.getHeight());
36         return Bitmap.createScaledBitmap(src,
37             (int) (src.getWidth() * scaleFactor), (int) (src.getHeight() * scaleFactor), false);
38     }
39 
scaleBitmap(int scaleFactor, InputStream is)40     public static Bitmap scaleBitmap(int scaleFactor, InputStream is) {
41         // Get the dimensions of the bitmap
42         BitmapFactory.Options bmOptions = new BitmapFactory.Options();
43 
44         // Decode the image file into a Bitmap sized to fill the View
45         bmOptions.inJustDecodeBounds = false;
46         bmOptions.inSampleSize = scaleFactor;
47 
48         return BitmapFactory.decodeStream(is, null, bmOptions);
49     }
50 
findScaleFactor(int targetW, int targetH, InputStream is)51     public static int findScaleFactor(int targetW, int targetH, InputStream is) {
52         // Get the dimensions of the bitmap
53         BitmapFactory.Options bmOptions = new BitmapFactory.Options();
54         bmOptions.inJustDecodeBounds = true;
55         BitmapFactory.decodeStream(is, null, bmOptions);
56         int actualW = bmOptions.outWidth;
57         int actualH = bmOptions.outHeight;
58 
59         // Determine how much to scale down the image
60         return Math.min(actualW/targetW, actualH/targetH);
61     }
62 
63     @SuppressWarnings("SameParameterValue")
fetchAndRescaleBitmap(String uri, int width, int height)64     public static Bitmap fetchAndRescaleBitmap(String uri, int width, int height)
65             throws IOException {
66         URL url = new URL(uri);
67         BufferedInputStream is = null;
68         try {
69             HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
70             is = new BufferedInputStream(urlConnection.getInputStream());
71             is.mark(MAX_READ_LIMIT_PER_IMG);
72             int scaleFactor = findScaleFactor(width, height, is);
73             LogHelper.d(TAG, "Scaling bitmap ", uri, " by factor ", scaleFactor, " to support ",
74                     width, "x", height, "requested dimension");
75             is.reset();
76             return scaleBitmap(scaleFactor, is);
77         } finally {
78             if (is != null) {
79                 is.close();
80             }
81         }
82     }
83 }
84