1
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 库

4

1 回答 1

0

我们可以使用以下代码调整图像大小

/*
   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;

  } 
于 2021-04-01T06:09:50.083 回答