Ejemplo n.º 1
0
        public Bitmap BlurImage(Bitmap image)
        {
            if (image == null)
                return null;

            image = RGB565toARGB888(image);
            if (!configured)
            {
                input = Allocation.CreateFromBitmap(rs, image);
                output = Allocation.CreateTyped(rs, input.Type);
                script = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));
                script.SetRadius(BLUR_RADIUS);
                configured = true;
            }
            else
            {
                input.CopyFrom(image);
            }

            script.SetInput(input);
            script.ForEach(output);
            output.CopyTo(image);

            return image;
        }
Ejemplo n.º 2
0
 public void Blur(Bitmap input, Bitmap output)
 {
     _mBlurInput.CopyFrom(input);
     _mBlurScript.SetInput(_mBlurInput);
     _mBlurScript.ForEach(_mBlurOutput);
     _mBlurOutput.CopyTo(output);
 }
 private Bitmap FastBlur(Bitmap sentBitmap)
 {
     try
     {
         if (null == sentBitmap)
         {
             return(null);
         }
         Bitmap       outputBitmap = Bitmap.CreateBitmap(sentBitmap);
         RenderScript renderScript = RenderScript.Create(ActivityContext);
         Allocation   tmpIn        = Allocation.CreateFromBitmap(renderScript, sentBitmap);
         Allocation   tmpOut       = Allocation.CreateFromBitmap(renderScript, outputBitmap);
         //Intrinsic Gausian blur filter
         ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.Create(renderScript, Element.U8_4(renderScript));
         theIntrinsic.SetRadius(AppSettings.BlurRadiusComment);
         theIntrinsic.SetInput(tmpIn);
         theIntrinsic.ForEach(tmpOut);
         tmpOut.CopyTo(outputBitmap);
         return(outputBitmap);
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
         return(null);
     }
 }
Ejemplo n.º 4
0
 protected void blur(Bitmap bitmapToBlur, Bitmap blurredBitmap)
 {
     mBlurInput.CopyFrom(bitmapToBlur);
     mBlurScript.SetInput(mBlurInput);
     mBlurScript.ForEach(mBlurOutput);
     mBlurOutput.CopyTo(blurredBitmap);
 }
Ejemplo n.º 5
0
        //Retrieves a blurred image
        public static Drawable Difuminar(Drawable papelTapiz, short blurRadius)
        {
            Bitmap              originalBitmap = ((BitmapDrawable)papelTapiz).Bitmap;
            Bitmap              blurredBitmap  = Bitmap.CreateScaledBitmap(originalBitmap, originalBitmap.Width, originalBitmap.Height, false);
            RenderScript        rs             = RenderScript.Create(Application.Context);
            Allocation          input          = Allocation.CreateFromBitmap(rs, originalBitmap, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);
            Allocation          output         = Allocation.CreateTyped(rs, input.Type);
            ScriptIntrinsicBlur script         = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));

            script.SetInput(input);
            if (blurRadius < maxRadius)
            {
                script.SetRadius(blurRadius);
            }
            script.ForEach(output);
            output.CopyTo(blurredBitmap);
            Drawable papelTapizDifuminado = new BitmapDrawable(Android.Content.Res.Resources.System, blurredBitmap);

            originalBitmap.Recycle();
            originalBitmap.Dispose();
            blurredBitmap.Recycle();
            blurredBitmap.Dispose();
            input.Dispose();
            output.Dispose();
            return(papelTapizDifuminado);
        }
Ejemplo n.º 6
0
        public Bitmap Blur(Bitmap bitmap, float blurRadius)
        {
            // Allocation will use the same backing array of pixels as bitmap if created with USAGE_SHARED flag
            using (var inAllocation = Allocation.CreateFromBitmap(_renderScript, bitmap)) {
                if (!CanReuseAllocation(bitmap))
                {
                    if (_outAllocation != null)
                    {
                        _outAllocation.Destroy();
                        _outAllocation.Dispose();
                    }

                    _outAllocation    = Allocation.CreateTyped(_renderScript, inAllocation.Type);
                    _lastBitmapWidth  = bitmap.Width;
                    _lastBitmapHeight = bitmap.Height;
                }

                _blurScript.SetRadius(blurRadius);
                _blurScript.SetInput(inAllocation);
                // Do not use inAllocation in forEach, it will caue visual artifacts on blurred bitmap
                _blurScript.ForEach(_outAllocation);
                _outAllocation.CopyTo(bitmap);

                inAllocation.Destroy();
            }

            return(bitmap);
        }
        public Bitmap Transform(Bitmap source)
        {
            // Create another bitmap that will hold the results of the filter.
            Bitmap blurredBitmap = Bitmap.CreateBitmap(source);

            // Allocate memory for Renderscript to work with
            Allocation input  = Allocation.CreateFromBitmap(m_Rs, source, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);
            Allocation output = Allocation.CreateTyped(m_Rs, input.Type);

            // Load up an instance of the specific script that we want to use.
            ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(m_Rs, Element.U8_4(m_Rs));

            script.SetInput(input);

            // Set the blur radius
            script.SetRadius(m_BlurRadius);

            // Start the ScriptIntrinisicBlur
            script.ForEach(output);

            // Copy the output to the blurred bitmap
            output.CopyTo(blurredBitmap);

            if (blurredBitmap != source)
            {
                source.Recycle();
            }

            return(blurredBitmap);
        }
