Java tutorial
//package com.java2s; /***************************************************************************** * BitmapUtil.java ***************************************************************************** * Copyright 2011-2014 VLC authors and VideoLAN * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ import android.graphics.Bitmap; public class Main { public static Bitmap cropBorders(Bitmap bitmap, int width, int height) { int top = 0; for (int i = 0; i < height / 2; i++) { int pixel1 = bitmap.getPixel(width / 2, i); int pixel2 = bitmap.getPixel(width / 2, height - i - 1); if ((pixel1 == 0 || pixel1 == -16777216) && (pixel2 == 0 || pixel2 == -16777216)) { top = i; } else { break; } } int left = 0; for (int i = 0; i < width / 2; i++) { int pixel1 = bitmap.getPixel(i, height / 2); int pixel2 = bitmap.getPixel(width - i - 1, height / 2); if ((pixel1 == 0 || pixel1 == -16777216) && (pixel2 == 0 || pixel2 == -16777216)) { left = i; } else { break; } } if (left >= width / 2 - 10 || top >= height / 2 - 10) return bitmap; // Cut off the transparency on the borders return Bitmap.createBitmap(bitmap, left, top, (width - (2 * left)), (height - (2 * top))); } }