我们在应用程序中使用自定义相机。我已经实现了使用以下链接的自定义相机.当点击捕捉按钮,相机预览被停止,它运行良好,但我如何才能获得捕获的图像数据,我想保存在图片库中,并返回路径。我已经通过使用PictureCallback获取了安卓系统中捕获的图像数据。请帮我处理这个。
自定义相机链接: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/view
发布于 2020-10-21 09:02:50
看看这个博客。为了在AVCapturePhotoCaptureDelegate中获取图像数据,我添加了AVCapturePhotoOutput photoOutput。下面是一个示例:
public class UICameraPreview : UIView
{
AVCaptureVideoPreviewLayer previewLayer;
CameraOptions cameraOptions;
AVCapturePhotoOutput photoOutput;
public event EventHandler<EventArgs> Tapped;
public AVCaptureSession CaptureSession { get; private set; }
public bool IsPreviewing { get; set; }
public UICameraPreview (CameraOptions options)
{
cameraOptions = options;
IsPreviewing = false;
Initialize ();
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
if (previewLayer != null)
previewLayer.Frame = Bounds;
}
public override void TouchesBegan (NSSet touches, UIEvent evt)
{
base.TouchesBegan (touches, evt);
OnTapped ();
}
protected virtual void OnTapped ()
{
var eventHandler = Tapped;
if (eventHandler != null) {
eventHandler (this, new EventArgs ());
}
}
void Initialize ()
{
CaptureSession = new AVCaptureSession ();
previewLayer = new AVCaptureVideoPreviewLayer (CaptureSession) {
Frame = Bounds,
VideoGravity = AVLayerVideoGravity.ResizeAspectFill
};
//self.photoOutput.capturePhoto(with: AVCapturePhotoSettings(), delegate: self)
photoOutput = new AVCapturePhotoOutput();
CaptureSession.AddOutput(photoOutput);
photoOutput.CapturePhoto(AVCapturePhotoSettings.Create(),new myDelegate());
var videoDevices = AVCaptureDevice.DevicesWithMediaType (AVMediaType.Video);
var cameraPosition = (cameraOptions == CameraOptions.Front) ? AVCaptureDevicePosition.Front : AVCaptureDevicePosition.Back;
var device = videoDevices.FirstOrDefault (d => d.Position == cameraPosition);
if (device == null) {
return;
}
NSError error;
var input = new AVCaptureDeviceInput (device, out error);
CaptureSession.AddInput (input);
Layer.AddSublayer (previewLayer);
CaptureSession.StartRunning ();
IsPreviewing = true;
}
}
public class myDelegate : AVCapturePhotoCaptureDelegate {
public override void DidFinishCapture(AVCapturePhotoOutput captureOutput, AVCaptureResolvedPhotoSettings resolvedSettings, NSError error)
{
base.DidFinishCapture(captureOutput, resolvedSettings, error);
}
public override void DidFinishProcessingPhoto(AVCapturePhotoOutput output, AVCapturePhoto photo, NSError error)
{
base.DidFinishProcessingPhoto(output, photo, error);
if (error == null)
{
var photodata = photo.FileDataRepresentation;
}
}
}我还没有将所有的快速代码翻译到c#,如果您有任何问题,请随时问我。
https://stackoverflow.com/questions/64457742
复制相似问题