android 扫描文件百分比,android canvas以百分比计算区域彩色像素

博客主要围绕Android开发展开,讲述了绘制画布位图时计算无色区域百分比的问题。作者给出了一个以百分比计算区域彩色像素的方法,通过遍历位图像素,统计透明像素数量,最终计算出透明像素占比。

我绘制了一个画布位图并试图计算无色区域的百分比。 我发现了一些方法,但他们没有计算像素,当我完成绘制屏幕并且我已经离开了一个很小的未知区域时,该方法写下了我完成的。android canvas以百分比计算区域彩色像素

public float percentTransparent(Bitmap bm, int scale) {

final int width = bm.getWidth();

final int height = bm.getHeight();

// size of sample rectangles

final int xStep = width/scale;

final int yStep = height/scale;

// center of the first rectangle

final int xInit = xStep/2;

final int yInit = yStep/2;

// center of the last rectangle

final int xEnd = width - xStep/2;

final int yEnd = height - yStep/2;

int totalTransparent = 0;

for(int x = xInit; x <= xEnd; x += xStep) {

for(int y = yInit; y <= yEnd; y += yStep) {

if (bm.getPixel(x, y) == Color.TRANSPARENT) {

totalTransparent++;

}

}

}

return ((float)totalTransparent)/(scale * scale);

}

这是我找到的方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值