為了添加相框,可以建立一個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