Here you can find the source of scaleImg(File file, int newWidth, int newHeight)
public static Bitmap scaleImg(File file, int newWidth, int newHeight)
/*//from w ww . ja v a2s . c o m * Copyright (C) 2013 www.418log.org * * 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: * * 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 java.io.File; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; public class Main { public static Bitmap scaleImg(File file, int newWidth, int newHeight) { Bitmap resizeBmp = null; if (newWidth <= 0 || newHeight <= 0) { throw new IllegalArgumentException( "???????????????0"); } BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeFile(file.getPath(), opts); int srcWidth = opts.outWidth; int srcHeight = opts.outHeight; int destWidth = srcWidth; int destHeight = srcHeight; float scale = 0; float scaleWidth = (float) newWidth / srcWidth; float scaleHeight = (float) newHeight / srcHeight; if (scaleWidth > scaleHeight) { scale = scaleWidth; } else { scale = scaleHeight; } if (scale != 0) { destWidth = (int) (destWidth / scale); destHeight = (int) (destHeight / scale); } opts.inPreferredConfig = Bitmap.Config.RGB_565; opts.inPurgeable = true; opts.inInputShareable = true; if (scale > 1) { opts.inSampleSize = (int) scale; } else { opts.inSampleSize = 1; } opts.outHeight = destHeight; opts.outWidth = destWidth; opts.inJustDecodeBounds = false; opts.inDither = false; resizeBmp = BitmapFactory.decodeFile(file.getPath(), opts); if (resizeBmp != null && scale != 1) { resizeBmp = scaleImg(resizeBmp, scale); } return resizeBmp; } public static Bitmap scaleImg(Bitmap bitmap, int newWidth, int newHeight) { Bitmap resizeBmp = null; if (bitmap == null) { return null; } if (newWidth <= 0 || newHeight <= 0) { throw new IllegalArgumentException( "???????????????0"); } int srcWidth = bitmap.getWidth(); int srcHeight = bitmap.getHeight(); if (srcWidth <= 0 || srcHeight <= 0) { return null; } float scale = 0; float scaleWidth = (float) newWidth / srcWidth; float scaleHeight = (float) newHeight / srcHeight; if (scaleWidth > scaleHeight) { scale = scaleWidth; } else { scale = scaleHeight; } if (bitmap != null && scale != 1) { resizeBmp = scaleImg(bitmap, scale); } return resizeBmp; } public static Bitmap scaleImg(Bitmap bitmap, float scale) { Bitmap resizeBmp = null; try { int bmpW = bitmap.getWidth(); int bmpH = bitmap.getHeight(); Matrix mt = new Matrix(); mt.postScale(scale, scale); resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bmpW, bmpH, mt, true); } catch (Exception e) { e.printStackTrace(); } finally { if (resizeBmp != bitmap) { bitmap.recycle(); } } return resizeBmp; } }