简单的android Q 摄像头预览操作

CTS camera 测试里面有很多操作摄像头的用例, 可以借用来使用

比如: CaptureRequestTest#testFaceDetection

adb shell am instrument -r -e class android.hardware.camera2.cts.CaptureRequestTest#testFaceDetection -w android.camera.cts/androidx.test.runner.AndroidJUnitRunner

    /**
     * Test face detection modes and results.
     */
    @Test
    public void testFaceDetection() throws Exception {
        for (int i = 0; i < mCameraIds.length; i++) {  循环打开前后摄
            try {
                if (!mAllStaticInfo.get(mCameraIds[i]).isColorOutputSupported()) {
                    Log.i(TAG, "Camera " + mCameraIds[i] +
                            " does not support color outputs, skipping");
                    continue;
                }
                openDevice(mCameraIds[i]);  打开摄像头
                faceDetectionTestByCamera();
            } finally {
                closeDevice(); 关闭当前摄像头
            }
        }
    }
    private void faceDetectionTestByCamera() throws Exception {
        int[] faceDetectModes = mStaticInfo.getAvailableFaceDetectModesChecked();

        SimpleCaptureCallback listener;
        CaptureRequest.Builder requestBuilder =
                mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);

        Size maxPreviewSz = mOrderedPreviewSizes.get(0); // Max preview size.
        for (int mode : faceDetectModes) {
            设置人脸检测模式
            requestBuilder.set(CaptureRequest.STATISTICS_FACE_DETECT_MODE, mode);
            if (VERBOSE) {
                Log.v(TAG, "Start testing face detection mode " + mode);
            }

            // Create a new listener for each run to avoid the results from one run spill
            // into another run.
            listener = new SimpleCaptureCallback();创建简单的capturecallback类型的listener
            startPreview(requestBuilder, maxPreviewSz, listener); 开始预览
            waitForSettingsApplied(listener, NUM_FRAMES_WAITED_FOR_UNKNOWN_LATENCY);
            SystemClock.sleep(10000);睡眠10秒
            删除以下这句,仅仅预览
            //verifyFaceDetectionResults(listener, NUM_FACE_DETECTION_FRAMES_VERIFIED, mode);
        }

        stopPreview();关闭当前预览
    }
    protected void startPreview(CaptureRequest.Builder request, Size previewSz,
            CaptureCallback listener) throws Exception {
        // Update preview size.
        updatePreviewSurface(previewSz);
        if (VERBOSE) {
            Log.v(TAG, "start preview with size " + mPreviewSize.toString());
        }

        configurePreviewOutput(request);配置预览
        把request, listener, mHandler 这几个关联起来
        mSession.setRepeatingRequest(request.build(), listener, mHandler);
    }

 

你可能感兴趣的:(android,camera)