Ejemplo n.º 1
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            var sampleURL = NSBundle.MainBundle.GetUrlForResource("sample_iPod", "m4v");

            var movieFile = new GPUImageMovie(sampleURL);

            movieFile.RunBenchmark      = true;
            movieFile.PlayAtActualSpeed = false;

            filter = new GPUImagePixellateFilter();
            movieFile.AddTarget(filter);

            // Only rotate the video for display, leave orientation the same for recording
            filter.AddTarget(imageView);

            // In addition to displaying to the screen, write out a processed version of the movie to disk
            var documents   = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var pathToMovie = Path.Combine(documents, "Movie.m4v");

            // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie
            if (File.Exists(pathToMovie))
            {
                File.Delete(pathToMovie);
            }
            var movieURL = new NSUrl(pathToMovie, false);

            var movieWriter = new GPUImageMovieWriter(movieURL, new CGSize(640.0f, 480.0f));

            filter.AddTarget(movieWriter);

            // Configure this for video from the movie file, where we want to preserve all video frames and audio samples
            movieWriter.ShouldPassthroughAudio = true;
            movieFile.AudioEncodingTarget      = movieWriter;
            movieFile.EnableSynchronizedEncoding(movieWriter);

            var timer = NSTimer.CreateRepeatingScheduledTimer(0.3, _ => {
                progressLabel.Text = movieFile.Progress.ToString("P0");
            });

            movieWriter.CompletionHandler = async() => {
                filter.RemoveTarget(movieWriter);
                await movieWriter.FinishRecordingAsync();

                InvokeOnMainThread(() => {
                    timer.Invalidate();
                    progressLabel.Text = 1.ToString ("P0");
                });
            };

            OnPixelWidthChanged(pixelWidthSlider);

            movieWriter.StartRecording();
            movieFile.StartProcessing();
        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            stillCamera = new GPUImageStillCamera();
            stillCamera.OutputImageOrientation = InterfaceOrientation;

            pixellateFilter = new GPUImagePixellateFilter();
            sepiaFilter     = new GPUImageSepiaFilter();

            stillCamera.AddTarget(pixellateFilter);
            pixellateFilter.AddTarget(sepiaFilter);
            sepiaFilter.AddTarget(imageView);

            stillCamera.StartCameraCapture();
        }
