scale Bitmap Image By Path - Android Graphics

Android examples for Graphics:Bitmap Scale

Description

scale Bitmap Image By Path

Demo Code


//package com.java2s;

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

import android.graphics.Matrix;

import android.text.TextUtils;

import java.io.File;
import java.io.FileInputStream;

public class Main {

    public static Bitmap scaleImageByPath(String pathString, int dstMaxWH) {
        Bitmap retBm = null;//  ww w .j av  a  2 s  . c om
        // 
        if (TextUtils.isEmpty(pathString) || dstMaxWH <= 0) {
            return retBm;
        }
        File file = new File(pathString);
        if (!file.exists()) {
            return retBm;
        }
        try {
            // 
            Bitmap srcBitmap;
            java.io.InputStream is = new FileInputStream(pathString);
            BitmapFactory.Options opts = getOptionsWithInSampleSize(
                    pathString, dstMaxWH);
            srcBitmap = BitmapFactory.decodeStream(is, null, opts);
            if (srcBitmap == null) {
                return retBm;
            }
            // 
            int width = srcBitmap.getWidth();
            int height = srcBitmap.getHeight();
            // 
            float scale = 1.f;
            if (width > dstMaxWH || height > dstMaxWH) {
                float scaleTemp = (float) dstMaxWH / (float) width;
                float scaleTemp2 = (float) dstMaxWH / (float) height;
                if (scaleTemp > scaleTemp2) {
                    scale = scaleTemp2;
                } else {
                    scale = scaleTemp;
                }
            }
            // 
            Bitmap dstBitmap;
            if (scale == 1.f) {
                dstBitmap = srcBitmap;
            } else {
                Matrix matrix = new Matrix();
                matrix.postScale(scale, scale);
                dstBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, width,
                        height, matrix, true);
                if (!srcBitmap.isRecycled())
                    srcBitmap.recycle();
                srcBitmap = null;
            }
            retBm = dstBitmap;
        } catch (Exception e) {
            return retBm;
        }
        return retBm;
    }

    public static BitmapFactory.Options getOptionsWithInSampleSize(
            String filePath, int maxWidth) {
        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inJustDecodeBounds = true;// outHeight()
        // outWidth()
        BitmapFactory.decodeFile(filePath, bitmapOptions);
        bitmapOptions.inJustDecodeBounds = false;
        int inSampleSize = bitmapOptions.outWidth / (maxWidth / 10);// 16016
        if (inSampleSize % 10 != 0) {
            inSampleSize += 10;// 
        }
        inSampleSize = inSampleSize / 10;
        if (inSampleSize <= 0) {// 200
            inSampleSize = 1;// 
        }
        bitmapOptions.inSampleSize = inSampleSize;
        return bitmapOptions;
    }
}

Related Tutorials