get Resized Bitmap by width and height - Android Graphics

Android examples for Graphics:Bitmap Resize

Description

get Resized Bitmap by width and height

Demo Code

/*// ww w . j  a  v a2s  .  com
 * Copyright (C) 2012 Kazuya (Kaz) Yokoyama <kazuya.yokoyama@gmail.com>
 *
 * 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.
 */
//package com.java2s;

import java.io.File;
import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Matrix;

public class Main {
    public static Bitmap getResizedBitmap(File file, int targetWidth,
            int targetHeight, float degrees) {
        Bitmap ret = null;

        if (file == null) {
        } else {
            BitmapFactory.Options option = new BitmapFactory.Options();
            Bitmap src = null;
            int sampleSize = 0;

            option.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(file.getAbsolutePath(), option);
            // more than 1MP
            if ((option.outWidth * option.outHeight) > 1048576) {
                double out_area = (double) (option.outWidth * option.outHeight) / 1048576.0;
                sampleSize = (int) (Math.sqrt(out_area) + 1);
            } else {
                sampleSize = 1;
            }

            option.inJustDecodeBounds = false;
            option.inSampleSize = sampleSize;
            src = BitmapFactory.decodeFile(file.getAbsolutePath(), option);
            if (src == null) {
            } else {
                ret = getResizedBitmap(src, targetWidth, targetHeight,
                        degrees);
            }
        }

        return ret;
    }

    public static Bitmap getResizedBitmap(Bitmap src, int targetWidth,
            int targetHeight, float degrees) {
        int srcWidth = src.getWidth();
        int srcHeight = src.getHeight();

        float scale = getFitScale(targetWidth, targetHeight, srcWidth,
                srcHeight);

        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        matrix.postRotate(degrees);

        return Bitmap.createBitmap(src, 0, 0, srcWidth, srcHeight, matrix,
                true);
    }

    public static float getFitScale(int targetWidth, int targetHeight,
            int srcWidth, int srcHeight) {
        float ret = 0;

        if (targetWidth < targetHeight) {
            if (srcWidth < srcHeight) {
                ret = (float) targetHeight / (float) srcHeight;
                if ((srcWidth * ret) > targetWidth) {
                    ret = (float) targetWidth / (float) srcWidth;
                }
            } else {
                ret = (float) targetWidth / (float) srcWidth;
            }
        } else {
            if (srcWidth < srcHeight) {
                ret = (float) targetHeight / (float) srcHeight;
            } else {
                ret = (float) targetWidth / (float) srcWidth;
                if ((srcHeight * ret) > targetHeight) {
                    ret = (float) targetHeight / (float) srcHeight;
                }
            }
        }

        return ret;
    }
}

Related Tutorials