Ejemplo n.º 3
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            videoCamera = new GPUImageVideoCamera(AVCaptureSession.Preset640x480, AVCaptureDevicePosition.Back);
            videoCamera.OutputImageOrientation = InterfaceOrientation;
            videoCamera.HorizontallyMirrorFrontFacingCamera = false;
            videoCamera.HorizontallyMirrorRearFacingCamera  = false;

            pixellateFilter = new GPUImagePixellateFilter();
            sepiaFilter     = new GPUImageSepiaFilter();

            videoCamera.AddTarget(pixellateFilter);
            pixellateFilter.AddTarget(sepiaFilter);
            sepiaFilter.AddTarget(imageView);

            videoCamera.StartCameraCapture();
        }
        private void updateImageTransformation(float elapsedTime)
        {
            if (mQuizz.ImageTransformation == ImageTransformations.NONE)
              {
            return;
              }

              mImageTransformationElapsedTime += elapsedTime;

              // Check countdown, to know if we should update image
              mAnimationIntervalCountdown -= elapsedTime;
              if (mAnimationIntervalCountdown <= 0f)
              {
            mAnimationIntervalCountdown = mAnimationIntervalBase;
              }
              else
              {
            // Do not update
            return;
              }

              // Update the image transformation
              // REMEMBER WE ARE ON THE TIMER THREAD
              // Not the UI one.
              // Don't worry, system will bring it back to you if you forget. So kind.
              GPUImageFilter filter = null;

              // Zoom and unzoom
              if (mQuizz.ImageTransformation == ImageTransformations.UNZOOM)
              {

            float startZoomFactor = Constants.ANIMATION_DEZOOM_FACTOR;
            float duration = Constants.ANIMATION_DEZOOM_DURATION;

            mImageTransformationElapsedTime = Math.Min(mImageTransformationElapsedTime, duration);

            // From elapsed time and animation duration
            // Get the current zoom factor (10x, 8x, etc)
            // We stop at 1x and not 0x that's why we substract 1 here.
            float stepValue = (startZoomFactor - 1) / duration;
            float currentZoomFactor = startZoomFactor - (mImageTransformationElapsedTime * stepValue);

            BeginInvokeOnMainThread(() => {

              // image size
              float imageBaseSizeWidth = ScrollViewImageGame.Frame.Width;
              float imageBaseSizeHeight = ScrollViewImageGame.Frame.Height;

              float width = imageBaseSizeWidth * currentZoomFactor;
              float height = imageBaseSizeHeight * currentZoomFactor;

              width = Math.Max(width, imageBaseSizeWidth);
              height = Math.Max(height, imageBaseSizeHeight);

              // Center largest image in the scroll view
              float x = (imageBaseSizeWidth / 2) - (width / 2);
              float y = (imageBaseSizeHeight / 2) - (height / 2);

              ImageGame.Frame = new RectangleF(x, y, width, height);
            });
              }
              else if (mQuizz.ImageTransformation == ImageTransformations.PROGRESSIVE_DRAWING)
              {

            float duration = Constants.ANIMATION_PROGRESSIVE_DRAWING_DURATION;
            mImageTransformationElapsedTime = Math.Min(mImageTransformationElapsedTime, duration);

            // All in the UI thread so we can acces UI things properties
            BeginInvokeOnMainThread(() => {

              if (mProgressiveDrawView == null)
              {

            // Choose a random corner
            float targetX = 0f;
            float targetY = 0f;

            int randomX = mRandom.Next(3) - 1;
            int randomY = mRandom.Next(3) - 1;

            switch (randomX)
            {

              case -1:
                            // Left
                targetX = - ScrollViewImageGame.Frame.Width;
                break;
              case 0:
                            // No move
                targetX = 0;
                break;
              case 1:
                            // Right
                targetX = ScrollViewImageGame.Frame.Width;
                break;
            }

            // Avoid black square not moving...
            if (randomX == 0 && randomY == 0)
            {
              randomY = 1;
            }
            switch (randomY)
            {

              case -1:
                            // Top
                targetY = - ScrollViewImageGame.Frame.Height;
                break;
              case 0:
                            // No move
                targetY = 0;
                break;
              case 1:
                            // Bottom
                targetY = ScrollViewImageGame.Frame.Height;
                break;
            }

            // Two directions? Speed is slower (= animation last longer)
            if (randomX != 0 && randomY != 0)
            {
              duration = duration + (duration * (1 / 2));
            }

            mProgressiveDrawTargetX = targetX;
            mProgressiveDrawTargetY = targetY;

            // Create black square
            mProgressiveDrawView = new UIView(
              new RectangleF(0, 0,
                       ScrollViewImageGame.Frame.Width,
                       ScrollViewImageGame.Frame.Height
            )
            );

            mProgressiveDrawView.BackgroundColor = UIColor.Black;
            ScrollViewImageGame.AddSubview(mProgressiveDrawView);
              }
              else
              {
            // Move the black square
            // The the movement value for each second
            // Remember that we start from 0,0
            float progressiveDrawingStepValueX = mProgressiveDrawTargetX / duration;
            float progressiveDrawingStepValueY = mProgressiveDrawTargetY / duration;

            // Apply the movement for the elapsed time
            float x = progressiveDrawingStepValueX * mImageTransformationElapsedTime;
            float y = progressiveDrawingStepValueY * mImageTransformationElapsedTime;

            mProgressiveDrawView.Frame = new RectangleF(x, y, ScrollViewImageGame.Frame.Width, ScrollViewImageGame.Frame.Height);
              }

            });
              }
              else if (mQuizz.ImageTransformation == ImageTransformations.PIXELIZATION)
              {

            // Pixelate!
            GPUImagePixellateFilter pixellateFilter = new GPUImagePixellateFilter();

            float duration = Constants.ANIMATION_PIXELISATION_DURATION;
            mImageTransformationElapsedTime = Math.Min(mImageTransformationElapsedTime, duration);

            // Get the pixelate factor
            // From 0 (clear) to 1f (max, do not do that)
            float startPixelateFactor = 0.07f;
            float stepValue = (startPixelateFactor / duration);
            float currentPixelateFactor = startPixelateFactor - (mImageTransformationElapsedTime * stepValue);

            pixellateFilter.FractionalWidthOfAPixel = currentPixelateFactor;

            // Set the filter
            filter = pixellateFilter;
              }
              else if (mQuizz.ImageTransformation == ImageTransformations.TEST)
              {

            GPUImageSwirlFilter testFilter = new GPUImageSwirlFilter();

            float duration = 5f;
            mImageTransformationElapsedTime = Math.Min(mImageTransformationElapsedTime, duration);

            float startValue = 1f;
            float stepValue = (startValue / duration);
            float currentValue = startValue + (mImageTransformationElapsedTime * stepValue);

            testFilter.Radius = currentValue;

            // Set the filter
            filter = testFilter;
              }

              if (filter != null)
              {

            // Generic filter call
            UIImage processedImage = filter.ImageByFilteringImage(mCurrentImage);

            // Set the image
            BeginInvokeOnMainThread(() => {
              ImageGame.Image = processedImage;
            });
              }
        }