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