photoOutput = new AVCapturePhotoOutput();
if (CaptureSession.CanAddOutput(photoOutput))
{
CaptureSession.AddOutput(photoOutput);
photoOutput.IsHighResolutionCaptureEnabled = true;
}
拍照按钮:
void TakePhoto()
{
AVCapturePhotoSettings settings = GetCurrentPhotoSettings();
photoOutput.CapturePhoto(settings, this);
}
AVCapturePhotoSettings GetCurrentPhotoSettings()
{
AVCapturePhotoSettings photoSettings = AVCapturePhotoSettings.Create();
var previewPixelType = photoSettings.AvailablePreviewPhotoPixelFormatTypes.First();
var keys = new[]
{
new NSString(CVPixelBuffer.PixelFormatTypeKey),
new NSString(CVPixelBuffer.WidthKey),
new NSString(CVPixelBuffer.HeightKey),
};
var objects = new NSObject[]
{
previewPixelType,
new NSNumber(160),
new NSNumber(160)
};
var dictionary = new NSDictionary<NSString, NSObject>(keys, objects);
photoSettings.PreviewPhotoFormat = dictionary;
photoSettings.IsHighResolutionPhotoEnabled = true;
return photoSettings;
}
[Export("captureOutput:didFinishProcessingPhotoSampleBuffer:previewPhotoSampleBuffer:resolvedSettings:bracketSettings:error:")]
void DidFinishProcessingPhoto(AVCapturePhotoOutput captureOutput,CMSampleBuffer photoSampleBuffer,CMSampleBuffer previewPhotoSampleBuffer,AVCaptureResolvedPhotoSettings resolvedSettings, AVCaptureBracketedStillImageSettings bracketSettings,NSError error)
{
if (photoSampleBuffer == null)
{
Console.WriteLine($"Error occurred while capturing photo: {error}");
return;
}
NSData imageData = AVCapturePhotoOutput.GetJpegPhotoDataRepresentation(photoSampleBuffer, previewPhotoSampleBuffer);
UIImage imageInfo = new UIImage(imageData);
(Element as CameraView).SetPhotoResult(imageInfo.AsJPEG().ToArray());
}
作为执行此代码的结果,我得到了一个使用函数保存的图像字节数组:
void SaveImageTest(string filename, byte[] arr)
{
UIImage img = new UIImage(NSData.FromArray(arr));
var jpeg = img.AsJPEG();
NSError error;
jpeg.Save(filename, false, out error);
}
将图像上传到服务器,我在图像细节中得到 96 DPI。 图像细节
我想创建 72 个 DPI 图像。,
我没有找到一个可理解的解决方案,也许 swift 无法创建这样的图像,你可能知道用于图像转换的 Xamarin 库