Ejemplo n.º 8
0
        private Bitmap CreateBlurredImage(int radius, Bitmap originalBitmap)
        {
            // Load a clean bitmap and work from that.

            //** Asi estaba antes Bitmap originalBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.dog_and_monkeys);

            // Create another bitmap that will hold the results of the filter.
            Bitmap blurredBitmap;

            blurredBitmap = Bitmap.CreateBitmap(originalBitmap);

            // Create the Renderscript instance that will do the work.
            RenderScript rs = RenderScript.Create(this);

            // Allocate memory for Renderscript to work with
            Allocation input  = Allocation.CreateFromBitmap(rs, originalBitmap, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);
            Allocation output = Allocation.CreateTyped(rs, input.Type);

            // Load up an instance of the specific script that we want to use.
            ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));

            script.SetInput(input);

            // Set the blur radius
            script.SetRadius(radius);

            // Start the ScriptIntrinisicBlur
            script.ForEach(output);

            // Copy the output to the blurred bitmap
            output.CopyTo(blurredBitmap);

            return(blurredBitmap);
        }
Ejemplo n.º 9
0
 protected void Blur()
 {
     _blurInput.CopyFrom(_bitmapToBlur);
     _blurScript.SetInput(_blurInput);
     _blurScript.ForEach(_blurOutput);
     _blurOutput.CopyTo(_blurredBitmap);
 }
Ejemplo n.º 10
0
        public static Bitmap RsBlur(Context context, Bitmap source, int radius)
        {
            Bitmap inputBmp = source;
            //(1)
            RenderScript renderScript = RenderScript.Create(context);

            // Allocate memory for Renderscript to work with
            //(2)
            Allocation input  = Allocation.CreateFromBitmap(renderScript, inputBmp);
            Allocation output = Allocation.CreateTyped(renderScript, input.Type);
            //(3)
            // Load up an instance of the specific script that we want to use.
            ScriptIntrinsicBlur scriptIntrinsicBlur = ScriptIntrinsicBlur
                                                      .Create(renderScript, Element.U8_4(renderScript));

            //(4)
            scriptIntrinsicBlur.SetInput(input);
            //(5)
            // Set the blur radius
            scriptIntrinsicBlur.SetRadius(radius);
            //(6)
            // Start the ScriptIntrinisicBlur
            scriptIntrinsicBlur.ForEach(output);
            //(7)
            // Copy the output to the blurred bitmap
            output.CopyTo(inputBmp);
            //(8)
            renderScript.Destroy();

            return(inputBmp);
        }
Ejemplo n.º 11
0
        private Bitmap CreateBlurredImage(Bitmap originalBitmap, int radius)
        {
            // Create another bitmap that will hold the results of the filter.
            Bitmap blurredBitmap;

            blurredBitmap = Bitmap.CreateBitmap(originalBitmap);

            // Create the Renderscript instance that will do the work.
            RenderScript rs = RenderScript.Create(Context);

            // Allocate memory for Renderscript to work with
            Allocation input  = Allocation.CreateFromBitmap(rs, originalBitmap, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);
            Allocation output = Allocation.CreateTyped(rs, input.Type);

            // Load up an instance of the specific script that we want to use.
            ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(rs, Android.Renderscripts.Element.U8_4(rs));

            script.SetInput(input);

            // Set the blur radius
            script.SetRadius(radius);

            // Start Renderscript working.
            script.ForEach(output);

            // Copy the output to the blurred bitmap
            output.CopyTo(blurredBitmap);

            return(blurredBitmap);
        }
Ejemplo n.º 12
0
    public Bitmap blur(Bitmap bitmap, float radius, int repeat)
    {
        if (!IS_BLUR_SUPPORTED)
        {
            return(null);
        }

        if (radius > MAX_RADIUS)
        {
            radius = MAX_RADIUS;
        }

        int width  = bitmap.Width;
        int height = bitmap.Height;

        // Create allocation type
        Type bitmapType = new Type.Builder(rs, Element.RGBA_8888(rs))
                          .SetX(width)
                          .SetY(height)
                          .SetMipmaps(false) // We are using MipmapControl.MIPMAP_NONE
                          .Create();

        // Create allocation
        Allocation allocation = Allocation.CreateTyped(rs, bitmapType);

        // Create blur script
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));

        blurScript.SetRadius(radius);

        // Copy data to allocation
        allocation.CopyFrom(bitmap);

        // set blur script input
        blurScript.SetInput(allocation);

        // invoke the script to blur
        blurScript.ForEach(allocation);

        // Repeat the blur for extra effect
        for (int i = 0; i < repeat; i++)
        {
            blurScript.ForEach(allocation);
        }

        // copy data back to the bitmap
        allocation.CopyTo(bitmap);

        // release memory
        allocation.Destroy();
        blurScript.Destroy();
        allocation = null;
        blurScript = null;

        return(bitmap);
    }
