1a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim/* 2a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * Copyright (C) 2015 The Android Open Source Project 3a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * 4a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * Licensed under the Apache License, Version 2.0 (the "License"); 5a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * you may not use this file except in compliance with the License. 6a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * You may obtain a copy of the License at 7a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * 8a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * http://www.apache.org/licenses/LICENSE-2.0 9a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * 10a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * Unless required by applicable law or agreed to in writing, software 11a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * distributed under the License is distributed on an "AS IS" BASIS, 12a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * See the License for the specific language governing permissions and 14a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * limitations under the License. 15a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim */ 16a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 17a907614755847b2630561a1e5949b2b416600d97Sungsoo Limpackage com.example.android.supportv4.media.utils; 18a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 19a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.graphics.Bitmap; 20a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.graphics.BitmapFactory; 21a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.util.Log; 22a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 23a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport java.io.BufferedInputStream; 24a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport java.io.IOException; 25a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport java.io.InputStream; 26a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport java.net.HttpURLConnection; 27a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport java.net.URL; 28a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 29a907614755847b2630561a1e5949b2b416600d97Sungsoo Limpublic class BitmapHelper { 30a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim private static final String TAG = "BitmapHelper"; 31a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 32a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim // Max read limit that we allow our input stream to mark/reset. 33a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim private static final int MAX_READ_LIMIT_PER_IMG = 1024 * 1024; 34a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 35a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim public static Bitmap scaleBitmap(Bitmap src, int maxWidth, int maxHeight) { 36a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim double scaleFactor = Math.min( 37a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim ((double) maxWidth)/src.getWidth(), ((double) maxHeight)/src.getHeight()); 38a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim return Bitmap.createScaledBitmap(src, 39a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim (int) (src.getWidth() * scaleFactor), (int) (src.getHeight() * scaleFactor), false); 40a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 41a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 42a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim public static Bitmap scaleBitmap(int scaleFactor, InputStream is) { 43a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim // Get the dimensions of the bitmap 44a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 45a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 46a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim // Decode the image file into a Bitmap sized to fill the View 47a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim bmOptions.inJustDecodeBounds = false; 48a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim bmOptions.inSampleSize = scaleFactor; 49a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 50a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim return BitmapFactory.decodeStream(is, null, bmOptions); 51a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 52a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 53a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim public static int findScaleFactor(int targetW, int targetH, InputStream is) { 54a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim // Get the dimensions of the bitmap 55a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 56a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim bmOptions.inJustDecodeBounds = true; 57a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim BitmapFactory.decodeStream(is, null, bmOptions); 58a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim int actualW = bmOptions.outWidth; 59a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim int actualH = bmOptions.outHeight; 60a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 61a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim // Determine how much to scale down the image 62a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim return Math.min(actualW/targetW, actualH/targetH); 63a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 64a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim 65a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim @SuppressWarnings("SameParameterValue") 66a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim public static Bitmap fetchAndRescaleBitmap(String uri, int width, int height) 67a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim throws IOException { 68a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim URL url = new URL(uri); 69a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim BufferedInputStream is = null; 70a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim try { 71a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 72a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim is = new BufferedInputStream(urlConnection.getInputStream()); 73a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim is.mark(MAX_READ_LIMIT_PER_IMG); 74a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim int scaleFactor = findScaleFactor(width, height, is); 75a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim Log.d(TAG, "Scaling bitmap " + uri + " by factor " + scaleFactor + " to support " 76a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim + width + "x" + height + "requested dimension"); 77a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim is.reset(); 78a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim return scaleBitmap(scaleFactor, is); 79a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } finally { 80a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim if (is != null) { 81a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim is.close(); 82a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 83a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 84a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim } 85a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim} 86