android影像處理系列之四-- 給圖片添加邊框(上)

來源:互聯網
上載者:User

標籤:進度條   tar   tac   png   length   sse   mat   tools   enter   

圖片處理時,有時需要為圖片加一些邊框,下面介紹一種為圖片添加簡單邊框的方法。

基本思路是:將邊框圖片裁剪成八張小圖片(圖片大小最好一致,不然後面處理會很麻煩),分別對應左上方,左邊,左下角,下邊,右下角,右邊,右上方,上邊,其中左右上下只需要一個有效長度,就像重寫水平進度條一樣,只需要一個有效長度,然後平鋪,就達到了最後想要的效果,不錯,左右上下邊採用的也是這樣的思路。也可以將八張圖片組合在一起,然後讀取整張圖片,用代碼裁剪,下面會給出相應的代碼。下面的代碼主要是給出第一種方法,後一種給出代碼,有興趣的可以自己試試。注意圖片不要放到drawable目錄下面,因為螢幕解析度會影響圖片的大小,所以最好是放在assets目錄裡面。下面代碼為了簡便所以沒有那樣做。後面一篇還會講到另一種添加邊框圖片的方法。

下面貼圖片:

原圖片:


處理後:


代碼(res參數為上面所說的八個邊框組合圖片資源):

 

[java] view plain copy
  1. /** 
  2.      * 圖片與邊框組合 
  3.      * @param bm 原圖片 
  4.      * @param res 邊框資源 
  5.      * @return 
  6.      */  
  7.     private Bitmap combinateFrame(Bitmap bm, int[] res)  
  8.     {  
  9.         Bitmap bmp = decodeBitmap(res[0]);  
  10.         // 邊框的寬高  
  11.         final int smallW = bmp.getWidth();  
  12.         final int smallH = bmp.getHeight();  
  13.           
  14.         // 原圖片的寬高  
  15.         final int bigW = bm.getWidth();  
  16.         final int bigH = bm.getHeight();  
  17.           
  18.         int wCount = (int) Math.ceil(bigW * 1.0 / smallW);  
  19.         int hCount = (int) Math.ceil(bigH  * 1.0 / smallH);  
  20.           
  21.         // 組合後圖片的寬高  
  22.         int newW = (wCount + 2) * smallW;  
  23.         int newH = (hCount + 2) * smallH;  
  24.           
  25.         // 重新定義大小  
  26.         Bitmap newBitmap = Bitmap.createBitmap(newW, newH, Config.ARGB_8888);  
  27.         Canvas canvas = new Canvas(newBitmap);  
  28.         Paint p = new Paint();  
  29.         p.setColor(Color.TRANSPARENT);  
  30.         canvas.drawRect(new Rect(0, 0, newW, newH), p);  
  31.           
  32.         Rect rect = new Rect(smallW, smallH, newW - smallW, newH - smallH);  
  33.         Paint paint = new Paint();  
  34.         paint.setColor(Color.WHITE);  
  35.         canvas.drawRect(rect, paint);  
  36.           
  37.         // 繪原圖  
  38.         canvas.drawBitmap(bm, (newW - bigW - 2 * smallW) / 2 + smallW, (newH - bigH - 2 * smallH) / 2 + smallH, null);  
  39.         // 繪邊框  
  40.         // 繪四個角  
  41.         int startW = newW - smallW;  
  42.         int startH = newH - smallH;  
  43.         Bitmap leftTopBm = decodeBitmap(res[0]); // 左上方  
  44.         Bitmap leftBottomBm = decodeBitmap(res[2]); // 左下角  
  45.         Bitmap rightBottomBm = decodeBitmap(res[4]); // 右下角  
  46.         Bitmap rightTopBm = decodeBitmap(res[6]); // 右上方  
  47.           
  48.         canvas.drawBitmap(leftTopBm, 0, 0, null);  
  49.         canvas.drawBitmap(leftBottomBm, 0, startH, null);  
  50.         canvas.drawBitmap(rightBottomBm, startW, startH, null);  
  51.         canvas.drawBitmap(rightTopBm, startW, 0, null);  
  52.           
  53.         leftTopBm.recycle();  
  54.         leftTopBm = null;  
  55.         leftBottomBm.recycle();  
  56.         leftBottomBm = null;  
  57.         rightBottomBm.recycle();  
  58.         rightBottomBm = null;  
  59.         rightTopBm.recycle();  
  60.         rightTopBm = null;  
  61.           
  62.         // 繪左右邊框  
  63.         Bitmap leftBm = decodeBitmap(res[1]);  
  64.         Bitmap rightBm = decodeBitmap(res[5]);  
  65.         for (int i = 0, length = hCount; i < length; i++)  
  66.         {  
  67.             int h = smallH * (i + 1);  
  68.             canvas.drawBitmap(leftBm, 0, h, null);  
  69.             canvas.drawBitmap(rightBm, startW, h, null);  
  70.         }  
  71.           
  72.         leftBm.recycle();  
  73.         leftBm = null;  
  74.         rightBm.recycle();  
  75.         rightBm = null;  
  76.           
  77.         // 繪上下邊框  
  78.         Bitmap bottomBm = decodeBitmap(res[3]);  
  79.         Bitmap topBm = decodeBitmap(res[7]);  
  80.         for (int i = 0, length = wCount; i < length; i++)  
  81.         {  
  82.             int w = smallW * (i + 1);  
  83.             canvas.drawBitmap(bottomBm, w, startH, null);  
  84.             canvas.drawBitmap(topBm, w, 0, null);  
  85.         }  
  86.           
  87.         bottomBm.recycle();  
  88.         bottomBm = null;  
  89.         topBm.recycle();  
  90.         topBm = null;  
  91.           
  92.         canvas.save(Canvas.ALL_SAVE_FLAG);  
  93.         canvas.restore();  
  94.           
  95.         return newBitmap;  
  96.     }  