Ejemplo n.º 13
0
        private Bitmap Blur()
        {
            //Now I am letting the user to risk by letting them to choose to blur the image when the device runs android 10 and beyond
            if (new ConfigurationManager(AppPreferences.Default).RetrieveAValue(ConfigurationParameters.BlurEnabledForAndroid10) == false)
            {
                //Workaround Android Q: IT causes SIGSEV in Android 10 for some reason. XOM related maybe
                if (Build.VERSION.SdkInt >= BuildVersionCodes.Q)
                {
                    return(image);
                }
            }
            if (image == null)
            {
                return(image);
            }
            if (intensity == 0)
            {
                return(image); //No need to blur the image.
            }

            Bitmap input;

            if (image.Width > deviceWidth || image.Height > deviceHeight)
            {
                input = Bitmap.CreateScaledBitmap(image, deviceWidth, deviceHeight, false);
            }
            else
            {
                input = Bitmap.CreateScaledBitmap(image, deviceWidth, deviceHeight, true);
            }
            try
            {
                Bitmap output = Bitmap.CreateBitmap(input);

                RenderScript        rs            = RenderScript.Create(context);
                ScriptIntrinsicBlur intrinsicBlur = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));

                Allocation inputallocation  = Allocation.CreateFromBitmap(rs, input);
                Allocation outputallocation = Allocation.CreateFromBitmap(rs, output);
                intrinsicBlur.SetRadius(intensity);
                intrinsicBlur.SetInput(inputallocation);
                intrinsicBlur.ForEach(outputallocation);

                outputallocation.CopyTo(output);

                return(output);
            }
            catch
            {
                return(input);
            }
        }
Ejemplo n.º 14
0
        public static void BlurBitmapWithRenderscript(RenderScript rs, Bitmap bitmap2)
        {
            // this will blur the bitmapOriginal with a radius of 25
            // and save it in bitmapOriginal
            // use this constructor for best performance, because it uses
            // USAGE_SHARED mode which reuses memory
            Allocation          input  = Allocation.CreateFromBitmap(rs, bitmap2);
            Allocation          output = Allocation.CreateTyped(rs, input.Type);
            ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));

            // must be >0 and <= 25
            script.SetRadius(25f);
            script.SetInput(input);
            script.ForEach(output);
            output.CopyTo(bitmap2);
        }
