Java tutorial
//package com.java2s; /* * Copyright (C) 2010 The Android Open Source Project * * 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; import android.graphics.BitmapFactory.Options; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; public class Main { public static Bitmap resizeAndCropCenter(Bitmap bitmap, int size, boolean recycle) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); if (w == size && h == size) return bitmap; // scale the image so that the shorter side equals to the target; // the longer side will be center-cropped. float scale = (float) size / Math.min(w, h); Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap)); int width = Math.round(scale * bitmap.getWidth()); int height = Math.round(scale * bitmap.getHeight()); Canvas canvas = new Canvas(target); canvas.translate((size - width) / 2f, (size - height) / 2f); canvas.scale(scale, scale); Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG); canvas.drawBitmap(bitmap, 0, 0, paint); if (recycle) bitmap.recycle(); return target; } /** * * @param filePath * @param targetWidth * @param targetHeight * @param recycle * @return */ public static Bitmap resizeAndCropCenter(String filePath, int targetWidth, int targetHeight, boolean recycle) { //get sampleBitmap Options opts = new Options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, opts); int scaleWidth = opts.outWidth / targetWidth; int scaleHeight = opts.outHeight / targetHeight; int scale = scaleWidth < scaleHeight ? scaleWidth : scaleHeight; if (scale < 1) { scale = 1; } opts.inJustDecodeBounds = false; opts.inSampleSize = scale; Bitmap sampleBitmap = BitmapFactory.decodeFile(filePath, opts); if (opts.outWidth == -1 || opts.outHeight == -1) { throw new IllegalArgumentException(); } //get scalebitmap float fScaleWidth = targetWidth / ((float) opts.outWidth); float fScaleHeight = targetHeight / ((float) opts.outHeight); float fScale = fScaleWidth > fScaleHeight ? fScaleWidth : fScaleHeight; if (fScale > 1) fScale = 1; Matrix matrix = new Matrix(); matrix.postScale(fScale, fScale); Bitmap scaleBitmap = Bitmap.createBitmap(sampleBitmap, 0, 0, opts.outWidth, opts.outHeight, matrix, true); //get targetBitmap int bitmapX = (scaleBitmap.getWidth() - targetWidth) / 2; bitmapX = bitmapX > 0 ? bitmapX : 0; int bitmapY = (scaleBitmap.getHeight() - targetHeight) / 2; bitmapY = bitmapY > 0 ? bitmapY : 0; targetWidth = targetWidth < (scaleBitmap.getWidth()) ? targetWidth : (scaleBitmap.getWidth()); targetHeight = targetHeight < (scaleBitmap.getHeight()) ? targetHeight : (scaleBitmap.getHeight()); Bitmap targetBitmap = Bitmap.createBitmap(scaleBitmap, bitmapX, bitmapY, targetWidth, targetHeight); if (recycle) sampleBitmap.recycle(); return targetBitmap; } /** * * @param filePath * @param targetWidth * @param targetHeight * @param recycle * @return */ public static Bitmap resizeAndCropCenter(Bitmap bitmap, int targetWidth, int targetHeight, boolean recycle) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); int scaleWidth = w / targetWidth; int scaleHeight = h / targetHeight; int scale = scaleWidth < scaleHeight ? scaleWidth : scaleHeight; if (scale < 1) { scale = 1; } //get scalebitmap float fScaleWidth = targetWidth / ((float) w); float fScaleHeight = targetHeight / ((float) h); float fScale = fScaleWidth > fScaleHeight ? fScaleWidth : fScaleHeight; if (fScale > 1) fScale = 1; Matrix matrix = new Matrix(); matrix.postScale(fScale, fScale); Bitmap scaleBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); //get targetBitmap int bitmapX = (scaleBitmap.getWidth() - targetWidth) / 2; bitmapX = bitmapX > 0 ? bitmapX : 0; int bitmapY = (scaleBitmap.getHeight() - targetHeight) / 2; bitmapY = bitmapY > 0 ? bitmapY : 0; targetWidth = targetWidth < (scaleBitmap.getWidth()) ? targetWidth : (scaleBitmap.getWidth()); targetHeight = targetHeight < (scaleBitmap.getHeight()) ? targetHeight : (scaleBitmap.getHeight()); if (bitmapX == 0 && bitmapY == 0 && targetWidth == w && targetHeight == h) { return bitmap; } Bitmap targetBitmap = Bitmap.createBitmap(scaleBitmap, bitmapX, bitmapY, targetWidth, targetHeight); if (recycle) bitmap.recycle(); return targetBitmap; } private static Bitmap.Config getConfig(Bitmap bitmap) { Bitmap.Config config = bitmap.getConfig(); if (config == null) { config = Bitmap.Config.ARGB_8888; } return config; } }