Android Bitmap Decode decodeSampledBitmapFromByte(byte[] res, int reqWidth, int reqHeight)

Here you can find the source of decodeSampledBitmapFromByte(byte[] res, int reqWidth, int reqHeight)

Description

decode Sampled Bitmap From Byte

License

Open Source License

Declaration

public static Bitmap decodeSampledBitmapFromByte(byte[] res,
            int reqWidth, int reqHeight) 

Method Source Code

//package com.java2s;
/*/*from w ww . j  a v  a2  s.  c om*/
 * The MIT License (MIT)
 * 
 * Copyright (c) 2010-2014 Carnegie Mellon University
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {
    public static Bitmap decodeSampledBitmapFromByte(byte[] res,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        try {
            BitmapFactory.decodeByteArray(res, 0, res.length, options);
        } catch (OutOfMemoryError e) {
            return null;
        }

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        try {
            return BitmapFactory.decodeByteArray(res, 0, res.length,
                    options);
        } catch (OutOfMemoryError e) {
            return null;
        }
    }

    public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float) height
                        / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }
        }
        return inSampleSize;
    }
}

Related

  1. decodeBitmap(String path, int maxImageSize)
  2. decodeSampledBitmapFromFile(String filename, int reqWidth, int reqHeight)
  3. decodeSampledBitmap(Uri uri, int reqWidth, int reqHeight, Activity act)
  4. decodeBitmapFromAssets(Context context, String fileName)
  5. decodeImageFile(String strImagePath, BitmapFactory.Options options, boolean checkOrientation)
  6. decodeSampledBitmapFromFile(String file, int reqWidth, int reqHeight)
  7. decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)