Xamarin.iOS自定义摄像头会话配置不会影响某些iPhone中的已保存图像

时间:2019-08-10 19:08:24

标签: ios xamarin camera

我正在使用Xamarin.iOS在iPhone上开发自定义相机,并在其中操纵RGB增益。这消除了我的应用程序的饱和度。因此,相机Session中的图像没有任何饱和度。然后,我拍摄一张图像并将其保存在图库中。这在iPhone XR上非常有效。但是,不是在iPhone 7上。它会消除相机会话预览中的饱和度,但是在保存图像时会发生某些情况,并且图像会饱和保存。这两款手机都有最新的iOS 12.4。

 partial void captureSpectraTouchUpInside(Foundation.NSObject sender)
    {
        jpegAsByteArray = null;

        //when i used await, it crashed.  
        takeThepictureAsync();
    }


 public async Task takeThepictureAsync()
    {
        var videoConnection =     stillImageOutput.ConnectionFromMediaType(AVMediaType.Video);
        var sampleBuffer = await stillImageOutput.CaptureStillImageTaskAsync(videoConnection);

        var jpegImageAsNsData = AVCaptureStillImageOutput.JpegStillToNSData(sampleBuffer);

        jpegAsByteArray = jpegImageAsNsData;

        var imageMeta = CIImage.FromData(jpegImageAsNsData);

        // creating an image and putting it in ImagePreview frame on top of the screen

        var imagePrev = new UIImage(jpegImageAsNsData); //UIImage.FromImage(imageMeta);
        ImagePreview.Image = imagePrev;

        sampleBuffer.Dispose();


        // Saving the complete image into the phone gallery with addition to its meta data 
        var meta = imageMeta.Properties.Dictionary.MutableCopy() as NSMutableDictionary;
        var library = new ALAssetsLibrary();
        library.WriteImageToSavedPhotosAlbum(jpegImageAsNsData, meta, (assetUrl, error) =>
        {
            if (error == null)
            {
                Console.WriteLine("saved: ");//+jpegImageAsNsData);
            }
            else
            {
                Console.WriteLine(error);
                UIAlertView alert = new UIAlertView()
                {
                    Title = "Alert!",
                    Message = "There was a problem with saving your image, please take a new picture"
                };
                alert.AddButton("OK");
                alert.Show();

            }
        });

    }

 public void SetupLiveCameraStream()
    {
        captureSession = new AVCaptureSession();

        //.PresetPhoto for camera image feed
        captureSession.SessionPreset = AVCaptureSession.PresetPhoto;

        var viewLayer = liveCameraStream.Layer;
        videoPreviewLayer = new AVCaptureVideoPreviewLayer(captureSession)
        {
            Frame = this.View.Frame,
        };

        liveCameraStream.Layer.AddSublayer(videoPreviewLayer);
        var captureDevice = AVCaptureDevice.GetDefaultDevice(AVMediaType.Video);

        ConfigureCameraForDevice(captureDevice);
        captureDeviceInput = AVCaptureDeviceInput.FromDevice(captureDevice);
        captureSession.AddInput(captureDeviceInput);

        var dictionary = new NSMutableDictionary();
        dictionary[AVVideo.CodecKey] = new NSNumber((int)AVVideoCodec.JPEG);
        stillImageOutput = new AVCaptureStillImageOutput()
        {
            OutputSettings = new NSDictionary()
        };

        captureSession.AddOutput(stillImageOutput);
        captureSession.StartRunning();

    }



    void ConfigureCameraForDevice(AVCaptureDevice device)
    {
        var error = new NSError();
        if (device.IsFocusModeSupported(AVCaptureFocusMode.ContinuousAutoFocus))
        {
            device.LockForConfiguration(out error);
            device.FocusMode = AVCaptureFocusMode.Locked;
            device.SetFocusModeLocked((float)0.3, null);

            Console.WriteLine("devidce device.FocusMode: " + device.FocusMode);
            device.UnlockForConfiguration();
        }

        if (device.IsExposureModeSupported(AVCaptureExposureMode.ContinuousAutoExposure))
        {
            device.LockForConfiguration(out error);
            device.ExposureMode = AVCaptureExposureMode.Custom;

            var iso = (device.ActiveFormat.MaxISO + device.ActiveFormat.MinISO) / 2;
            CMTime exposureTime = new CMTime(1, 10);
            device.LockExposure(exposureTime, iso, null);
            Console.WriteLine("deviceexposure: " + device.ExposureDuration + " = " + exposureTime + " iso: " + iso);

            device.UnlockForConfiguration();

            //Setting RGB gains using WhiteBalance
            device.LockForConfiguration(out error);
            AVCaptureWhiteBalanceGains gains = device.DeviceWhiteBalanceGains;
            //normalizign Gains
            gains.RedGain = Math.Max(1, gains.RedGain);
            gains.GreenGain = Math.Max(1, gains.GreenGain);
            gains.BlueGain = Math.Min(1, gains.BlueGain);

            float maxGain = device.MaxWhiteBalanceGain;
            gains.RedGain = Math.Min(maxGain, gains.RedGain);
            gains.GreenGain = Math.Min(maxGain, gains.GreenGain);
            gains.BlueGain = Math.Min(maxGain, gains.BlueGain);
            Console.WriteLine("devidce device.WhiteBalanceMode gains RGB: " + gains.RedGain + " " + gains.GreenGain + " " + gains.BlueGain);

            device.SetWhiteBalanceModeLockedWithDeviceWhiteBalanceGains(gains, null);
            device.UnlockForConfiguration();
        }
    }

我希望保存的图像在RG通道中不会饱和。

0 个答案:

没有答案