Ejemplo n.º 1
0
    void OnMediaPicked(object sender, MediaPickedEventArgs e)
    {
        Log("Image picked: " + e.image);

        // expensive process to convert to texture 2d and upload to GPU
        //Texture2D texture = e.image.ToTexture2D();
    }
Ejemplo n.º 2
0
            public void Run()
            {
                // Gets MediaPickerActivity's private members
                _imageAvailableHandler._mediaPickerActivity.Accept(_imageAvailableHandler);
                _imageAvailableHandler._createMediaFile();

                StoreImage();

                Task.Run(NotifyImageStored);

                #region Local functions

                void StoreImage()                 // Stores captured image to specified location
                {
                    ByteBuffer buffer = _image.GetPlanes()[0].Buffer;

                    byte[] bytes = new byte[buffer.Remaining()];
                    buffer.Get(bytes);
                    using (var fileOutputStream =
                               new FileOutputStream(_imageAvailableHandler._convertUriToPath(_imageAvailableHandler._newMediaFileUri), true))
                    {
                        try
                        {
                            fileOutputStream.Write(bytes);
                        }
                        catch (IOException e)
                        {
                            e.PrintStackTrace();
                        }
                        finally
                        {
                            _image.Close();
                        }
                    }
                }

                async Task NotifyImageStored()
                {
                    MediaPickedEventArgs args = GetMediaFile();

                    await Task.Delay(1).ConfigureAwait(false);

                    _imageAvailableHandler._onMediaPicked(args);
                }

                MediaPickedEventArgs GetMediaFile()
                {
                    string albumPath  = _imageAvailableHandler._newMediaFileUri.ToString(),
                           resultPath = _imageAvailableHandler._convertUriToPath(_imageAvailableHandler._newMediaFileUri);

                    if (resultPath != null && File.Exists(resultPath))
                    {
                        var mediaFile = new MediaFile(resultPath, () => File.OpenRead(resultPath), albumPath);
                        return(new MediaPickedEventArgs(default, false, mediaFile));
Ejemplo n.º 3
0
        private void OnMediaPicked(MediaPickedEventArgs e)
        {
            this.tracer.Debug("OnMediaPicked");

            var picked = MediaPicked;

            if (picked != null)
            {
                picked(null, e);
            }
        }
Ejemplo n.º 4
0
    void OnMediaPicked(object sender, MediaPickedEventArgs e)
    {
        if (e.url != null)
        {
            Log("Image picked with url at: " + e.url.AbsoluteString());
        }
        else
        {
            Log("Image picked: " + e.image);
        }

        // clean up previous texture2d first
        if (_image != null)
        {
            Texture2D.Destroy(_image);
        }

        // scaling to 25% because it's an expensive process to convert to texture 2d and upload to GPU
        _image = e.image.ToTexture2D(true, 0.25f);
    }
Ejemplo n.º 5
0
    void OnMediaPicked(object sender, MediaPickedEventArgs e)
    {
        // clean up old stuff
        if (_photo != null)
        {
            Texture2D.Destroy(_photo);
        }

        if (_scrambledFaces != null)
        {
            foreach (var face in _scrambledFaces)
            {
                Texture2D.Destroy(face);
            }
            _scrambledFaces = null;
        }

//		_photo = e.image.ToTexture2D(true, 0.25f);

        System.Random random = new System.Random();

        // set input
        _imageFilter.SetInput(e.image);

        // randomly apply some filter to the image first
        switch (random.Next(7))
        {
        case 0:
            Log("Applying auto-adjust.");
            _imageFilter.AutoAdjust();
            break;

        case 1:
            Log("Applying sepia and sharpen.");
            _imageFilter.SepiaTone(1.0f);
            break;

        case 2:
            Log("Applying bloom and vignette.");
            // chaining multiple filters together
            _imageFilter.Bloom(10.0f, 1.0f)
            .Filter("CIVignette", new Dictionary <string, object> {
                { "inputRadius", 1.0f },
                { "inputIntensity", 0.5f }
            });
            break;

        case 3:
            Log("Applying color invert.");
            _imageFilter.ColorInvert();
            break;

        case 4:
            Log("Applying red monochrome.");
            _imageFilter.ColorMonochrome(new Color32(0xff, 0x00, 0x00, 0xff), 1.0f);
            break;

        case 5:
            Log("Applying yellow monochrome.");
            _imageFilter.ColorMonochrome(new Color32(0xff, 0xff, 0x00, 0xff), 1.0f);
            break;

        case 6:
            Log("Applying blue monochrome.");
            _imageFilter.ColorMonochrome(new Color32(0x00, 0x00, 0xff, 0xff), 1.0f);
            break;
        }

        // render the image
        _photo = _imageFilter.Render(
            new Rect(0, 0, e.image.size.Width, e.image.size.Height),
            null, CONVERT_SCALE, e.image.imageOrientation.ToCorrectedRotateAngle());

        // detect faces
        _faces = _faceDetector.DetectInImage(e.image);

        if (_faces.Length > 0)
        {
            _scrambledFaces = new Texture2D[_faces.Length];

            for (int i = 0; i < _faces.Length; i++)
            {
                var face = _faces[i];
//				Log("face: " + face.bounds + ", " + face.hasMouthPosition + ", " + face.leftEyePosition + ", " + face.rightEyePosition);

                // randomly scramble the faces
                _imageFilter.SetInput(_photo);

                switch (random.Next(3))
                {
                case 0:
                    Log("Pixellating face.");
                    _imageFilter.Pixellate(new float[] { 0, 0 }, 10);
                    break;

                case 1:
                    Log("Applying blur to face.");
                    _imageFilter.GaussianBlur(30);
                    break;

                case 2:
                    Log("Applying vortex distortion to face.");
                    _imageFilter.VortexDistortion(
                        new float[] { face.bounds.x *CONVERT_SCALE, face.bounds.y *CONVERT_SCALE },
                        3000, 9000
                        );
                    break;
                }

                // render the face only
                _scrambledFaces[i] = _imageFilter.Render(
                    new Rect(
                        face.bounds.x * CONVERT_SCALE,
                        face.bounds.y * CONVERT_SCALE,
                        face.bounds.width * CONVERT_SCALE,
                        face.bounds.height * CONVERT_SCALE
                        ));
            }
        }
    }
 /// <summary>
 /// Raises the <see cref="MediaPicked"/> event.
 /// </summary>
 /// <param name="e">The <see cref="MediaPickedEventArgs"/> instance containing the event data.</param>
 private static void RaiseOnMediaPicked(MediaPickedEventArgs e)
 {
     MediaPickerActivity.MediaPicked?.Invoke(null, e);
 }