Ejemplo n.º 15
0
        public static Bitmap Blur(Bitmap BMap, float Radius)
        {
            Bitmap       MutableBMap = BMap.Copy(Bitmap.Config.Argb8888, true);
            RenderScript RS          = RenderScript.Create(Application.Context);

            Allocation Input  = Allocation.CreateFromBitmap(RS, MutableBMap, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);
            Allocation Output = Allocation.CreateTyped(RS, Input.Type);

            ScriptIntrinsicBlur Script = ScriptIntrinsicBlur.Create(RS, Element.U8_4(RS));

            Script.SetInput(Input);
            Script.SetRadius(20);
            Script.ForEach(Output);

            Output.CopyTo(MutableBMap);
            return(MutableBMap);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Handles the drawing of the blurred view.
        /// </summary>
        /// <param name="canvas">The canvas on which to draw.</param>
        protected override void OnDraw(Canvas canvas)
        {
            base.OnDraw(canvas);

            if (BlurredView != null)
            {
                if (Prepare())
                {
                    bitmapToBlur.EraseColor(OverlayBackgroundColor);

                    BlurredView.Draw(blurringCanvas);

                    if (renderScript != null)
                    {
                        blurInput.CopyFrom(bitmapToBlur);
                        blurScript.SetInput(blurInput);
                        blurScript.ForEach(blurOutput);
                        blurOutput.CopyTo(blurredBitmap);
                    }
                    else
                    {
                        StackBlur.Blur(bitmapToBlur, blurredBitmap, BlurRadius);
                    }

                    canvas.Save();
                    float x = 0;
                    float y = 0;
                    if (Build.VERSION.SdkInt < BuildVersionCodes.Honeycomb)
                    {
                        x = BlurredView.Left - Left;
                        y = BlurredView.Top - Top;
                    }
                    else
                    {
                        x = BlurredView.GetX() - GetX();
                        y = BlurredView.GetY() - GetY();
                    }
                    canvas.Translate(x, y);
                    canvas.Scale(downsampleFactor, downsampleFactor);
                    canvas.DrawBitmap(blurredBitmap, 0, 0, null);
                    canvas.Restore();
                }

                canvas.DrawColor(OverlayColor);
            }
        }
Ejemplo n.º 17
0
        Bitmap CreateBlurredImage(int radius)
        {
            Bitmap originalBitmap      = BitmapFactory.DecodeResource(Resources, Resource.Drawable.imagebackground);
            Bitmap blurredBitmap       = Bitmap.CreateBitmap(originalBitmap);
            ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(renderScript, Element.U8_4(renderScript));
            Allocation          input  = Allocation.CreateFromBitmap(renderScript, originalBitmap, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);

            script.SetInput(input);
            script.SetRadius(radius);
            Allocation output = Allocation.CreateTyped(renderScript, input.Type);

            script.ForEach(output);
            output.CopyTo(blurredBitmap);
            output.Destroy();
            input.Destroy();
            script.Destroy();
            return(blurredBitmap);
        }
Ejemplo n.º 18
0
        public void Show()
        {
            var obj  = (Activity)Xamarin.Forms.Forms.Context;
            var root = obj.Window.DecorView.FindViewById(Resource.Id.Content);

            root.DrawingCacheEnabled = true;
            var b = Bitmap.CreateBitmap(root.GetDrawingCache(true));

            root.DrawingCacheEnabled = false;

            // Create another bitmap that will hold the results of the filter.
            Bitmap blurredBitmap;

            blurredBitmap = Bitmap.CreateBitmap(b);

            // Create the Renderscript instance that will do the work.
            RenderScript rs = RenderScript.Create(obj);

            // Allocate memory for Renderscript to work with
            Allocation input  = Allocation.CreateFromBitmap(rs, b, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);
            Allocation output = Allocation.CreateTyped(rs, input.Type);

            // Load up an instance of the specific script that we want to use.


            ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(rs, Renderscripts.Element.U8_4(rs));

            script.SetInput(input);

            // Set the blur radius
            script.SetRadius(25);

            // Start the ScriptIntrinisicBlur
            script.ForEach(output);

            // Copy the output to the blurred bitmap
            output.CopyTo(blurredBitmap);

            dialog = new Dialog(obj, Resource.Style.ThemeNoTitleBar);
            Drawable d = new BitmapDrawable(blurredBitmap);

            dialog.Window.SetBackgroundDrawable(d);
            dialog.Show();
        }
        public Bitmap CreateBlurredImageoffline(Context contexto, int radius, int resid)
        {
            // Load a clean bitmap and work from that.
            WallpaperManager wm = WallpaperManager.GetInstance(this);
            Drawable         d  = wm.Drawable;
            Bitmap           originalBitmap;

            originalBitmap = ((BitmapDrawable)d).Bitmap;

            if (Build.VERSION.SdkInt >= BuildVersionCodes.JellyBeanMr1)
            {
                // Create another bitmap that will hold the results of the filter.
                Bitmap blurredBitmap;
                blurredBitmap = Bitmap.CreateBitmap(originalBitmap);

                // Create the Renderscript instance that will do the work.
                RenderScript rs = RenderScript.Create(contexto);

                // Allocate memory for Renderscript to work with
                Allocation input  = Allocation.CreateFromBitmap(rs, originalBitmap, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);
                Allocation output = Allocation.CreateTyped(rs, input.Type);

                // Load up an instance of the specific script that we want to use.
                ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));
                script.SetInput(input);

                // Set the blur radius
                script.SetRadius(radius);

                // Start the ScriptIntrinisicBlur
                script.ForEach(output);

                // Copy the output to the blurred bitmap
                output.CopyTo(blurredBitmap);

                return(blurredBitmap);
            }
            else
            {
                return(originalBitmap);
            }
        }
Ejemplo n.º 20
0
        private void CreateBlurredImage(int radius)
        {
            // Load a clean bitmap and work from that.
            Bitmap originalBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.dog_and_monkeys);

            if (blurredBmp != null)
            {
                blurredBmp.Recycle();
                blurredBmp.Dispose();
                blurredBmp = null;
            }

            // Create another bitmap that will hold the results of the filter.
            blurredBmp = Bitmap.CreateBitmap(originalBitmap);

            // Create the Renderscript instance that will do the work.
            RenderScript rs = RenderScript.Create(this);

            // Allocate memory for Renderscript to work with
            Allocation input  = Allocation.CreateFromBitmap(rs, originalBitmap, Allocation.MipmapControl.MipmapFull, Allocation.UsageScript);
            Allocation output = Allocation.CreateTyped(rs, input.Type);

            // Load up an instance of the specific script that we want to use.
            ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));

            script.SetInput(input);

            // Set the blur radius
            script.SetRadius(radius);

            // Start Renderscript working.
            script.ForEach(output);

            // Copy the output to the blurred bitmap
            output.CopyTo(blurredBmp);

            input.Destroy();
            input.Dispose();

            output.Destroy();
            output.Dispose();
        }
