You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
2.1 KiB
68 lines
2.1 KiB
using AVFoundation;
|
|
using CoreFoundation;
|
|
using CoreGraphics;
|
|
using CoreVideo;
|
|
using Demo.Controls;
|
|
using Foundation;
|
|
using System;
|
|
using System.Linq;
|
|
using UIKit;
|
|
|
|
namespace Demo.iOS.Controls
|
|
{
|
|
public class UICameraPreview : UIView
|
|
{
|
|
private AVCaptureVideoPreviewLayer previewLayer;
|
|
private CameraOptions cameraOptions;
|
|
|
|
public AVCaptureSession CaptureSession { get; private set; }
|
|
|
|
public bool IsPreviewing { get; set; }
|
|
|
|
public UICameraPreview(CameraOptions options)
|
|
{
|
|
cameraOptions = options;
|
|
IsPreviewing = false;
|
|
Initialize();
|
|
}
|
|
|
|
public override void Draw(CGRect rect)
|
|
{
|
|
base.Draw(rect);
|
|
previewLayer.Frame = rect;
|
|
}
|
|
|
|
private void Initialize()
|
|
{
|
|
CaptureSession = new AVCaptureSession();
|
|
previewLayer = new AVCaptureVideoPreviewLayer(CaptureSession)
|
|
{
|
|
Frame = Bounds,
|
|
VideoGravity = AVLayerVideoGravity.ResizeAspectFill
|
|
};
|
|
|
|
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);
|
|
|
|
//
|
|
var output = new AVCaptureVideoDataOutput();//CVPixelFormatType.CV32BGRA
|
|
output.WeakVideoSettings = new CVPixelBufferAttributes { PixelFormatType = CVPixelFormatType.CV32BGRA }.Dictionary;
|
|
output.SetSampleBufferDelegateQueue(new SampleBufferDelegate(), new DispatchQueue("myQueue"));
|
|
CaptureSession.AddOutput(output);
|
|
//
|
|
|
|
Layer.AddSublayer(previewLayer);
|
|
CaptureSession.StartRunning();
|
|
IsPreviewing = true;
|
|
}
|
|
}
|
|
} |