Camera 相機詳解(下)

image

前言


實現(xiàn)思路

  1. 在相機開始預(yù)覽后,調(diào)用startFaceDetection()方法開啟人臉檢測

  2. 設(shè)置人臉檢測回調(diào)setFaceDetectionListener(FaceDetectionListener listener)

  3. 自定義一個FaceView,繪制人臉矩形區(qū)域

  4. 在人臉檢測回調(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()
    }
}

在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)
    }

接下來,我們運行看一下效果:

image

汗~~那個矩形怎么位置不對呀....

還記得在第一篇文章中將的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
    }

修改完后,我們在運行看一下效果:

image
image

總結(jié)

本篇文章主要給小伙伴們介紹了如果使用系統(tǒng)自帶的人臉檢測功能檢測人臉,并結(jié)合一個簡單的自定義view,將其繪制在屏幕上

需要主要的是,系統(tǒng)所返回的人臉信息中使用的坐標(biāo)系,我們需要進行轉(zhuǎn)換成安卓屏幕坐標(biāo)系后才能正確地顯示人臉位置

作者:Smashing丶
鏈接:http://www.itdecent.cn/p/3bb301c302e8
來源:簡書
簡書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請聯(lián)系作者獲得授權(quán)并注明出處。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容