Set the camera focus area in Android

后端 未结 3 1189
心在旅途
心在旅途 2021-02-02 12:14

following several tutorials and examples I came up with the next algorithm to set the camera focus on a specific spot, the problem is that the camera completely ignores the spot

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-02 13:01

    this work for me focus on camera 1 Api

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    
            //variable for storing the time of first click
            //constant for defining the time duration between the click that can be considered as double-tap
            final int MAX_DURATION = 200;
            // handle single touch events
            if (action == MotionEvent.ACTION_UP) {
                handleFocus(event, params);
                startTime = System.currentTimeMillis();
            }
    
            else if (event.getAction() == MotionEvent.ACTION_DOWN) {
    
                if(System.currentTimeMillis() - startTime <= MAX_DURATION)
                {
    
                    //capture image on Double Tap
                    mCamera.autoFocus(ShotActivity_CameraActivity.this);
    
    
                }
            }
        }
        return true;
    
    }
    

    then this method to handle touch focus area

    public void handleFocus(MotionEvent event, Camera.Parameters params) {
        int pointerId = event.getPointerId(0);
        int pointerIndex = event.findPointerIndex(pointerId);
        // Get the pointer's current position
        float x = event.getX(pointerIndex);
        float y = event.getY(pointerIndex);
    
        Rect touchRect = new Rect(
                (int) (x - 100),
                (int) (y - 100),
                (int) (x + 100),
                (int) (y + 100) );
        final Rect targetFocusRect = new Rect(
                touchRect.left * 2000/mTextureView.getWidth() - 1000,
                touchRect.top * 2000/mTextureView.getHeight() - 1000,
                touchRect.right * 2000/mTextureView.getWidth() - 1000,
                touchRect.bottom * 2000/mTextureView.getHeight() - 1000);
    
        List supportedFocusModes = params.getSupportedFocusModes();
        if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
            try {
                List focusList = new ArrayList();
                Camera.Area focusArea = new Camera.Area(targetFocusRect, 1000);
                focusList.add(focusArea);
    
    
                params.setFocusAreas(focusList);
                params.setMeteringAreas(focusList);
                mCamera.setParameters(params);
    
    
    
               /* mCamera.autoFocus(this);*/
            } catch (Exception e) {
                e.printStackTrace();
                Log.i(TAG, "Unable to autofocus");
            }
    
        }
    }
    

    i hope this help

提交回复
热议问题