最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年11月10日 10:46:30   作者:AAA啊哈  
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)去除bitmap無用白色邊框的方法,結(jié)合實(shí)例形式給出了Android去除bitmap無用白色邊框的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了Android開發(fā)實(shí)現(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);     //獲取藍(lán)色灰度值
        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ū)域的像素點(diǎn)
    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, 針對(duì)彩色圖,把 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ā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

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

相關(guān)文章

  • Android RecyclerView使用方法解析

    Android RecyclerView使用方法解析

    這篇文章主要為大家詳細(xì)解析了Android RecyclerView使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android自定義ListView實(shí)現(xiàn)下拉刷新

    Android自定義ListView實(shí)現(xiàn)下拉刷新

    這篇文章主要為大家詳細(xì)介紹了Android自定義ListView實(shí)現(xiàn)下拉刷新的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Flutter 狀態(tài)管理的實(shí)現(xiàn)

    Flutter 狀態(tài)管理的實(shí)現(xiàn)

    這篇文章主要介紹了Flutter 狀態(tài)管理的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Android DrawerLayout布局與NavigationView導(dǎo)航菜單應(yīng)用

    Android DrawerLayout布局與NavigationView導(dǎo)航菜單應(yīng)用

    這篇文章主要介紹了Android DrawerLayout抽屜布局與NavigationView導(dǎo)航菜單應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-01-01
  • Android實(shí)現(xiàn)通話自動(dòng)錄音

    Android實(shí)現(xiàn)通話自動(dòng)錄音

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)通話自動(dòng)錄音,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Android仿制淘寶滾動(dòng)圖文條的示例代碼

    Android仿制淘寶滾動(dòng)圖文條的示例代碼

    這篇文章主要介紹了Android仿制淘寶滾動(dòng)圖文條的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • android webView截圖的4種方法

    android webView截圖的4種方法

    這篇文章主要為大家詳細(xì)介紹了android webView截圖的4種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android如何實(shí)現(xiàn)底部菜單固定到底部

    Android如何實(shí)現(xiàn)底部菜單固定到底部

    這篇文章主要介紹了Android如何實(shí)現(xiàn)底部菜單固定到底部,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Presenting?Streams?in?Flutter小技巧

    Presenting?Streams?in?Flutter小技巧

    這篇文章主要為大家介紹了Presenting?Streams?in?Flutter小技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Android自定View實(shí)現(xiàn)滑動(dòng)驗(yàn)證效果的代碼

    Android自定View實(shí)現(xiàn)滑動(dòng)驗(yàn)證效果的代碼

    這篇文章主要介紹了Android自定View實(shí)現(xiàn)滑動(dòng)驗(yàn)證效果,代碼分為自定義屬性代碼和自定義view代碼及使用方法,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-12-12

最新評(píng)論

伊金霍洛旗| 永平县| 赤峰市| 梁河县| 阿合奇县| 大丰市| 湖北省| 茂名市| 阳山县| 休宁县| 安阳市| 米泉市| 南溪县| 广水市| 高邮市| 思茅市| 广昌县| 抚顺市| 通海县| 抚远县| 靖州| 布拖县| 武邑县| 长葛市| 福泉市| 辽中县| 澄迈县| 清涧县| 连云港市| 思茅市| 和田市| 右玉县| 浦江县| 保康县| 连平县| 太白县| 宕昌县| 天津市| 佳木斯市| 孙吴县| 泸定县|