如何将android触摸屏坐标转换为传感器坐标

时间:2019-05-24 09:42:37

标签: android-camera2

要实现点击以专注于android-camera 2 API,必须将屏幕上的触摸坐标转换为相机传感器的坐标,根据this答案,该坐标是不相同的。 我想为相机2 api实现触摸对焦,为此,需要根据相机传感器的active array size

适当缩放触摸坐标

我已经尝试过this gist,但是对我来说,测光矩形计算没有意义,而且焦点也无法使用。它会导致矩形的左,上,右,下为负值,具体取决于有人触摸屏幕的位置。 我也尝试过this并得到与上述相同的问题。

我正在遵循camera2basic android代码。

这是我认为可以使用的代码,但它并不专注于触摸屏幕的位置:


    mTextureView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_DOWN){
                        Rect sensor_array_size =characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
                        focusCameraOnArea(
                           (int)(sensor_array_size.width()*(event.getX()-event.getTouchMajor())/mTexturePreview.getWidth()),
                           (int)(sensor_array_size.height()*(event.getY()-event.getTouchMinor())/mTexturePreview.getHeight()),
                           (int)(event.getX()+event.getTouchMajor()),
                           (int)(event.getY()+event.getTouchMinor());
                    }
                    return true;

                }
            });
    private void focusCameraOnArea(int x, int y, int width, int height){
        MeteringRectangle focusArea = new MeteringRectangle(x, y, width, height);
        CameraCaptureSession.CaptureCallback focusCaptureCallback = new CameraCaptureSession.CaptureCallback() {
                    @Override
                    public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                        super.onCaptureCompleted(session, request, result);
                        if(request.getTag()=="FOCUS_TAG"){
                            try{ 
                              mPreviewRequestBuilder.set(
                                    CaptureRequest.CONTROL_AF_TRIGGER,null);
                              mCaptureSession.setRepeatingRequest(
                                 mPreviewRequestBuilder.build(),null,null);}
                            catch(CameraAccessException e){
                                 e.printStackTrace();}
                                               }
                        }

                    @Override
                    public void onCaptureFailed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureFailure failure) {
                        super.onCaptureFailed(session, request, failure);
                        Log.wtf(TAG, "Manual AF failure: " + failure);

                    }
                };
                mCaptureSession.stopRepeating();

                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF);
                mCaptureSession.capture(mPreviewRequestBuilder.build(),focusCaptureCallback,mBackgroundHandler);

                if(characteristics.get(CameraCharacteristics.CONTROL_MAX_REGIONS_AF) >= 1){
                    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{focusArea});
                    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_REGIONS, new MeteringRectangle[]{focusArea});
                }
                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_MODE,CaptureRequest.CONTROL_MODE_AUTO);
                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,CaptureRequest.CONTROL_AF_MODE_AUTO);
                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,CaptureRequest.CONTROL_AF_TRIGGER_START);
                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,CaptureRequest.CONTROL_AE_MODE_ON);
                mPreviewRequestBuilder.setTag("FOCUS_TAG");

                mCaptureSession.capture(mPreviewRequestBuilder.build(),focusCaptureCallback,mBackgroundHandler);
            }catch(CameraAccessException e){e.printStackTrace();}
            catch (Exception e){e.printStackTrace();}            
    }

0 个答案:

没有答案