Ejemplo n.º 21
0
        public static Bitmap Blur(Context ctx, Bitmap image)
        {
            int width  = (int)Math.Round(image.Width * BITMAP_SCALE);
            int height = (int)Math.Round(image.Height * BITMAP_SCALE);

            Bitmap inputBitmap  = Bitmap.CreateScaledBitmap(image, width, height, false);
            Bitmap outputBitmap = Bitmap.CreateBitmap(inputBitmap);

            var rs = RenderScript.Create(ctx);
            ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));
            Allocation          tmpIn        = Allocation.CreateFromBitmap(rs, inputBitmap);
            Allocation          tmpOut       = Allocation.CreateFromBitmap(rs, outputBitmap);

            theIntrinsic.SetRadius(BLUR_RADIUS);
            theIntrinsic.SetInput(tmpIn);
            theIntrinsic.ForEach(tmpOut);
            tmpOut.CopyTo(outputBitmap);

            return(outputBitmap);
        }
Ejemplo n.º 22
0
        public Bitmap FnBlurEffectToImage(Bitmap image)
        {
            if (image == null)
            {
                return(null);
            }

            Bitmap       outputBitmap = Bitmap.CreateBitmap(image);
            RenderScript renderScript = RenderScript.Create(this);
            Allocation   tmpIn        = Allocation.CreateFromBitmap(renderScript, image);
            Allocation   tmpOut       = Allocation.CreateFromBitmap(renderScript, outputBitmap);
            //Intrinsic Gausian blur filter
            ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.Create(renderScript, Element.U8_4(renderScript));

            theIntrinsic.SetRadius(10f);
            theIntrinsic.SetInput(tmpIn);
            theIntrinsic.ForEach(tmpOut);
            tmpOut.CopyTo(outputBitmap);
            return(outputBitmap);
        }
Ejemplo n.º 23
0
        Bitmap CreateBlurredImage(int radius)
        {
            // Load a clean bitmap to work from.
            Bitmap originalBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.dog_and_monkeys);

            // Create another bitmap that will hold the results of the filter.
            Bitmap blurredBitmap = Bitmap.CreateBitmap(originalBitmap);

            // Load up an instance of the specific script that we want to use.
            // An Element is similar to a C type. The second parameter, Element.U8_4,
            // tells the Allocation is made up of 4 fields of 8 unsigned bits.
            ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(renderScript, Element.U8_4(renderScript));

            // Create an Allocation for the kernel inputs.
            Allocation input = Allocation.CreateFromBitmap(renderScript, originalBitmap,
                                                           Allocation.MipmapControl.MipmapFull,
                                                           AllocationUsage.Script);

            // Assign the input Allocation to the script.
            script.SetInput(input);

            // Set the blur radius
            script.SetRadius(radius);

            // Finally we need to create an output allocation to hold the output of the Renderscript.
            Allocation output = Allocation.CreateTyped(renderScript, input.Type);

            // Next, run the script. This will run the script over each Element in the Allocation, and copy it's
            // output to the allocation we just created for this purpose.
            script.ForEach(output);

            // Copy the output to the blurred bitmap
            output.CopyTo(blurredBitmap);

            // Cleanup.
            output.Destroy();
            input.Destroy();
            script.Destroy();

            return(blurredBitmap);
        }
Ejemplo n.º 24
0
        protected override Android.Graphics.Bitmap Transform(Android.Graphics.Bitmap source)
        {
            Bitmap outBitmap = Bitmap.CreateBitmap(source.Width, source.Height, Bitmap.Config.Argb8888);
            Canvas canvas    = new Canvas(outBitmap);

            canvas.DrawBitmap(source, 0, 0, null);

            RenderScript        rs           = RenderScript.Create(mContext);
            Allocation          overlayAlloc = Allocation.CreateFromBitmap(rs, outBitmap);
            ScriptIntrinsicBlur blur         = ScriptIntrinsicBlur.Create(rs, overlayAlloc.Element);

            blur.SetInput(overlayAlloc);
            blur.SetRadius(mRadius);
            blur.ForEach(overlayAlloc);
            overlayAlloc.CopyTo(outBitmap);

            source.Recycle();
            rs.Destroy();

            return(outBitmap);
        }
