[英]How to get captured image of custom renderer camera in iOS xamarin forms
我們在我們的應用程序中使用自定義相機。 我已經使用下面提到的鏈接實現了自定義相機。 單擊捕獲按鈕時,相機預覽停止並且工作正常,但是如何獲取捕獲的圖像數據,並且我想將圖像保存在圖庫中並返回路徑。 我已經通過使用 PictureCallback 在 android 中獲取了捕獲的圖像數據。 請幫我解決一下這個。
自定義相機鏈接: https : //docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/view
看看這個博客。 為了在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;
}
}
}
我還沒有將所有的 swift 代碼翻譯成 c#,如果您有任何問題,請隨時問我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.