UnityNativeCamera icon indicating copy to clipboard operation
UnityNativeCamera copied to clipboard

App is zoomed in after zooming in while taking picture (AR Kit) iOS only.

Open marcolbx opened this issue 4 years ago • 8 comments

Description of the bug Only happening on iOS! We are currently using this asset to take pictures and we are also using AR Core/Kit. If we are taking a picture and we zoom in, then after exiting the native camera, the zoom is still there and we cannot reset it because AR Core/Kit takes control of the camera stream.

Reproduction steps Start the plugin to take a picture. Zoom in, Take picture or close the native camera, The camera is zommed in the entire app.

Additional info On Android the camera's zoom is reset automatically, which could probably mean that it could be also done for iOS.

marcolbx avatar Jan 25 '22 11:01 marcolbx

That is indeed a very interesting issue. I don't create the camera user interface manually (i.e. zoom button, switch camera button, capture button), they are all parts of the native camera user interface (a preset). This native preset returns me only a single value: the captured photo's path. It doesn't return the zoom level or AFAIK allow me to change the zoom level programmatically. So, at the moment, this issue will probably remain open.

yasirkula avatar Jan 25 '22 13:01 yasirkula

let me know if you need more details in the future, we will certainly keep trying to find a solution

marcolbx avatar Jan 25 '22 14:01 marcolbx

Can you try adding the following code to here and cross your fingers: [[AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack] videoZoomFactor] = 1.0;

NOTE: I didn't compile this code myself, there might be compiler errors.

PPS. You can add this code in the Xcode project (while testing), no need to rebuild the Unity project.

yasirkula avatar Jan 25 '22 15:01 yasirkula

So I do not have Xcode with me, but I changed the .mm file by adding your line of code on line 276, and tried with Unity Cloud builds and I have this:

Error from Cloud Build: /BUILD_PATH/xxxxxxx/temp20220127-4912-acmz1m/Libraries/Plugins/NativeCamera/iOS/NativeCamera.mm:277:171: assigning to 'readonly' return result of an Objective-C message not allowed

I believe it is on: videoZoomFactor = 1.0, could it be that we cannot set it? I wonder also if there is some other zoomFactor property since this one is for video but the problem is for taking pictures 🤔
PS: I do not know Objective C++ so I do not know if your recommendation works.

marcolbx avatar Jan 27 '22 12:01 marcolbx

How about this:

[AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack].videoZoomFactor = 1.0;

PS. There is no equivalent of AVMediaTypeVideo for photos, so I'm hoping that it points to the camera that takes the photos.

yasirkula avatar Jan 27 '22 18:01 yasirkula

I could create the build and test, however it was not successful. The problem persists

marcolbx avatar Jan 28 '22 06:01 marcolbx

My final attempt:

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack];
if (device != nil)
{
	NSError *error = nil;
	if ([device lockForConfiguration:&error])
	{
		device.videoZoomFactor = 1.0;
		[device unlockForConfiguration];
	}
	else
	{
		NSLog(@"Reset zoom error: %@", error);
	}
}

As an unrelated note, if you can take pictures by capturing a screenshot of the AR camera instead (same solution I've proposed in #84), then you won't have zoom issues, as well.

yasirkula avatar Jan 28 '22 07:01 yasirkula

Same result, it is still a problem. Yes, the screenshot would solve the issue.

marcolbx avatar Jan 28 '22 09:01 marcolbx