Ejemplo n.º 25
0
        public static Bitmap Blur(this Bitmap image, Context context, float blurRadius = 25)
        {
            Bitmap outputBitmap = null;

            if (image != null)
            {
                if (blurRadius == 0)
                {
                    return(image);
                }

                if (blurRadius < 1)
                {
                    blurRadius = 1;
                }

                if (blurRadius > 25)
                {
                    blurRadius = 25;
                }

                BLUR_RADIUS = blurRadius;

                int width  = (int)Math.Round(image.Width * BITMAP_SCALE);
                int height = (int)Math.Round(image.Width * BITMAP_SCALE);

                Bitmap inputBitmap = Bitmap.CreateScaledBitmap(image, width, height, false);
                outputBitmap = Bitmap.CreateBitmap(inputBitmap);

                RenderScript        rs           = RenderScript.Create(context);
                ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));
                Allocation          tmpIn        = Allocation.CreateFromBitmap(rs, inputBitmap);
                Allocation          tmpOut       = Allocation.CreateFromBitmap(rs, outputBitmap);
                theIntrinsic.SetRadius(BLUR_RADIUS);
                theIntrinsic.SetInput(tmpIn);
                theIntrinsic.ForEach(tmpOut);
                tmpOut.CopyTo(outputBitmap);
            }
            return(outputBitmap);
        }
Ejemplo n.º 26
0
        public Drawable Difuminar(Drawable papelTapiz)
        {
            //Fondo de escritorio provista por el Argumento que se pasa en <papelTapiz>
            Bitmap originalBitmap = ((BitmapDrawable)papelTapiz).Bitmap;
            // Un bitmap null que almacenará la imagen difuminada.
            Bitmap blurredBitmap;

            //Asignar a este bitmap la imagen original para trabajar con ella.
            blurredBitmap = Bitmap.CreateBitmap(originalBitmap);
            //Crear la instancia de RenderScript que hará el trabajo
            RenderScript rs = RenderScript.Create(Application.Context);
            //Alocar memoria para que RenderScript trabaje.
            Allocation input  = Allocation.CreateFromBitmap(rs, originalBitmap, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);
            Allocation output = Allocation.CreateTyped(rs, input.Type);

            // Load up an instance of the specific script that we want to use.
            ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));

            script.SetInput(input);

            // Set the blur radius
            script.SetRadius(25);

            // Start the ScriptIntrinisicBlur
            script.ForEach(output);

            // Copy the output to the blurred bitmap
            output.CopyTo(blurredBitmap);

            //Scale the bitmap:
            Bitmap blurredBitMapResized = Bitmap.CreateScaledBitmap(blurredBitmap, 70, 80, false);

            Drawable papelTapizDifuminado = new BitmapDrawable(blurredBitMapResized);

            originalBitmap       = null;
            blurredBitmap        = null;
            blurredBitMapResized = null;
            return(papelTapizDifuminado);
        }
Ejemplo n.º 27
0
        public static Bitmap BlurBitmap(Bitmap smallBitmap, int radius, Context context)
        {
            Bitmap bitmap = Bitmap.CreateBitmap(
                smallBitmap.Width, smallBitmap.Height,
                Bitmap.Config.Argb8888);

            RenderScript renderScript = RenderScript.Create(context);

            Allocation blurInput  = Allocation.CreateFromBitmap(renderScript, smallBitmap);
            Allocation blurOutput = Allocation.CreateFromBitmap(renderScript, bitmap);

            ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.Create(renderScript,
                                                                  Element.U8_4(renderScript));

            blur.SetInput(blurInput);
            blur.SetRadius(radius); // radius must be 0 < r <= 25
            blur.ForEach(blurOutput);

            blurOutput.CopyTo(bitmap);
            renderScript.Destroy();

            return(bitmap);
        }
Ejemplo n.º 28
0
        public static Bitmap CreateBlurredImageFromBitmap(Context context, int radius, Bitmap image)
        {
            // Load a clean bitmap and work from that.
            Bitmap originalBitmap = image;


            if (Build.VERSION.SdkInt >= BuildVersionCodes.JellyBeanMr1)
            {
                try
                {
                    Bitmap blurredBitmap;
                    blurredBitmap = Bitmap.CreateBitmap(originalBitmap);
                    RenderScript        rs     = RenderScript.Create(context);
                    Allocation          input  = Allocation.CreateFromBitmap(rs, originalBitmap, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);
                    Allocation          output = Allocation.CreateTyped(rs, input.Type);
                    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));
                    script.SetInput(input);
                    script.SetRadius(radius);
                    script.ForEach(output);
                    output.CopyTo(blurredBitmap);
                    output.Dispose();
                    script.Dispose();
                    input.Dispose();
                    rs.Dispose();
                    return(blurredBitmap);
                }
                catch (Exception)
                {
                    return(originalBitmap);
                }
            }
            else
            {
                return(originalBitmap);
            }
        }
