【问题标题】:Xamarin.iOS capture photo in 72 dpiXamarin.iOS 以 72 dpi 拍摄照片
【发布时间】:2021-06-23 03:47:31
【问题描述】:
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。 image details

我想创建 72 个 DPI 图像。,

我没有找到一个可理解的解决方案,也许 swift 无法创建这样的图像,你可能知道用于图像转换的 Xamarin 库

【问题讨论】:

标签: swift xamarin.ios dpi avcapturephotooutput


【解决方案1】:

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

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

  } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多