Android開發(fā)實現(xiàn)去除bitmap無用白色邊框的方法示例
本文實例講述了Android開發(fā)實現(xiàn)去除bitmap無用白色邊框的方法。分享給大家供大家參考,具體如下:
圖示
如下圖所示,之前介紹過Android Bitmap的用法,這里提供的工具類作用是,去除內容區(qū)域以外的白色邊框。
代碼
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++; } // 獲取內容區(qū)域的寬高 int cropHeight = bitmap.getHeight() - bottom - top; int cropWidth = bitmap.getWidth() - left - right; // 獲取內容區(qū)域的像素點 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); } } // 創(chuàng)建切割后的 bitmap, 針對彩色圖,把 newPix 替換為 originBitmap 的 pixs return Bitmap.createBitmap(newPix, cropWidth, cropHeight, Bitmap.Config.ARGB_8888); } }
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結》、《Android拍照與圖片處理技巧總結》、《Android開發(fā)入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
- Android Activity之間傳遞圖片(Bitmap)的方法
- android保存Bitmap圖片到指定文件夾示例
- Android截取視頻幀并轉化為Bitmap示例
- android中Bitmap的放大和縮小實例代碼
- 解析Android開發(fā)優(yōu)化之:對Bitmap的內存優(yōu)化詳解
- Android讀取本地或網絡圖片并轉換為Bitmap
- android通過bitmap生成新圖片關鍵性代碼
- Android中使用Bitmap類將矩形圖片轉為圓形的方法
- Android canvas drawBitmap方法詳解及實例
- Android實現(xiàn)波浪線效果(xml bitmap)
- Android App開發(fā)中將View或Drawable轉為Bitmap的方法
- android中Bitmap用法(顯示,保存,縮放,旋轉)實例分析
相關文章
深入剖析Android系統(tǒng)中Service和IntentService的區(qū)別
這篇文章主要介紹了Android系統(tǒng)中Service和IntentService的區(qū)別,與普通的服務相比,IntentService可以開啟單獨的線程來處理intent請求,需要的朋友可以參考下2016-04-04Android ListView里控件添加監(jiān)聽方法的實例詳解
這篇文章主要介紹了Android ListView里控件添加監(jiān)聽方法的實例詳解的相關資料,這里提供實例幫助大家學習理解這部分內容,需要的朋友可以參考下2017-08-08Flutter利用ORM框架簡化本地數(shù)據(jù)庫管理詳解
使用?sqflite?相對來說還是有點復雜,比如遇到數(shù)據(jù)不兼容的時候需要手動轉換,增加了不少繁瑣的代碼。本篇我們就來介紹一個?ORM?框架,來簡化數(shù)據(jù)庫的管理,感興趣的可以了解一下2023-04-04android studio節(jié)省C盤空間的配置方法
這篇文章主要介紹了android studio節(jié)省C盤空間的配置方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-07-07Android CheckBox中設置padding無效解決辦法
這篇文章主要介紹了Android CheckBox中設置padding無效解決辦法的相關資料,希望通過本文能幫助到大家,讓大家解決這樣類似的問題,需要的朋友可以參考下2017-10-10基于Android RecyclerView實現(xiàn)宮格拖拽效果
在Android發(fā)展的進程中,網格布局一直比較有熱度,其中一個原因是對用戶來說便捷操作,對app廠商而言也會帶來很多的曝光量,本篇我們會使用RecyclerView來實現(xiàn)網格拖拽,本篇將結合圖片分片案例,實現(xiàn)拖拽效果,需要的朋友可以參考下2024-03-03