赞
踩
Android的Bitmap看起来挺好用,下面的代码只是大致思路,未必能运行,性能也是呵呵哒,切勿当真
Bitmap bitmap = BitmapFactory.decodeFile("src.png");
int top = 0, right = 0, bottom = 0, left = 0;
int w = bitmap.getWidth();
int h = bitmap.getHeight();
for (int i = 0; i < h; i++) {
boolean blank = true;
for (int j = 0; j < w; i++) {
int color = bitmap.getPixel(j, i);
if (Color.alpha(color) != 0xff) {
blank = false;
}
}
if (blank) {
top++;
}
blank =true;
for (int j = 0; j < w; i++) {
int color = bitmap.getPixel(j, h-i-1);
if (Color.alpha(color) != 0xff) {
blank = false;
}
}
if (blank) {
bottom++;
}
}
for (int i = 0; i < w; i++) {
boolean blank = true;
for (int j = 0; j < h; i++) {
int color = bitmap.getPixel(i, j);
if (Color.alpha(color) != 0xff) {
blank = false;
}
}
if (blank) {
left++;
}
blank =true;
for (int j = 0; j < w; i++) {
int color = bitmap.getPixel(h-i-1,j);
if (Color.alpha(color) != 0xff) {
blank = false;
}
}
if (blank) {
right++;
}
}
Bitmap dst = Bitmap.createBitmap(bitmap, left, top, w - left - right, h - top - bottom);
FileOutputStream outputStream = new FileOutputStream("dst.png");
dst.compress(Bitmap.CompressFormat.PNG,100,outputStream);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。