Android網路攝影機開發:拍照後添加相框,融合相框和圖片為一副 圖片

來源:互聯網
上載者:User

為了添加相框,可以建立一個bitmap,依此執行個體化一個canvas。然後再上面依次畫上原圖和相框。

在onPictureTaken()函數裡,得到原始bitmap後,得到相框,然後調用融合函數。

Bitmap frame = BitmapFactory.decodeResource(getResources(), R.drawable.border);
 Bitmap monBM = montageBitmap(frame, sizeBitmap, 200, 200);


 

/*將像框和圖片進行融合,返回一個Bitmap*/public Bitmap montageBitmap(Bitmap frame, Bitmap src, int x, int y){int w = src.getWidth();int h = src.getHeight();Bitmap sizeFrame = Bitmap.createScaledBitmap(frame, w, h, true);Bitmap newBM = Bitmap.createBitmap(w, h, Config.ARGB_8888);Canvas canvas = new Canvas(newBM);canvas.drawBitmap(src, x, y, null);canvas.drawBitmap(sizeFrame, 0, 0, null);return newBM;}

程式中frame代表相框,src代表原圖,大小為600*800.首先將相框的大小縮放到600*800,然後執行個體化一個canvas。記住先畫原圖。這裡面有個x、y座標。
這裡是這個api的注釋:

public void drawBitmap (Bitmap bitmap, float left, float top, Paint paint)Added in API level 1Draw the specified bitmap, with its top/left corner at (x,y), using the specified paint, transformed by the current matrix.Note: if the paint contains a maskfilter that generates a mask which extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), then the bitmap will be drawn as if it were in a Shader with CLAMP mode. Thus the color outside of the original width/height will be the edge color replicated.If the bitmap and canvas have different densities, this function will take care of automatically scaling the bitmap to draw at the same density as the canvas.ParametersbitmapThe bitmap to be drawnleftThe position of the left side of the bitmap being drawntopThe position of the top side of the bitmap being drawnpaintThe paint used to draw the bitmap (may be null)

看上面的解釋,貌似不清楚這個x y座標到底是誰的座標,是原圖的 還是canvas的?而且如果要畫的圖超過canvas的大小怎麼辦?經過實際測試,參考這裡,這個x、y座標是指canvas上的,也就是以canvas上的點(x,y)為頂點,來畫圖bitmap。如果bitmap的大小超過canvas的大小,就不顯示了。下面兩組測試圖片可以清楚看到。

 

第一組測試照片(x,y)=(20, 20):

原圖:

 

 原圖+相框:

 

第二組(x,y)=(200, 200):

原圖:

 

原圖+相框: 

可以看到,當傳進去的座標較小時看不出來啥差別。事實上,如果將兩個座標都設為(0,0),看到的是兩個同樣大小的照片層疊的效果。這就看對相框如何定義了。如果要求不遮擋原圖,則需要把原圖縮放到rect大小,這個rect是指相框裡面的空白(透明)部分大小。然後從canvas的透明部分的左上頂點開始畫縮放後的原圖。

http://blog.csdn.net/lgl125/article/details/7866930這個連結是給原圖加邊框的,但不是相框!可以參考。

                                                                                 -----------------------------------------------------------------本文系原創,轉載請註明作者:yanzi1225627

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.