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 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> camera 自动对焦&手动对焦 -> 正文阅读

[游戏开发]camera 自动对焦&手动对焦

基础内容可参考

自动对焦系统概述|Camera

https://jingyan.baidu.com/article/22a299b5c882a29e19376aad.html

对焦框坐标基本概念:

Android Camera2之 手动点击区域对焦 - 简书

CSDN

Camera2点击对焦实现 - mydddfly - 博客园

本文主要总结自己开发过程中遇到的各个知识点

需求: Camera2? App开发:取消自动对焦,设置为触摸手动对焦,对焦完成后依然不允许自动对焦

思路:

触摸屏幕OnTouchListener监听--》根据坐标转换,计算对焦区域--》进行对焦--》显示对焦框

1、触摸屏幕,

private View.OnTouchListener mOntouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            try {
                switch(event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        int[] location = new int[2];
                        mTextureView[0].getLocationOnScreen(location);

                        int tmpx = mTextureView[0].getWidth();
                        int tmpy = mTextureView[0].getHeight();
                        Log.d(TAG, "onTouch,TextureView.getWidth:" + tmpx +
                                ",TextureView.getHeight:" + tmpy + ",location[0]:" + location[0] +
                                ",location[1]" + location[1] + ",TextureView.getLeft:" + mTextureView[0].getLeft()
                                + ",TextureView.getTop:" + mTextureView[0].getTop()+", mTextureView[0].getRight():"+ mTextureView[0].getRight()+", mTextureView[0].getBottom():"+ mTextureView[0].getBottom());

                        int focusWidth = mAutoFoucView.getLayoutParams().width;
                        int focusHeight = mAutoFoucView.getLayoutParams().height;

                        float x_raw = event.getRawX();//between screen
                        float y_raw = event.getRawY();//between screen

                        mAutoFoucView.setVisibility(View.VISIBLE);
                        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) mAutoFoucView.getLayoutParams();
//                        int left = (int) (event.getX() - focusWidth / 2 + location[0]);
//                        int top = (int) (event.getY() - focusHeight / 2 + location[1]);
                        int left = (int) (event.getX() - focusWidth / 2);
                        int top = (int) (event.getY() - focusHeight / 2);


                        Log.d(TAG, "onTouch,focusWidth:" + focusWidth +
                                ",focusHeight:" + focusHeight + ",event.getX():" + event.getX() +
                                ",event.getY():" + event.getY() + ",event.getRawX():" + event.getRawX()
                                + ",event.getRawY():" + event.getRawY());

                        layoutParams.leftMargin = left;
                        layoutParams.topMargin = top;
                        mAutoFoucView.setLayoutParams(layoutParams);
                        mAutoFoucView.prepareFocus();

                        int width = mTextureView[0].getWidth();
                        int height = mTextureView[0].getHeight();

                        meteringRectangle = getMeteringRectangles(event.getX(), event.getY(), width, height);

                        Message msg = Message.obtain();
                        msg.what = CameraOpt.MSG_SET_TOUCH_POINT_VALUE;
                        msg.obj= meteringRectangle;
                        msg.arg1 = mAutoFoucView.isFocusing()? 1 : 0;
                        mCameraOpt[0].getHandler().sendMessage(msg);
                        break;
                }
                return true;
            } catch (Exception e) {
                Log.v("onTouch", e.toString());
                return false;
            }

        }


    };
                case MSG_SET_TOUCH_POINT_VALUE:
                    meteringRectangle =(MeteringRectangle[]) msg.obj;
                    isFocusing = msg.arg1 != 0;
                    //Log.i(TAG,"isFocusing="+isFocusing);
                    onTouchFoucs(meteringRectangle);

                    break;
    private void onTouchFoucs(MeteringRectangle[] meteringRectangle) {
        if(isFocusing)
        {
//设置AF/AE对焦区域,开始对焦
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, meteringRectangle);
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_REGIONS, meteringRectangle);
            //        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);

            mPreviewRequest = mPreviewRequestBuilder.build();
            try {
                mCaptureSession.setRepeatingRequest(mPreviewRequest, mPreviewListener, mHandler);
            } catch (CameraAccessException e) {
                Log.e(TAG, "setRepeatingRequest failed, " + e.getMessage());
            }

        }
    }
private final CameraCaptureSession.CaptureCallback mPreviewListener = new CameraCaptureSession.CaptureCallback() {
        @Override
        public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
            Log.d(TAG, "onCaptureCompleted begin isFocusing="+isFocusing);
            if (mPreviewRequestBuilder == null) {
                return;
            }
            if(isFocusing){
                Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);//
                Log.d(TAG, "onCaptureCompleted begin afState="+afState);
                if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState
                        || CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {

                    isFocusSucess = true;
                    mCamLister.onFocusState(isFocusSucess);

                    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
                    //如果对焦完成后不允许自动对焦,注释掉如下几行。
//                    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_IDLE);
//                    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
//                    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);


                    mPreviewRequest = mPreviewRequestBuilder.build();
                    try {
                        mCaptureSession.setRepeatingRequest(mPreviewRequest, null, mHandler);//
                    } catch (CameraAccessException e) {
                        Log.e(TAG, "setRepeatingRequest failed, errMsg: " + e.getMessage());
                    }

                }
                else {
                    isFocusSucess = false;
                    mCamLister.onFocusState(isFocusSucess);
                }
            }

            isFocusing = false;
    super.onCaptureCompleted(session, request, result);
//            Log.d(TAG, mCameraId + " onCaptureCompleted end");
        }
        @Override
        public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request, long timestamp, long frameNumber) {
            super.onCaptureStarted(session, request, timestamp, frameNumber);
        }

        @Override
        public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request, CaptureFailure failure) {
            super.onCaptureFailed(session, request, failure);
        }
    };

mPreviewListener是监听预览界面;

getMeteringRectangles函数后面补充

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-01-16 13:26:03  更:2022-01-16 13:27:58 
 
开发: 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/27 18:44:30-

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