question

hosseintavakoli-7723 avatar image
0 Votes"
hosseintavakoli-7723 asked RobCaplan edited

zoom in Camera2Basic xamarin


I useing Camera2basic.

I have a Button ("Zoom") in MyApp.

I want to when I click on Zoom Button or when scale finger on screen set camera zoom value, but I dont have any idea how to do it.

here is my app screenshot

sorry I cant speak English very well.


dotnet-xamarin
screenshot.png (2.3 MiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I want to when I click on Zoom Button or when scale finger on screen set camera zoom value, but I dont have any idea how to do it

Hi, hosseintavakoli. Try to get the maxZoom and setZoom with the target CaptureRequest.Builder. Then send the new zoom to the current CameraCaptureSession with a Repeating Request to make it active.

Similar issue cases you could refer to:
https://stackoverflow.com/questions/52568987/camera-zoom-setting-using-camera2-api
https://stackoverflow.com/questions/35968315/android-camera2-handle-zoom

0 Votes 0 ·

I checked above link but I don't understand how can I do it?
could you give me some code please?

0 Votes 0 ·

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT edited

Here is related code about the function, please check it.

var manager = Android.App.Application.Context.GetSystemService(Context.CameraService) as CameraManager;
var id_list = manager.GetCameraIdList();
var characteristics = manager.GetCameraCharacteristics(id_list[0]);
float maxZoom;
Rect mSensorSize = (Rect)characteristics.Get(CameraCharacteristics.SensorInfoActiveArraySize);
if (mSensorSize == null)
{
    maxZoom = 1.0f;
    return;
}

Rect mCropRegion = new Rect();
float zoom = 1.5f;

var newZoom = MathUtils.Clamp(zoom, 1.0f, 6.0f);

int centerX = mSensorSize.Width() / 2;
int centerY = mSensorSize.Height() / 2;
int deltaX = (int)((0.5f * mSensorSize.Width()) / newZoom);
int deltaY = (int)((0.5f * mSensorSize.Height()) / newZoom);

mCropRegion.Set(centerX - deltaX,
        centerY - deltaY,
        centerX + deltaX,
        centerY + deltaY);

mPreviewRequestBuilder.Set(CaptureRequest.ScalerCropRegion, mCropRegion);
mCaptureSession.SetRepeatingRequest(mPreviewRequestBuilder.Build(), null, null);//you could define the callback and handler
· 10
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Tnx a lot zoom is working, but when I click on TakePicture Button can't take picture.

0 Votes 0 ·

Hi, did you change some other code? The above code shouldn't affect the 'TakePicture' function and it works fine on my side.

0 Votes 0 ·

No I didn't change code just Copy & Paste. when I click on take_picture button it will try to focus but doesn't take picture.

0 Votes 0 ·
Show more comments

I didn't change code just Copy & Paste. when I click on take_picture button it will try to focus but doesn't take picture.

0 Votes 0 ·

Sorry, I cannot reproduce the issue on my side. Would you mind sharing a basic on github or onedrive and then post the link here? It'll help to get the cause to the issue.

0 Votes 0 ·

When I click on Zoom button zoom work correctly , but after zoom when I click on TakePicture Button It cant take photo with zoom.

0 Votes 0 ·

Please add a breakpoint to debug the 'TakePicture' method to check which line code caused the problem.

0 Votes 0 ·

Zoom and Taking photo worked correctly now. but there is a problem when I taking photo with zoom after click on TakePhoto button picture saved without zoom and camera view going out of zoom.

I checked on some devices and problem still yet.

screen



0 Votes 0 ·
animation.gif (2.7 MiB)

Please change the parameters of the captureBuilder.Set command in CaptureStillPicture method.

Rect mCropRegion = null;
private void ZoomButton_Click(object sender, EventArgs e)
{
    ...
    mCropRegion = new Rect();
    ...
}

public void CaptureStillPicture()
{
    try
    {
        ...
        if (mCropRegion != null)
        {
            stillCaptureBuilder.Set(CaptureRequest.ScalerCropRegion, mCropRegion);
            mCropRegion = null;
        }
        else
        {
            stillCaptureBuilder.Set(CaptureRequest.ControlAfMode, (int)ControlAFMode.ContinuousPicture);
        }
        ...
    }
    catch (CameraAccessException e)
    {
        e.PrintStackTrace();
    }
}

Check the link: https://stackoverflow.com/questions/35968315/android-camera2-handle-zoom

0 Votes 0 ·