前言
在上一篇文章中抹缕,給小伙伴們介紹了如何使用Camera類實現(xiàn)預(yù)覽拍照等功能《Android: Camera相機開發(fā)詳解(中) ——實現(xiàn)預(yù)覽泰讽、拍照、保存照片等功能》
如果有小伙伴對于Camera相關(guān)的知識還不太了解的話,建議先去看這篇文章《Android: Camera相機開發(fā)詳解(上) —— 知識儲備》
本篇文章是在上篇文章的基礎(chǔ)之上囚玫,在預(yù)覽的時候加入人臉檢測功能媒楼,并自定義一個view顯示在預(yù)覽畫面上
實現(xiàn)思路
在相機開始預(yù)覽后乐尊,調(diào)用startFaceDetection()方法開啟人臉檢測
設(shè)置人臉檢測回調(diào)setFaceDetectionListener(FaceDetectionListener listener)
自定義一個FaceView,繪制人臉矩形區(qū)域
在人臉檢測回調(diào)中划址,將檢測到的人臉信息傳遞給自定義的FaceView扔嵌,F(xiàn)aceView根據(jù)人臉信息中矩形位置繪制矩形,然后重新繪制FaceView
具體實現(xiàn)步驟
一夺颤、 開始人臉檢測痢缎,添加回調(diào)方法
private fun startFaceDetect() {
mCamera?.let {
it.startFaceDetection() //開始人臉檢測
it.setFaceDetectionListener { faces, _ ->
mCallBack?.onFaceDetect(transForm(faces))
log("檢測到 ${faces.size} 張人臉")
}
}
}
在人臉檢測的回調(diào)中第一個參數(shù)就是返回的人臉信息
注意,每個相機所支持的檢測到的最大人臉數(shù)是不同的世澜,
如:vivo x9 后置攝像頭支持的最大人臉數(shù)是10
二独旷、自定義一個FaceView,繪制人臉?biāo)谖恢玫木匦?/h2>
class FaceView : View {
lateinit var mPaint: Paint
private var mCorlor = "#42ed45"
var mFaces: ArrayList<RectF>? = null //人臉信息
constructor(context: Context) : super(context) {
init()
}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
init()
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init()
}
private fun init() {
mPaint = Paint()
mPaint.color = Color.parseColor(mCorlor)
mPaint.style = Paint.Style.STROKE
mPaint.strokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1f, context.resources.displayMetrics)
mPaint.isAntiAlias = true
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
mFaces?.let {
for (face in it) { //因為會同時存在多張人臉寥裂,所以用循環(huán)
canvas.drawRect(face, mPaint) //繪制人臉?biāo)谖恢玫木匦? }
}
}
fun setFaces(faces: ArrayList<RectF>) { //設(shè)置人臉信息嵌洼,然后刷新FaceView
this.mFaces = faces
invalidate()
}
}
class FaceView : View {
lateinit var mPaint: Paint
private var mCorlor = "#42ed45"
var mFaces: ArrayList<RectF>? = null //人臉信息
constructor(context: Context) : super(context) {
init()
}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
init()
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init()
}
private fun init() {
mPaint = Paint()
mPaint.color = Color.parseColor(mCorlor)
mPaint.style = Paint.Style.STROKE
mPaint.strokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1f, context.resources.displayMetrics)
mPaint.isAntiAlias = true
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
mFaces?.let {
for (face in it) { //因為會同時存在多張人臉寥裂,所以用循環(huán)
canvas.drawRect(face, mPaint) //繪制人臉?biāo)谖恢玫木匦? }
}
}
fun setFaces(faces: ArrayList<RectF>) { //設(shè)置人臉信息嵌洼,然后刷新FaceView
this.mFaces = faces
invalidate()
}
}
在xml布局文件中定義一個FaceView
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.cs.camerademo.view.FaceView
android:id="@+id/faceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
三、將檢測到的人臉信息傳遞給FaceView
override fun onFaceDetect(faces: ArrayList<RectF>) {
faceView.setFaces(faces)
}
接下來封恰,我們運行看一下效果:
汗~~那個矩形怎么位置不對呀....
還記得在第一篇文章中將的Face這個類么麻养?里面有一個Rect對象,這個對象就是人臉位置的矩形诺舔,但是這個矩形所用的坐標(biāo)并不是安卓屏幕所用的坐標(biāo)鳖昌,源碼是這么描述的:
* Bounds of the face. (-1000, -1000) represents the top-left of the
* camera field of view, and (1000, 1000) represents the bottom-right of
* the field of view. For example, suppose the size of the viewfinder UI
* is 800x480\. The rect passed from the driver is (-1000, -1000, 0, 0).
* The corresponding viewfinder rect should be (0, 0, 400, 240). It is
* guaranteed left < right and top < bottom. The coordinates can be
* smaller than -1000 or bigger than 1000. But at least one vertex will
* be within (-1000, -1000) and (1000, 1000).
大致翻譯一下:
人臉的邊界。它所使用的坐標(biāo)系中低飒,左上角的坐標(biāo)是(-1000许昨,-1000),右下角的坐標(biāo)是(1000,1000)
例如:假設(shè)屏幕的尺寸是800 * 480褥赊,有一個矩形在相機的坐標(biāo)系中的位置是(-1000,-1000,0,0)糕档,它相對應(yīng)的在安卓屏幕坐標(biāo)系中的位置就是(0,0,400,240)
看到這相信小伙伴們已經(jīng)明白了,在人臉檢測返回的人臉信息中所使用的坐標(biāo)系與我們安卓屏幕的坐標(biāo)系并不一致拌喉,所以才會出現(xiàn)上圖那樣矩形框與人臉不重合的問題速那。
下面我們要對坐標(biāo)系進行一個轉(zhuǎn)換:
//將相機中用于表示人臉矩形的坐標(biāo)轉(zhuǎn)換成UI頁面的坐標(biāo)
fun transForm(faces: Array<Camera.Face>): ArrayList<RectF> {
val matrix = Matrix()
// Need mirror for front camera.
val mirror = (mCameraFacing == Camera.CameraInfo.CAMERA_FACING_FRONT)
matrix.setScale(if (mirror) -1f else 1f, 1f)
// This is the value for android.hardware.Camera.setDisplayOrientation.
matrix.postRotate(mDisplayOrientation.toFloat())
// Camera driver coordinates range from (-1000, -1000) to (1000, 1000).
// UI coordinates range from (0, 0) to (width, height).
matrix.postScale(mSurfaceView.width / 2000f, mSurfaceView.height / 2000f)
matrix.postTranslate(mSurfaceView.width / 2f, mSurfaceView.height / 2f)
val rectList = ArrayList<RectF>()
for (face in faces) {
var srcRect = RectF(face.rect)
var dstRect = RectF(0f, 0f, 0f, 0f)
matrix.mapRect(dstRect, srcRect)
rectList.add(dstRect)
}
return rectList
}
修改完后,我們在運行看一下效果:
總結(jié)
本篇文章主要給小伙伴們介紹了如果使用系統(tǒng)自帶的人臉檢測功能檢測人臉司光,并結(jié)合一個簡單的自定義view琅坡,將其繪制在屏幕上
需要主要的是,系統(tǒng)所返回的人臉信息中使用的坐標(biāo)系残家,我們需要進行轉(zhuǎn)換成安卓屏幕坐標(biāo)系后才能正確地顯示人臉位置
作者:Smashing丶
鏈接:http://www.reibang.com/p/3bb301c302e8
來源:簡書
簡書著作權(quán)歸作者所有榆俺,任何形式的轉(zhuǎn)載都請聯(lián)系作者獲得授權(quán)并注明出處。