Ejemplo n.º 29
0
        public async void RefreshPlayer()
        {
            while (MainActivity.instance == null || MusicPlayer.CurrentID() == -1)
            {
                await Task.Delay(100);
            }

            Song current = await MusicPlayer.GetItem();

            if (current == null || (current.IsYt && current.Album == null))
            {
                return;
            }

            FrameLayout smallPlayer = MainActivity.instance.FindViewById <FrameLayout>(Resource.Id.smallPlayer);

            smallPlayer.FindViewById <TextView>(Resource.Id.spTitle).Text  = current.Title;
            smallPlayer.FindViewById <TextView>(Resource.Id.spArtist).Text = current.Artist;
            smallPlayer.FindViewById <ImageView>(Resource.Id.spPlay).SetImageResource(Resource.Drawable.Pause);
            ImageView art = smallPlayer.FindViewById <ImageView>(Resource.Id.spArt);

            if (!current.IsYt)
            {
                var songCover       = Android.Net.Uri.Parse("content://media/external/audio/albumart");
                var nextAlbumArtUri = ContentUris.WithAppendedId(songCover, current.AlbumArt);

                Picasso.With(Application.Context).Load(nextAlbumArtUri).Placeholder(Resource.Drawable.noAlbum).Resize(400, 400).CenterCrop().Into(art);
            }
            else
            {
                Picasso.With(Application.Context).Load(current.Album).Placeholder(Resource.Drawable.noAlbum).Transform(new RemoveBlackBorder(true)).Into(art);
            }

            TextView title  = MainActivity.instance.FindViewById <TextView>(Resource.Id.playerTitle);
            TextView artist = MainActivity.instance.FindViewById <TextView>(Resource.Id.playerArtist);

            albumArt = MainActivity.instance.FindViewById <ImageView>(Resource.Id.playerAlbum);
            SpannableString titleText = new SpannableString(current.Title);

            titleText.SetSpan(new BackgroundColorSpan(Color.ParseColor("#BF000000")), 0, current.Title.Length, SpanTypes.InclusiveInclusive);
            title.TextFormatted = titleText;
            SpannableString artistText = new SpannableString(current.Artist);

            artistText.SetSpan(new BackgroundColorSpan(Color.ParseColor("#BF000000")), 0, current.Artist.Length, SpanTypes.InclusiveInclusive);
            artist.TextFormatted = artistText;

            if (!errorState)
            {
                if (MusicPlayer.isRunning)
                {
                    MainActivity.instance.FindViewById <ImageButton>(Resource.Id.playButton).SetImageResource(Resource.Drawable.Pause);
                    smallPlayer.FindViewById <ImageButton>(Resource.Id.spPlay).SetImageResource(Resource.Drawable.Pause);
                }
                else
                {
                    MainActivity.instance.FindViewById <ImageButton>(Resource.Id.playButton).SetImageResource(Resource.Drawable.Play);
                    smallPlayer.FindViewById <ImageButton>(Resource.Id.spPlay).SetImageResource(Resource.Drawable.Play);
                }
            }

            Bitmap drawable = null;

            if (current.AlbumArt == -1)
            {
                await Task.Run(() =>
                {
                    try
                    {
                        drawable = Picasso.With(Application.Context).Load(current.Album).Error(Resource.Drawable.noAlbum).Placeholder(Resource.Drawable.noAlbum).Transform(new RemoveBlackBorder(true)).Get();
                    }
                    catch
                    {
                        drawable = Picasso.With(Application.Context).Load(Resource.Drawable.noAlbum).Get();
                    }
                });
            }
            else
            {
                Android.Net.Uri songCover = Android.Net.Uri.Parse("content://media/external/audio/albumart");
                Android.Net.Uri iconURI   = ContentUris.WithAppendedId(songCover, current.AlbumArt);

                await Task.Run(() =>
                {
                    try
                    {
                        drawable = Picasso.With(Application.Context).Load(iconURI).Error(Resource.Drawable.noAlbum).Placeholder(Resource.Drawable.noAlbum).NetworkPolicy(NetworkPolicy.Offline).Get();
                    }
                    catch
                    {
                        drawable = Picasso.With(Application.Context).Load(Resource.Drawable.noAlbum).Get();
                    }
                });
            }

            albumArt.SetImageBitmap(drawable);
            Palette.From(drawable).MaximumColorCount(28).Generate(this);

            if (await SongManager.IsFavorite(current))
            {
                MainActivity.instance?.FindViewById <ImageButton>(Resource.Id.fav)?.SetImageResource(Resource.Drawable.Unfav);
            }
            else
            {
                MainActivity.instance?.FindViewById <ImageButton>(Resource.Id.fav)?.SetImageResource(Resource.Drawable.Fav);
            }


            if (albumArt.Width > 0)
            {
                try
                {
                    //k is the coeficient to convert ImageView's size to Bitmap's size
                    //We want to take the lower coeficient because if we take the higher, we will have a final bitmap larger than the initial one and we can't create pixels
                    float k      = Math.Min((float)drawable.Height / albumArt.Height, (float)drawable.Width / albumArt.Width);
                    int   width  = (int)(albumArt.Width * k);
                    int   height = (int)(albumArt.Height * k);

                    int dX = (int)((drawable.Width - width) * 0.5f);
                    int dY = (int)((drawable.Height - height) * 0.5f);

                    Console.WriteLine("&Drawable Info: Width: " + drawable.Width + " Height: " + drawable.Height);
                    Console.WriteLine("&AlbumArt Info: Width: " + albumArt.Width + " Height: " + albumArt.Height);
                    Console.WriteLine("&Blur Creation: Width: " + width + " Height: " + height + " dX: " + dX + " dY: " + dY);
                    //The width of the view in pixel (we'll multiply this by 0.75f because the drawer has a width of 75%)
                    Bitmap blured = Bitmap.CreateBitmap(drawable, dX, dY, (int)(width * 0.75f), height);
                    Console.WriteLine("&BLured bitmap created");

                    RenderScript        rs      = RenderScript.Create(MainActivity.instance);
                    Allocation          input   = Allocation.CreateFromBitmap(rs, blured);
                    Allocation          output  = Allocation.CreateTyped(rs, input.Type);
                    ScriptIntrinsicBlur blurrer = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));
                    blurrer.SetRadius(13);
                    blurrer.SetInput(input);
                    blurrer.ForEach(output);

                    output.CopyTo(blured);
                    MainActivity.instance.FindViewById <ImageView>(Resource.Id.queueBackground).SetImageBitmap(blured);
                    Console.WriteLine("&Bitmap set to image view");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("&Queue background error: " + ex.Message);
                }
            }

            if (bar != null)
            {
                if (spBar == null)
                {
                    spBar = MainActivity.instance.FindViewById <ProgressBar>(Resource.Id.spProgress);
                }

                if (current.IsLiveStream)
                {
                    bar.Max        = 1;
                    bar.Progress   = 1;
                    spBar.Max      = 1;
                    spBar.Progress = 1;
                    timer.Text     = "🔴 LIVE";
                }
                else
                {
                    int duration = await MusicPlayer.Duration();

                    bar.Max        = duration;
                    timer.Text     = string.Format("{0} | {1}", DurationToTimer((int)MusicPlayer.CurrentPosition), DurationToTimer(duration));
                    spBar.Max      = duration;
                    spBar.Progress = (int)MusicPlayer.CurrentPosition;

                    handler.PostDelayed(UpdateSeekBar, 1000);

                    int LoadedMax = (int)await MusicPlayer.LoadDuration();

                    bar.Max    = LoadedMax;
                    spBar.Max  = LoadedMax;
                    timer.Text = string.Format("{0} | {1}", DurationToTimer((int)MusicPlayer.CurrentPosition), DurationToTimer(LoadedMax));
                }
            }
        }
