前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发实现去除bitmap无用白色边框的方法示例

Android开发实现去除bitmap无用白色边框的方法示例

作者头像
砸漏
发布2020-10-26 18:08:20
1.3K0
发布2020-10-26 18:08:20
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

本文实例讲述了Android开发实现去除bitmap无用白色边框的方法。分享给大家供大家参考,具体如下:

图示

如下图所示,之前介绍过Android Bitmap的用法,这里提供的工具类作用是,去除内容区域以外的白色边框。

代码

代码语言:javascript
复制
import android.graphics.Bitmap;
/**
* Created by Victor Yang on 2016/6/17.
* 去除 bitmap 无用的白色边框
*/
public class BitmapDeleteNoUseSpaceUtil {
/**
* 灰度化 bitmap
* @param imgTheWidth
* @param imgTheHeight
* @param imgThePixels
* @return
*/
private static Bitmap getGrayImg(int imgTheWidth, int imgTheHeight, int[] imgThePixels) {
int alpha = 0xFF << 24; //设置透明度
for (int i = 0; i < imgTheHeight; i++) {
for (int j = 0; j < imgTheWidth; j++) {
int grey = imgThePixels[imgTheWidth * i + j];
int red = ((grey & 0x00FF0000)    16); //获取红色灰度值
int green = ((grey & 0x0000FF00)    8); //获取绿色灰度值
int blue = (grey & 0x000000FF);     //获取蓝色灰度值
grey = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11);
grey = alpha | (grey << 16) | (grey << 8) | grey; //添加透明度
imgThePixels[imgTheWidth * i + j] = grey;  //更改像素色值
}
}
Bitmap result =
Bitmap.createBitmap(imgTheWidth, imgTheHeight, Bitmap.Config.RGB_565);
result.setPixels(imgThePixels, 0, imgTheWidth, 0, 0, imgTheWidth, imgTheHeight);
return result;
}
/**
* 去除多余白框
* @param originBitmap
* @return
*/
public static Bitmap deleteNoUseWhiteSpace(Bitmap originBitmap) {
int[] imgThePixels = new int[originBitmap.getWidth() * originBitmap.getHeight()];
originBitmap.getPixels(
imgThePixels,
0,
originBitmap.getWidth(),
0,
0,
originBitmap.getWidth(),
originBitmap.getHeight());
// 灰度化 bitmap
Bitmap bitmap = getGrayImg(
originBitmap.getWidth(),
originBitmap.getHeight(),
imgThePixels);
int top = 0; // 上边框白色高度
int left = 0; // 左边框白色高度
int right = 0; // 右边框白色高度
int bottom = 0; // 底边框白色高度
for (int h = 0; h < bitmap.getHeight(); h++) {
boolean holdBlackPix = false;
for (int w = 0; w < bitmap.getWidth(); w++) {
if (bitmap.getPixel(w, h) != -1) { // -1 是白色
holdBlackPix = true; // 如果不是-1 则是其他颜色
break;
}
}
if (holdBlackPix) {
break;
}
top++;
}
for (int w = 0; w < bitmap.getWidth(); w++) {
boolean holdBlackPix = false;
for (int h = 0; h < bitmap.getHeight(); h++) {
if (bitmap.getPixel(w, h) != -1) {
holdBlackPix = true;
break;
}
}
if (holdBlackPix) {
break;
}
left++;
}
for (int w = bitmap.getWidth() - 1; w  = 0; w--) {
boolean holdBlackPix = false;
for (int h = 0; h < bitmap.getHeight(); h++) {
if (bitmap.getPixel(w, h) != -1) {
holdBlackPix = true;
break;
}
}
if (holdBlackPix) {
break;
}
right++;
}
for (int h = bitmap.getHeight() - 1; h  = 0; h--) {
boolean holdBlackPix = false;
for (int w = 0; w < bitmap.getWidth(); w++) {
if (bitmap.getPixel(w, h) != -1) {
holdBlackPix = true;
break;
}
}
if (holdBlackPix) {
break;
}
bottom++;
}
// 获取内容区域的宽高
int cropHeight = bitmap.getHeight() - bottom - top;
int cropWidth = bitmap.getWidth() - left - right;
// 获取内容区域的像素点
int[] newPix = new int[cropWidth * cropHeight];
int i = 0;
for (int h = top; h < top + cropHeight; h++) {
for (int w = left; w < left + cropWidth; w++) {
newPix[i++] = bitmap.getPixel(w, h);
}
}
// 创建切割后的 bitmap, 针对彩色图,把 newPix 替换为 originBitmap 的 pixs
return Bitmap.createBitmap(newPix, cropWidth, cropHeight, Bitmap.Config.ARGB_8888);
}
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android图形与图像处理技巧总结》、《Android拍照与图片处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
图片处理
图片处理(Image Processing,IP)是由腾讯云数据万象提供的丰富的图片处理服务,广泛应用于腾讯内部各产品。支持对腾讯云对象存储 COS 或第三方源的图片进行处理,提供基础处理能力(图片裁剪、转格式、缩放、打水印等)、图片瘦身能力(Guetzli 压缩、AVIF 转码压缩)、盲水印版权保护能力,同时支持先进的图像 AI 功能(图像增强、图像标签、图像评分、图像修复、商品抠图等),满足多种业务场景下的图片处理需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档