Android Camera2对焦框和对焦

预览界面顶部布局,添加点击对焦功能以及相应UI

case R.id.flashOffButton:
    mCameraHelper.openFlash();
    //mCameraHelper.closeFlash();
    break;


public void openFlash() {
    //        try {
    //            mCameraManager.setTorchMode(mCameraId,true);
    //        } catch (CameraAccessException e) {
    //            e.printStackTrace();
    //        }
    if(!isStartFlash){
        captureRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
        Log.d(TAG, "openFlash: 闪光灯打开");
    }else {
        captureRequestBuilder.set(CaptureRequest.FLASH_MODE,CaptureRequest.FLASH_MODE_OFF);
        Log.d(TAG, "openFlash: 闪光灯关闭");
    }
    isStartFlash = !isStartFlash;
}

获取captureRequestBuilder

private void createCaptureSession(CameraDevice cameraDevice){
        try {
            Log.d(TAG, "createCaptureSession的CameraDevice :" + cameraDevice);
            CaptureRequest.Builder builder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            captureRequestBuilder = builder;
            @SuppressLint("Recycle")
            Surface surface = new Surface(mTextureView.getSurfaceTexture());
            captureRequestBuilder.addTarget(surface);
            captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
            captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
            captureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,CaptureRequest.CONTROL_AF_TRIGGER_CANCEL);

            List<Surface> list = new ArrayList<>();
            list.add(surface);
            list.add(mImageReader.getSurface());
            cameraDevice.createCaptureSession(list, new CameraCaptureSession.StateCallback() {
                @Override
                public void onConfigured(@NonNull CameraCaptureSession session) {
                    Log.d(TAG, "onConfigured: 1111");
                    mCameraCaptureSession = session;
                    try {
                        session.setRepeatingRequest(captureRequestBuilder.build(), mCaptureCallback, mCameraHandler );
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                    Log.d(TAG, "onConfigureFailed: 开启预览会话失败");
                }
            }, mCameraHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

调整相机预览界面整体布局为frameLayout,方便点击屏幕出现对焦框:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relativeLayout"
    android:background="#000">

    <LinearLayout
        android:id="@+id/topLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="102dp"
        android:background="@color/black"
        android:layout_gravity="top"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/effectButton"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginStart="18dp"
            android:layout_marginTop="37dp"
            android:background="@drawable/camera_quick_ic_effect"
            android:contentDescription="setting button" />

        <ImageButton
            android:id="@+id/flashOffButton"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginStart="39dp"
            android:layout_marginTop="37dp"
            android:background="@drawable/camera_quick_ic_flash_off"
            android:contentDescription="flash button" />

        <ImageButton
            android:id="@+id/timeButton"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginStart="39dp"
            android:layout_marginTop="37dp"
            android:background="@drawable/camera_quick_ic_timer_off"
            android:contentDescription="timer button" />

        <ImageButton
            android:id="@+id/ratioButton"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginStart="39dp"
            android:layout_marginTop="37dp"
            android:background="@drawable/camera_quick_ic_ratio_3v4"
            android:contentDescription="ratio 3v4 , 9v16 or full screen" />

        <ImageButton
            android:id="@+id/settingButton"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginStart="39dp"
            android:layout_marginTop="37dp"
            android:background="@drawable/camera_quick_ic_setting"
            android:contentDescription="beauty and filter" />

    </LinearLayout>

    <com.bilibili.camera2.view.TextureViewController
        android:id="@+id/textureView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <com.bilibili.camera2.view.FocusView
        android:id="@+id/focusView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible" />

    <TextView
        android:id="@+id/timeTextView"
        android:layout_marginTop="48.8dp"
        android:layout_marginStart="165dp"
        android:layout_width="75dp"
        android:layout_height="18dp"
        android:text="00:00:00"
        android:visibility="gone"
        android:textColor="#FFFFFFFF"
        android:textSize="17dp"/>

    <View
        android:id="@+id/redPoint"
        android:layout_marginTop="55dp"
        android:layout_marginStart="146dp"
        android:layout_width="12dp"
        android:layout_height="12dp"
        android:visibility="gone"
        android:background="#FFD71E23" />


    
    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="208dp"
        android:layout_gravity="bottom"
        android:orientation="vertical">
        
        <com.bilibili.camera2.view.SideBar
            android:id="@+id/sideBar"
            android:layout_width="300dp"
            android:layout_height="60dp"
            android:visibility="gone"
            android:layout_marginTop="10dp"
            android:layout_gravity="center_horizontal"/>

        <com.bilibili.camera2.view.HorizontalSelectedView
            android:id="@+id/selectedView"
            android:layout_width="match_parent"
            android:layout_marginTop="10dp"
            android:layout_height="60dp"
            android:background="@color/black"
            android:layout_gravity="center_horizontal"
            app:HorizontalSelectedViewSeeSize="5"
            android:visibility="visible"
            app:HorizontalSelectedViewSelectedTextColor="@color/white"
            app:HorizontalSelectedViewSelectedTextSize="40"
            app:HorizontalSelectedViewTextColor="@color/gray"
            app:HorizontalSelectedViewTextSize="30" />

        <RelativeLayout
            android:background="@color/black"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <!-- 拍照按钮 -->
            <ImageButton
                android:id="@+id/btnTakePicture"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_centerHorizontal="true"
                android:background="@drawable/selector"
                android:layout_gravity="center"
                tools:ignore="ContentDescription" />
            <!-- 录像开始按钮 -->
            <ImageButton
                android:id="@+id/btnVideoStart"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center"
                android:background="@drawable/ic_camera_main_btn_01_auto"
                android:src="@drawable/camera_main_btn_01_rec"
                android:visibility="gone"
                tools:ignore="ContentDescription" />
            <!-- 录像结束按钮 -->
            <ImageButton
                android:id="@+id/btnVideoStop"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center"
                android:background="@drawable/ic_camera_main_btn_01_auto"
                android:src="@drawable/camera_main_btn_01_stop"
                android:visibility="gone"
                tools:ignore="ContentDescription" />

            <com.bilibili.camera2.view.CircleImageView
                android:id="@+id/btnImagePreview"
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:layout_centerVertical="true"
                android:padding="5dp"
                android:layout_marginStart="50dp" />
            <ImageButton
                android:id="@+id/videoPause"
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:layout_centerVertical="true"
                android:layout_marginStart="50dp"
                android:background="@drawable/camera_main_btn_02_pause"
                android:visibility="gone"/>

            <ImageView
                android:id="@+id/faceBackCameraChange"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_marginStart="280dp"
                android:layout_centerVertical="true"
                android:background="@drawable/ic_camera_main_btn_02_switch"

                android:clickable="true"
                tools:ignore="ContentDescription" />
        </RelativeLayout>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearLayoutBottom"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="bottom"
        android:background="@color/black"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" />

</FrameLayout>

在textureview中添加OnTouchListener监听事件监听屏幕的点击实现对焦:

mTextureView.setOnTouchListener((v, event) -> {
    DisplayMetrics metrics = new DisplayMetrics();
    mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    Log.d(TAG, "init: 屏幕分辨率 metrics : " + metrics.widthPixels + " " + metrics.heightPixels);

    int screenW = metrics.widthPixels;//屏幕宽度
    int screenH = metrics.widthPixels;//屏幕宽度

    int realPreviewWidth = 4000;
    int realPreviewHeight = 3000;

    float focusX = (float) realPreviewWidth / screenW * event.getX();
    float focusY = (float) realPreviewHeight / screenH * (event.getX() + 112 * 2.54f);

    Rect cropRegion = captureRequestBuilder.get(CaptureRequest.SCALER_CROP_REGION);
    Log.d(TAG, "init: cropRegion.height() " + cropRegion.height() );
    float cutDx = (cropRegion.height() - 1440) / 2.0f;

    Log.d(TAG, "init: realPreviewWidth" + realPreviewWidth + " " + realPreviewHeight);

    float x1 = event.getX();
    float y1 = event.getY() + 112*2.54f;
    Rect rect1 = new Rect();
    rect1.left = (int) (focusX);
    rect1.top = (int) (focusY + cutDx);
    rect1.right = (int) (focusX + 50);
    rect1.bottom = (int) (focusY + cutDx + 50);
    Log.d(TAG, "init: rect1 : " + rect1.left + " " + rect1.right + " " + rect1.top + " " + rect1.bottom);

    try {
        CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        builder.addTarget(new Surface(mTextureView.getSurfaceTexture()));
        builder.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[] {new MeteringRectangle(rect1, 1000)});
        builder.set(CaptureRequest.CONTROL_AE_REGIONS, new MeteringRectangle[] {new MeteringRectangle(rect1, 1000)});
        builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
        builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
        builder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_START);
        mCameraCaptureSession.setRepeatingRequest(builder.build(), null, mCameraHandler);

        builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
        mCameraCaptureSession.capture(builder.build(),mCaptureCallback,mCameraHandler);

        if (!rect1.isEmpty()){
            //focusView.setVisibility(View.VISIBLE);

            Log.d(TAG, "init: x1--y1 " + x1 + " " + y1);
            focusView.setTouchFocusRect(rect1,x1,y1);
        }

        //                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
        //                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        //                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
        //                mCameraCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), null, mCameraHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
    return false;
});
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值