為了添加相框,可以新建一個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 1
Draw 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.
Parameters
bitmap The bitmap to be drawn
left The position of the left side of the bitmap being drawn
top The position of the top side of the bitmap being drawn
paint The paint used to draw the bitmap (may be null)
~~~
看上面的解釋,貌似不清楚這個x y坐標到底是誰的坐標,是原圖的 還是canvas的?而且如果要畫的圖超過canvas的大小怎么辦?經過實際測試,參考[這里](http://book.51cto.com/art/201204/328278.htm),這個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](http://blog.csdn.net/lgl125/article/details/7866930)這個鏈接是給原圖加邊框的,但不是相框!可以參考。
??????????????????????????????????????????????????????????????????????????????? -----------------------------------------------------------------本文系原創,轉載請注明作者:yanzi1225627
- 前言
- Linux下使用QT調用opencv讀取攝像頭視頻 調試心得
- Android開發 攝像頭SurfaceView預覽 背景帶矩形框 實現(原理:雙surfaceview,頂層畫矩形框,底層預覽視頻)
- Android開發:安裝NDK,移植OpenCV2.3.1,JNI調用OpenCV全過程
- 2013新春奉送:Android攝像頭開發完美demo---(循環聚焦,縮放大小,旋轉picture,查詢支持的picturesize, ImageButton按鍵效果)
- 如何設置ImageButton按鍵按下去后的 特效----(如類似風車旋轉的動畫特效)
- Android攝像頭:只拍攝SurfaceView預覽界面特定區域內容(矩形框)---完整實現(原理:底層SurfaceView+上層繪制ImageView)
- Android開發:SurfaceView上新建線程繪制旋轉圖片 及 刷新特定區域(臟矩形)
- Android開發:ImageView上繪制旋轉圓環(透明度不同的旋轉圓環,利用canvas.drawArc實現)
- Android上掌紋識別第一步:基于OpenCV的6種膚色分割 源碼和效果圖
- Android開發:實時處理攝像頭預覽幀視頻------淺析PreviewCallback,onPreviewFrame,AsyncTask的綜合應用
- Android攝像頭開發:拍照后添加相框,融合相框和圖片為一副 圖片
- Android(OpenCV) NDK開發: 0xdeadbaad(code=1)錯誤 及 關閉armeabi和libnative_camera_r2.2.2.so的生成
- Android攝像頭開發:實時攝像頭視頻預覽幀的編碼問題(二)
- setContentView切換頁面(無需每次都findViewById)-----二
- Android開發:setContentView切換界面,自定義帶CheckBox的ListView顯示SQlite條目-----實現