android-Camera2Basic icon indicating copy to clipboard operation
android-Camera2Basic copied to clipboard

Focus Areas

Open gilles-yvetot opened this issue 11 years ago • 8 comments

I am trying to implement a camera with a focus on a special area. So far I Have implemented a method from what I have seen in the tutorial

   private void setFocusArea()
         {

        MeteringRectangle[] focusAreas = new MeteringRectangle[1];

        Rect sensor_rect = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
        Rect focusArea = new Rect(
                0,
                0,
                sensor_rect.width()-1,
                (int)(sensor_rect.height()*0.5));
        focusAreas[0] = new MeteringRectangle(focusArea,MeteringRectangle.METERING_WEIGHT_MAX);

        if(mCameraCharacteristics!= null
                && mCameraCharacteristics.get(CameraCharacteristics.CONTROL_MAX_REGIONS_AF)>0)
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_REGIONS,focusAreas);

//        if(mCameraCharacteristics!= null
//                && mCameraCharacteristics.get(CameraCharacteristics.CONTROL_MAX_REGIONS_AE)>0)
//            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_REGIONS,focusAreas);



    }

And I am calling this function right before every mPreviewRequestBuilder.build()

So it is supposed to focus on the top 50% of the image, but it just doesn't.

Any ideas, feedback, or where should I look ?

gilles-yvetot avatar Oct 09 '15 13:10 gilles-yvetot

Did you trigger af manually or set focus mode as CONTINUOUS_PICTURE?

xiaozhou-eric avatar Oct 10 '15 08:10 xiaozhou-eric

I set focus mode to CONTINUOUS_PICTURE

gilles-yvetot avatar Oct 15 '15 20:10 gilles-yvetot

Then maybe your focus area is too big, you can try another reasonable setting.

xiaozhou-eric avatar Oct 16 '15 07:10 xiaozhou-eric

I have tried without setting any focus area and it does not work. It looks like it depends on the device, I have tested on a Motoroall Moto E, it is working, but it does NOT work on a Samsung S5.

gilles-yvetot avatar Oct 16 '15 19:10 gilles-yvetot

Actually even the basic projet does not work on Samsung S5, I meant the continuous focus mode...

gilles-yvetot avatar Oct 21 '15 14:10 gilles-yvetot

Hello @yaraki, did you have time to check this out? That's my priority now so we can even work together on that?

gilles-yvetot avatar Dec 21 '15 20:12 gilles-yvetot

this working to me

`public void handleFocus(MotionEvent event) { 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) );


    if (mCameraId == null) return;
    CameraManager cm = (CameraManager)this.getSystemService(Context.CAMERA_SERVICE);
    CameraCharacteristics cc = null;
    try {
        cc = cm.getCameraCharacteristics(mCameraId);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }


    MeteringRectangle focusArea = new MeteringRectangle(touchRect,MeteringRectangle.METERING_WEIGHT_DONT_CARE);
    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
    try {
        mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
                mBackgroundHandler);
        // After this, the camera will go back to the normal state of preview.
        mState = STATE_PREVIEW;
    } catch (CameraAccessException e){
        // log
    }

   /* if (isMeteringAreaAESupported(cc)) {
        *//*mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_REGIONS,
                new MeteringRectangle[]{focusArea});*//*
    }
    if (isMeteringAreaAFSupported(cc)) {
        *//*mPreviewRequestBuilder
                .set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{focusArea});
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                CaptureRequest.CONTROL_AF_MODE_AUTO);*//*
    }*/
    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_REGIONS,
            new MeteringRectangle[]{focusArea});
    mPreviewRequestBuilder
            .set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{focusArea});
    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
            CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
            CameraMetadata.CONTROL_AF_TRIGGER_START);
    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
            CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_START);
    try {
        mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback,
                mBackgroundHandler);
       /* mManualFocusEngaged = true;*/
    } catch (CameraAccessException e) {
        // error handling
    }
}`

yudikarma avatar Dec 20 '18 11:12 yudikarma

@yudikarma, it is a bit late for me, I am not working for the same company anymore but I appreciate you took the time to reply, I am sure it will help other people.

gilles-yvetot avatar Dec 20 '18 12:12 gilles-yvetot