如何在android中显示Camera自动对焦指示(Frame)

时间:2016-07-27 05:38:16

标签: android camera android-camera autofocus

我正在使用camera-api制作自定义相机2.一切正常工作意味着我能够捕捉图片,设置预览等。但我的问题是它不会推迟原生相机具有的自动对焦矩形指示器。我想在图像中显示矩形框(默认的android自动对焦框)。

enter image description here 那么如何在我的客户相机中显示它。我使用下面的代码打开相机

 private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener()
{

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
    {
        Log.e(TAG, "onSurfaceTextureAvailable, width=" + width + ",height=" + height);

        openCamera(width, height);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height)
    {
        Log.e(TAG, "onSurfaceTextureSizeChanged");
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface)
    {
        surface.release();
        if (mCameraDevice != null) {
            mCameraDevice.close();
        }

        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface)
    {
        // Log.e(TAG, "onSurfaceTextureUpdated");
    }

};

以下是openCamera()

  private void openCamera(int width, int height)
{

    CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    Log.e(TAG, "openCamera E");
    try {
        String cameraId = "";
        if (cameraFront) {
            cameraId = "" + findFrontFacingCamera();
        } else {
            cameraId = "" + findBackFacingCamera();
        }
        CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

        StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
        // mPreviewSize = map.getOutputSizes(SurfaceTexture.class)[Integer.parseInt(cameraId)];

        Size largest =
                Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)), new CompareSizesByArea());

        // Danger, W.R.! Attempting to use too large a preview size could exceed the camera
        // bus' bandwidth limitation, resulting in gorgeous previews but the storage of
        // garbage capture data.
        mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), width, height, largest);

        // We fit the aspect ratio of TextureView to the size of preview we picked.
        manager.openCamera(cameraId, mStateCallback, null);

    } catch (CameraAccessException e) {
        AppLogger.exception(myContext, getClass().getSimpleName(), e);
        // e.printStackTrace();
    }
    Log.e(TAG, "openCamera X");
}

并在我正在使用的代码下面预览

protected void startPreview()
{
    try {

        if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) {
            Log.e(TAG, "startPreview fail, return");
            return;
        }

        SurfaceTexture texture = mTextureView.getSurfaceTexture();
        if (null == texture) {
            Log.e(TAG, "texture is null, return");
            return;
        }
        Log.e(TAG, "Width: " + mPreviewSize.getWidth() + "  Hieght : " + mPreviewSize.getHeight());
        texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        Surface surface = new Surface(texture);

        try {
            mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            mPreviewBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);

        } catch (CameraAccessException e) {
            AppLogger.exception(myContext, getClass().getSimpleName(), e);
            // e.printStackTrace();
        }
        mPreviewBuilder.addTarget(surface);

        mCameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback()
        {

            @Override
            public void onConfigured(CameraCaptureSession session)
            {

                mPreviewSession = session;
                updatePreview();
            }

            @Override
            public void onConfigureFailed(CameraCaptureSession session)
            {

            }
        }, null);
    } catch (CameraAccessException e) {
        AppLogger.exception(myContext, getClass().getSimpleName(), e);
        // e.printStackTrace();
    }
}

由于

0 个答案:

没有答案