Reputation: 11
photoOutput = new AVCapturePhotoOutput();
if (CaptureSession.CanAddOutput(photoOutput))
{
CaptureSession.AddOutput(photoOutput);
photoOutput.IsHighResolutionCaptureEnabled = true;
}
TakePhoto button:
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());
}
As a result of executing this code, I get an array of bytes of the image that I save using the function:
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);
}
Uploading an image to the server, I get 96 DPI in image details. image details
I would like to create 72 DPI images.,
I did not find an intelligible solution, maybe swift is not able to create such images and you probably know Xamarin libraries for image transformation
Upvotes: 1
Views: 220
Reputation: 18861
We could resize the image with the following code
/*
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;
}
Upvotes: 0