Ejemplo n.º 30
0
        private Bitmap CreateBitmap(ShadeInfo shadeInfo)
        {
#if DEBUG
            var stopWatch = new Stopwatch();
            stopWatch.Start();
#endif
            var shadow = Bitmap.CreateBitmap(
                shadeInfo.Width,
                shadeInfo.Height,
                Bitmap.Config.Argb8888);

            InternalLogger.Debug(LogTag, () => $"CreateBitmap( shadeInfo: {shadeInfo} )");
            RectF rect = new RectF(
                ShadeInfo.Padding,
                ShadeInfo.Padding,
                shadeInfo.Width - ShadeInfo.Padding,
                shadeInfo.Height - ShadeInfo.Padding);

            using var bitmapCanvas = new Canvas(shadow);
            using var paint        = new Paint { Color = shadeInfo.Color };
            bitmapCanvas.DrawRoundRect(
                rect,
                _cornerRadius,
                _cornerRadius,
                paint);

            if (shadeInfo.BlurRadius < 1)
            {
                return(shadow);
            }

            const int MaxBlur    = 25;
            float     blurAmount = shadeInfo.BlurRadius > MaxRadius ? MaxRadius : shadeInfo.BlurRadius;
            while (blurAmount > 0)
            {
                Allocation input = Allocation.CreateFromBitmap(
                    _renderScript,
                    shadow,
                    Allocation.MipmapControl.MipmapNone,
                    AllocationUsage.Script);
                Allocation          output = Allocation.CreateTyped(_renderScript, input.Type);
                ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(_renderScript, Element.U8_4(_renderScript));

                float blurRadius;
                if (blurAmount > MaxBlur)
                {
                    blurRadius  = MaxBlur;
                    blurAmount -= MaxBlur;
                }
                else
                {
                    blurRadius = blurAmount;
                    blurAmount = 0;
                }

                script.SetRadius(blurRadius);
                script.SetInput(input);
                script.ForEach(output);
                output.CopyTo(shadow);
            }
#if DEBUG
            LogPerf(LogTag, stopWatch);
#endif
            return(shadow);
        }