IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android Camera2对焦框和对焦 -> 正文阅读

[移动开发]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;
});
  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-11-24 08:03:55  更:2021-11-24 08:06:08 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 5:05:47-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码