Android camera2.params.face矩形放置在画布上

时间:2015-06-10 03:14:40

标签: java android canvas camera face-detection

我正在尝试在相机预览中实现脸部检测。我按照Android参考页面在while arr2[i] != nil && num > arr2[i] 中实现了自定义相机预览,放置在TextureView中。此FrameLayout中还有FrameLayout,背景清晰(与相机预览重叠)。每次更新相机预览时,我的应用都会将SurfaceView面部边界动态识别的Rect动态显示到CaptureResult.STATISTICS_FACES的画布上每帧)。我的应用假设只需要识别一张脸。

绘制矩形时出现问题。如果我将脸保持在摄像机视图的中心,我将矩形放在正确的位置,但是当我向上移动头部时,矩形向右移动,当我向右移动头部时,矩形向下移动。好像画布需要旋转-90,但这对我不起作用(下面的代码解释)。

在我的活动中SurfaceView

onCreate()
//face recognition rectangleView = (SurfaceView) findViewById(R.id.rectangleView); rectangleView.setZOrderOnTop(true); rectangleView.getHolder().setFormat( PixelFormat.TRANSPARENT); //remove black background from view purplePaint = new Paint(); purplePaint.setColor(Color.rgb(175,0,255)); purplePaint.setStyle(Paint.Style.STROKE); 中的

(在封装TextureView.SurfaceTextureListener.onSurfaceTextureAvailable()的{​​{1}}块中:

try{}

camera.open()内的同一个监听器中

Rect cameraBounds = cameraCharacteristics.get(
    CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
cameraWidth = cameraBounds.right;
cameraHeight = cameraBounds.bottom;
onSurfaceTextureUpdated() looper调用的if (detectedFace != null && rectangleFace.height() > 0) { Canvas currentCanvas = rectangleView.getHolder().lockCanvas(); if (currentCanvas != null) { currentCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); int canvasWidth = currentCanvas.getWidth(); int canvasHeight = currentCanvas.getHeight(); int l = rectangleFace.right; int t = rectangleFace.bottom; int r = rectangleFace.left; int b = rectangleFace.top; int left = (canvasWidth*l)/cameraWidth; int top = (canvasHeight*t)/cameraHeight; int right = (canvasWidth*r)/cameraWidth; int bottom = (canvasHeight*b)/cameraHeight; currentCanvas.drawRect(left, top, right, bottom, purplePaint); } rectangleView.getHolder().unlockCanvasAndPost(currentCanvas); } 中的

方法onCaptureCompleted

CameraCaptureSession.CameraCallback

所有变量都在函数之外实例化。

如果您无法理解我的问题或需要其他信息,请在此处发布类似的问题:

How can i handle the rotation issue with Preview & FaceDetection

然而,与上面的海报不同,旋转画布后我甚至无法让画布显示矩形,因此无法解决问题。

我尝试使用x = -y,y = x(left = -top,top = left)将我的点旋转-90度,并且它也没有做到这一点。我觉得某些功能需要应用于这些要点,但我不知道该怎么做。

有关如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:3)

为了将来参考,这是我最终得到的解决方案:

设置一个名为orientation_offset的类/活动变量:

orientation_offset = cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);

这是需要旋转相机传感器的视图(或用于面部检测的矩形)才能正确查看的角度。

然后,我改变了onSurfaceTextureUpdated()

Canvas currentCanvas = rectangleView.getHolder().lockCanvas();
    if (currentCanvas != null) {

        currentCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

        if (detectedFace != null && rectangleFace.height() > 0) {

            int canvasWidth = currentCanvas.getWidth();
            int canvasHeight = currentCanvas.getHeight();
            int faceWidthOffset = rectangleFace.width()/8;
            int faceHeightOffset = rectangleFace.height()/8;

            currentCanvas.save();
            currentCanvas.rotate(360 - orientation_offset, canvasWidth / 2, 
                canvasHeight / 2);

            int l = rectangleFace.right;
            int t = rectangleFace.bottom;
            int r = rectangleFace.left;
            int b = rectangleFace.top;
            int left = (canvasWidth - (canvasWidth*l)/cameraWidth)-(faceWidthOffset);
            int top  = (canvasHeight*t)/cameraHeight - (faceHeightOffset);
            int right = (canvasWidth - (canvasWidth*r)/cameraWidth) + (faceWidthOffset);
            int bottom = (canvasHeight*b)/cameraHeight + (faceHeightOffset);

            currentCanvas.drawRect(left, top, right, bottom, purplePaint);
            currentCanvas.restore();
        }
    }
    rectangleView.getHolder().unlockCanvasAndPost(currentCanvas);

如果其他人有解决方案可以提供,我会将问题保持打开状态。