当前位置:   article > 正文

java裁剪png图片_怎么通过 Java 将一张图片的四周的透明区域剪切掉?

java裁剪去掉图片四周透明部分

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);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/604386
推荐阅读
相关标签
  

闽ICP备14008679号