photoOutput = new AVCapturePhotoOutput();
if (CaptureSession.CanAddOutput(photoOutput))
{
CaptureSession.AddOutput(photoOutput);
photoOutput.IsHighResolutionCaptureEnabled = true;
}TakePhoto按钮:
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);
}将图像上传到服务器,我在图像详细信息中获得96DPI。image details
我想创建72个DPI图像。,
我没有找到一个容易理解的解决方案,也许swift不能创建这样的图像,而您可能知道用于图像转换的Xamarin库
发布于 2021-04-01 14:09:50
我们可以使用以下代码调整图像的大小
/*
sourceImage:the iamge that you download
newSize the size you want ( for example 72*72)
*/
public UIImage ScalingImageToSize(UIImage sourceImage,CGSize newSize)
{
UIGraphics.BeginImageContextWithOptions(newSize, false, UIScreen.MainScreen.Scale);
sourceImage.Draw(new CGRect(0, 0, newSize.Width, newSize.Height));
UIImage newImage = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return newImage;
} https://stackoverflow.com/questions/66814832
复制相似问题