如果邊框是在一張圖片裡面,下面給出從一張圖片取中間200X200的地區。如何類似邊框過多,可以將裁剪的資訊寫入到指定的檔案,裁剪時可先將邊框圖片資訊讀取出來,然後再裁剪出邊框。如果處理的原圖片太大,可能會出記憶體溢出。可先將圖片縮小到一定尺寸再處理,具體縮小方法,參見android影像處理系列之二--圖片旋轉、縮放、反轉的圖片縮放。

[java] view plain copy
  1. /** 
  2.  * 截取圖片的中間的200X200的地區 
  3.  * @param bm 
  4.  * @return 
  5.  */  
  6. private Bitmap cropCenter(Bitmap bm)  
  7. {  
  8.     int dstWidth = 200;  
  9.        int dstHeight = 200;  
  10.        int startWidth = (bm.getWidth() - dstWidth)/2;  
  11.        int startHeight = ((bm.getHeight() - dstHeight) / 2);  
  12.        Rect src = new Rect(startWidth, startHeight, startWidth + dstWidth, startHeight + dstHeight);  
  13.        return dividePart(bm, src);  
  14. }  
  15.   
  16. /** 
  17.  * 剪下圖片 
  18.  * @param bmp 被剪下的圖片 
  19.  * @param src 剪下的位置 
  20.  * @return 剪下後的圖片 
  21.  */  
  22. private Bitmap dividePart(Bitmap bmp, Rect src)  
  23. {  
  24.     int width = src.width();  
  25.     int height = src.height();  
  26.     Rect des = new Rect(0, 0, width, height);  
  27.     Bitmap croppedImage = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);  
  28.     Canvas canvas = new Canvas(croppedImage);  
  29.     canvas.drawBitmap(bmp, src, des, null);  
  30.     return croppedImage;  
  31. }  


處理後圖片(原圖片還是上面的圖片):

android影像處理系列之四-- 給圖片添加邊框(上)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.