Java tutorial
//package com.java2s; /******************************************************************************* * Copyright 2013 AppGlu, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class Main { /** * Decodes an image byte[] to Bitmap resizing the image to be <code>inSampleSize</code> times smaller then the original. * @param imageBytes image content in byte[] * @param inSampleSize how much smaller that the image will be, for example, <code>inSampleSize</code> equals 2 will return an image 1/2 the size of the original * @return the resized Bitmap */ public static Bitmap decodeSampledBitmapFromByteArray(byte[] imageBytes, int inSampleSize) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = inSampleSize; return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, options); } /** * Decodes an image byte[] to Bitmap resizing the image to fit the <code>requestedWidth</code> and <code>requestedHeight</code> dimensions. * @param imageBytes image content in byte[] * @param requestedWidth the final image width will be close to the requestedWidth (value is in pixels) * @param requestedHeight the final image height will be close to the requestedHeight (value is in pixels) * @return the resized Bitmap */ public static Bitmap decodeSampledBitmapFromByteArray(byte[] imageBytes, int requestedWidth, int requestedHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, options); int inSampleSize = calculateSampleSize(options, requestedWidth, requestedHeight); return decodeSampledBitmapFromByteArray(imageBytes, inSampleSize); } /** * Giving a max width and height in pixels this method will calculate a good value to use in BitmapFactory.Options.inSampleSize. */ public static int calculateSampleSize(BitmapFactory.Options options, int requestedWidth, int requestedHeight) { // Raw height and width of image int width = options.outWidth; int height = options.outHeight; int inSampleSize = 1; if (width > requestedWidth || height > requestedHeight) { // Calculate ratios of height and width to requested height and width final int widthRatio = Math.round((float) width / (float) requestedWidth); final int heightRatio = Math.round((float) height / (float) requestedHeight); // Choose the smallest ratio as inSampleSize value, this will guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } }