public async Task <ICameraPreview> OpenWithPreviewAsync(Xamarin.Forms.Size previewRequestSize) { await OpenAsync(true); var stateCallback = new StateCallback(); stateCallback.Configured += SessionConfigured; stateCallback.ConfigureFailed += SessionConfigureFailed; var previewSurface = _cameraPreview.CreateSurface(previewRequestSize); _previewRequestBuilder.AddTarget(previewSurface); _previewRequestBuilder.Set(CaptureRequest.ControlAfMode, (int)ControlAFMode.ContinuousPicture); var characteristics = _manager.GetCameraCharacteristics(_cameraDevice.Id); var map = (StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap); // ReSharper disable once CoVariantArrayConversion _imageSize = (Size)Collections.Max( Arrays.AsList(map.GetOutputSizes((int)ImageFormatType.Jpeg)), new CompareSizesByArea()); _imageReader = ImageReader.NewInstance(_imageSize.Width, _imageSize.Height, ImageFormatType.Jpeg, /* maxImages */ 2); _imageAvailableListener = new ImageAvailableListener(); _imageAvailableListener.ImageAvailable += CaptureAvailable; _imageReader.SetOnImageAvailableListener(_imageAvailableListener, _backgroundThread.Handler); var surfaces = new List <Surface> { previewSurface, _imageReader.Surface }; _cameraDevice.CreateCaptureSession(surfaces, stateCallback, null); return(_cameraPreview); }
public Surface CreateSurface(Size requestSize) { var requestPixelSize = ToPixels(requestSize); var bufferSize = GetBufferSize(requestPixelSize); Size = DimensionUtils.ToXamarinFormsSize(bufferSize); var pixelSize = new System.Drawing.Size { Width = bufferSize.Width, Height = bufferSize.Height }; PixelSize = pixelSize; ConfigureTransform(requestPixelSize.Width, requestPixelSize.Height); _imageReader = ImageReader.NewInstance(bufferSize.Width, bufferSize.Height, ImageFormatType.Yuv420888, 4); _imageAvailableListener = new ImageAvailableListener(); _imageAvailableListener.ImageAvailable += PreviewImageAvailable; _imageReader.SetOnImageAvailableListener(_imageAvailableListener, _backgroundHandler); return(_imageReader.Surface); }