国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android開發(fā)實現(xiàn)去除bitmap無用白色邊框的方法示例

2019-12-12 01:43:06
字體:
供稿:網(wǎng)友

本文實例講述了Android開發(fā)實現(xiàn)去除bitmap無用白色邊框的方法。分享給大家供大家參考,具體如下:

圖示

如下圖所示,之前介紹過Android Bitmap的用法,這里提供的工具類作用是,去除內(nèi)容區(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; //設(shè)置透明度    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++;    }    // 獲取內(nèi)容區(qū)域的寬高    int cropHeight = bitmap.getHeight() - bottom - top;    int cropWidth = bitmap.getWidth() - left - right;    // 獲取內(nèi)容區(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);  }}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android拍照與圖片處理技巧總結(jié)》、《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 治多县| 北安市| 沁水县| 寿宁县| 醴陵市| 县级市| 方正县| 灵寿县| 齐河县| 辽源市| 荔波县| 高要市| 延长县| 丰顺县| 共和县| 廊坊市| 富锦市| 宝山区| 衡水市| 石屏县| 金塔县| 沙坪坝区| 浦城县| 深州市| 星子县| 沾益县| 儋州市| 凌源市| 江口县| 甘谷县| 普格县| 青岛市| 萍乡市| 偏关县| 卓尼县| 溧水县| 龙江县| 宁陕县| 行唐县| 西和县| 盐源县|