Пример #1
0
        public Bitmap Transform(Bitmap source)
        {
            // Load a clean bitmap and work from that.
            var originalBitmap = source;

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

            blurredBitmap = Bitmap.CreateBitmap(originalBitmap);

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

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

            // Load up an instance of the specific script that we want to use.
            var 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);

            source.Recycle();
            return(blurredBitmap);
        }
Пример #2
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 override Bitmap Operations(Bitmap source, Bitmap result)
        {
            Bitmap originalBitmap = source;
            Bitmap blurredBitmap  = null;

            blurredBitmap = Bitmap.CreateBitmap(originalBitmap);

            var rs = RenderScript.Create(mContext);

            var input = Allocation.CreateFromBitmap(rs, originalBitmap,
                                                    Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);
            var output = Allocation.CreateTyped(rs, input.Type);

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

            script.SetInput(input);
            script.SetRadius(mRadius);

            script.ForEach(output);
            output.CopyTo(blurredBitmap);

            source.Recycle();

            return(blurredBitmap);
        }
Пример #4
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;
        }
Пример #5
0
        public bool Prepare(Context context, Bitmap buffer, float radius)
        {
            if (_mRenderScript == null)
            {
                try
                {
                    _mRenderScript = RenderScript.Create(context);
                    _mBlurScript   = ScriptIntrinsicBlur.Create(_mRenderScript, Element.U8_4(_mRenderScript));
                }
                catch (Android.Renderscripts.RSRuntimeException e)
                {
                    if (DEBUG)
                    {
                        throw e;
                    }
                    else
                    {
                        // In release mode, just ignore
                        Release();
                        return(false);
                    }
                }
            }

            _mBlurScript.SetRadius(radius);

            _mBlurInput = Allocation.CreateFromBitmap(
                _mRenderScript,
                buffer,
                Allocation.MipmapControl.MipmapNone,
                AllocationUsage.Script);
            _mBlurOutput = Allocation.CreateTyped(_mRenderScript, _mBlurInput.Type);

            return(true);
        }
Пример #6
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);
        }
        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);
        }
Пример #8
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);
        }
Пример #9
0
        private void YUV_420_888_toRGBIntrinsicsProcess(int width, int height, byte[] yuv)
        {
            if (yuv.Length != _yuvLength)
            {
                Android.Renderscripts.Type.Builder yuvType =
                    new Android.Renderscripts.Type.Builder(rs, Element.U8(rs)).SetX(yuv.Length);
                if (_input != null)
                {
                    _input.Destroy();
                }
                _input     = Allocation.CreateTyped(rs, yuvType.Create(), AllocationUsage.Script);
                _yuvLength = yuv.Length;
            }

            if (_width != width || _height != height)
            {
                Android.Renderscripts.Type.Builder rgbaType = new Android.Renderscripts.Type.Builder(rs, Element.RGBA_8888(rs)).SetX(width).SetY(height);
                if (_output != null)
                {
                    _output.Destroy();
                }
                _output = Allocation.CreateTyped(rs, rgbaType.Create(), AllocationUsage.Script);
                _width  = width;
                _height = height;
            }


            _input.CopyFromUnchecked(yuv);

            _yuvToRgbIntrinsic.SetInput(_input);
            _yuvToRgbIntrinsic.ForEach(_output);
        }
Пример #10
0
        private Bitmap CreateResizedImage(Bitmap originalBitmap)
        {
            int width  = Convert.ToInt32(Math.Round(originalBitmap.Width * BITMAP_SCALE));
            int height = Convert.ToInt32(Math.Round(originalBitmap.Height * BITMAP_SCALE));

            // Create another bitmap that will hold the results of the filter.
            Bitmap inputBitmap  = Bitmap.CreateScaledBitmap(originalBitmap, width, height, false);
            Bitmap outputBitmap = Bitmap.CreateBitmap(inputBitmap);

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


            Allocation tmpIn  = Allocation.CreateFromBitmap(rs, inputBitmap);
            Allocation tmpOut = Allocation.CreateFromBitmap(rs, outputBitmap);

            // Allocate memory for Renderscript to work with
            var        t          = Android.Renderscripts.Type.CreateXY(rs, tmpIn.Element, Convert.ToInt32(width * RESIZE_SCALE), Convert.ToInt32(height * RESIZE_SCALE));
            Allocation tmpScratch = Allocation.CreateTyped(rs, t);

            ScriptIntrinsicResize theIntrinsic = ScriptIntrinsicResize.Create(rs);

            // Resize the original img down.
            theIntrinsic.SetInput(tmpIn);
            theIntrinsic.ForEach_bicubic(tmpScratch);

            // Resize smaller img up.
            theIntrinsic.SetInput(tmpScratch);
            theIntrinsic.ForEach_bicubic(tmpOut);
            tmpOut.CopyTo(outputBitmap);

            return(outputBitmap);
        }
Пример #11
0
        public ViewfinderProcessor(RenderScript rs, Size dimensions)
        {
            Type.Builder yuvTypeBuilder = new Type.Builder(rs, Element.YUV(rs));
            yuvTypeBuilder.SetX(dimensions.Width);
            yuvTypeBuilder.SetY(dimensions.Height);
            yuvTypeBuilder.SetYuvFormat((int)ImageFormatType.Yuv420888);
            mInputHdrAllocation = Allocation.CreateTyped(rs, yuvTypeBuilder.Create(),
                                                         AllocationUsage.IoInput | AllocationUsage.Script);

            Type.Builder rgbTypeBuilder = new Type.Builder(rs, Element.RGBA_8888(rs));
            rgbTypeBuilder.SetX(dimensions.Width);
            rgbTypeBuilder.SetY(dimensions.Height);
            mPrevAllocation = Allocation.CreateTyped(rs, rgbTypeBuilder.Create(),
                                                     AllocationUsage.Script);
            mOutputAllocation = Allocation.CreateTyped(rs, rgbTypeBuilder.Create(),
                                                       AllocationUsage.IoOutput | AllocationUsage.Script);

            mHdrYuvToRGBScript    = ScriptIntrinsicYuvToRGB.Create(rs, Element.RGBA_8888(rs));
            mHdrColorMatrixScript = ScriptIntrinsicColorMatrix.Create(rs);
            mHdrColorMatrixScript.SetColorMatrix(new Matrix4f(new float[] { 0.5f, 0, 0, 0, 0, 0.5f, 0, 0, 0, 0, 0.5f, 0, 0, 0, 0, 0.5f }));
            mHdrBlendScript = ScriptIntrinsicBlend.Create(rs, Element.RGBA_8888(rs));

            mDimensions   = dimensions;
            mFrameCounter = 0;

            SetRenderMode(ModeNormal);
        }
Пример #12
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);
        }
Пример #13
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);
        }
        public static Drawable CreateBlurredImageFromBitmap(Bitmap bm, Context context, int size)
        {
            var rs      = RenderScript.Create(context);
            var options = new BitmapFactory.Options
            {
                InSampleSize = size
            };

            byte[] imageInBytes;
            using (var stream = new MemoryStream())
            {
                bm.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
                imageInBytes = stream.ToArray();
            }

            var blurred = BitmapFactory.DecodeByteArray(imageInBytes, 0, imageInBytes.Length);

            var input  = Allocation.CreateFromBitmap(rs, blurred);
            var output = Allocation.CreateTyped(rs, input.Type);

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

            script.SetRadius(8f);
            script.SetInput(input);
            script.ForEach(output);

            output.CopyTo(blurred);

            return(new BitmapDrawable(context.Resources, blurred));
        }
Пример #15
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);
    }
        /// <summary>
        /// Converts YUV_420_888 raw data to RGB bitmap.
        /// </summary>
        /// <returns>The RGB bitmap.</returns>
        /// <param name="y">Y</param>
        /// <param name="u">U</param>
        /// <param name="v">V</param>
        /// <param name="width">Width</param>
        /// <param name="height">Height</param>
        /// <param name="yRowStride">Y row stride (we know from documentation that PixelStride is 1 for y).</param>
        /// <param name="uvRowStride">UV row stride (we know from   documentation that RowStride is the same for u and v).</param>
        /// <param name="uvPixelStride">Uv pixel stride (we know from   documentation that PixelStride is the same for u and v).</param>
        public Bitmap Yuv420888ToRgb(byte[] y, byte[] u, byte[] v, int width, int height, int yRowStride, int uvRowStride, int uvPixelStride)
        {
            // rs creation just for demo. Create rs just once in onCreate and use it again.
            //RenderScript rs = (activity as MainActivity).RenderScript; // RenderScript.create(this);
            RenderScript rs = RenderScript.Create(Android.App.Application.Context);
            //RenderScript rs = MainActivity.rs;
            ScriptC_yuv420888 mYuv420 = new ScriptC_yuv420888(rs);

            // Y,U,V are defined as global allocations, the out-Allocation is the Bitmap.
            // Note also that uAlloc and vAlloc are 1-dimensional while yAlloc is 2-dimensional.
            RType.Builder typeUcharY = new RType.Builder(rs, Element.U8(rs));
            typeUcharY.SetX(yRowStride).SetY(height);
            Allocation yAlloc = Allocation.CreateTyped(rs, typeUcharY.Create());

            yAlloc.CopyFrom(y);
            mYuv420.Set_ypsIn(yAlloc);

            RType.Builder typeUcharUV = new RType.Builder(rs, Element.U8(rs));
            // note that the size of the u's and v's are as follows:
            //      (  (width/2)*PixelStride + padding  ) * (height/2)
            // =    (RowStride                          ) * (height/2)
            // but I noted that on the S7 it is 1 less...
            typeUcharUV.SetX(u.Length);
            Allocation uAlloc = Allocation.CreateTyped(rs, typeUcharUV.Create());

            uAlloc.CopyFrom(u);
            mYuv420.Set_uIn(uAlloc);

            Allocation vAlloc = Allocation.CreateTyped(rs, typeUcharUV.Create());

            vAlloc.CopyFrom(v);
            mYuv420.Set_vIn(vAlloc);

            // handover parameters
            mYuv420.Set_picWidth(width);
            mYuv420.Set_uvRowStride(uvRowStride);
            mYuv420.Set_uvPixelStride(uvPixelStride);

            Bitmap     outBitmap = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888);
            Allocation outAlloc  = Allocation.CreateFromBitmap(rs, outBitmap, Allocation.MipmapControl.MipmapNone, Allocation.UsageScript);

            Script.LaunchOptions lo = new Script.LaunchOptions();
            lo.SetX(0, width); // by this we ignore the y’s padding zone, i.e. the right side of x between width and yRowStride
            lo.SetY(0, height);

            mYuv420.ForEach_doConvert(outAlloc, lo);
            outAlloc.CopyTo(outBitmap);

            return(outBitmap);
        }
Пример #17
0
        protected bool Prepare()
        {
            var width  = _mBlurredView.Width;
            var height = _mBlurredView.Height;

            if (_mBlurringCanvas != null && !_mDownsampleFactorChanged && _mBlurredViewWidth == width &&
                _mBlurredViewHeight == height)
            {
                return(true);
            }
            _mDownsampleFactorChanged = false;

            _mBlurredViewWidth  = width;
            _mBlurredViewHeight = height;

            var scaledWidth  = width / _mDownsampleFactor;
            var scaledHeight = height / _mDownsampleFactor;

            // The following manipulation is to avoid some RenderScript artifacts at the edge.
            scaledWidth  = scaledWidth - scaledWidth % 4 + 4;
            scaledHeight = scaledHeight - scaledHeight % 4 + 4;

            if (_mBlurredBitmap == null ||
                _mBlurredBitmap.Width != scaledWidth ||
                _mBlurredBitmap.Height != scaledHeight)
            {
                _mBitmapToBlur = Bitmap.CreateBitmap(scaledWidth, scaledHeight,
                                                     Bitmap.Config.Argb8888);
                if (_mBitmapToBlur == null)
                {
                    return(false);
                }

                _mBlurredBitmap = Bitmap.CreateBitmap(scaledWidth, scaledHeight,
                                                      Bitmap.Config.Argb8888);
                if (_mBlurredBitmap == null)
                {
                    return(false);
                }
            }

            _mBlurringCanvas = new Canvas(_mBitmapToBlur);
            _mBlurringCanvas.Scale(1f / _mDownsampleFactor, 1f / _mDownsampleFactor);
            _mBlurInput = Allocation.CreateFromBitmap(_mRenderScript, _mBitmapToBlur,
                                                      Allocation.MipmapControl.MipmapNone, AllocationUsage.Script);
            _mBlurOutput = Allocation.CreateTyped(_mRenderScript, _mBlurInput.Type);
            return(true);
        }
Пример #18
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);
        }
Пример #19
0
        protected bool prepare()
        {
            int width  = mBlurredView.Width;
            int height = mBlurredView.Height;

            if (mBlurringCanvas == null || mDownsampleFactorChanged ||
                mBlurredViewWidth != width || mBlurredViewHeight != height)
            {
                mDownsampleFactorChanged = false;

                mBlurredViewWidth  = width;
                mBlurredViewHeight = height;

                int scaledWidth  = width / mDownsampleFactor;
                int scaledHeight = height / mDownsampleFactor;

                // The following manipulation is to avoid some RenderScript artifacts at the edge.
                scaledWidth  = scaledWidth - scaledWidth % 4 + 4;
                scaledHeight = scaledHeight - scaledHeight % 4 + 4;

                if (mBlurredBitmap == null ||
                    mBlurredBitmap.Width != scaledWidth ||
                    mBlurredBitmap.Height != scaledHeight)
                {
                    mBitmapToBlur = Bitmap.CreateBitmap(scaledWidth, scaledHeight,
                                                        Bitmap.Config.Argb8888);
                    if (mBitmapToBlur == null)
                    {
                        return(false);
                    }

                    mBlurredBitmap = Bitmap.CreateBitmap(scaledWidth, scaledHeight,
                                                         Bitmap.Config.Argb8888);
                    if (mBlurredBitmap == null)
                    {
                        return(false);
                    }
                }

                mBlurringCanvas = new Canvas(mBitmapToBlur);
                mBlurringCanvas.Scale(1f / mDownsampleFactor, 1f / mDownsampleFactor);
                mBlurInput = Allocation.CreateFromBitmap(mRenderScript, mBitmapToBlur,
                                                         Allocation.MipmapControl.MipmapNone, AllocationUsage.Script);
                mBlurOutput = Allocation.CreateTyped(mRenderScript, mBlurInput.Type);
            }
            return(true);
        }
Пример #20
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);
        }
Пример #21
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);
        }
Пример #22
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();
        }
Пример #23
0
        private bool Prepare()
        {
            int width  = BlurredView.Width;
            int height = BlurredView.Height;

            if (blurringCanvas == null || downsampleFactorChanged || blurredViewWidth != width || blurredViewHeight != height)
            {
                downsampleFactorChanged = false;

                blurredViewWidth  = width;
                blurredViewHeight = height;

                int scaledWidth  = width / downsampleFactor;
                int scaledHeight = height / downsampleFactor;

                scaledWidth  = scaledWidth - scaledWidth % 4 + 4;
                scaledHeight = scaledHeight - scaledHeight % 4 + 4;

                if (blurredBitmap == null || blurredBitmap.Width != scaledWidth || blurredBitmap.Height != scaledHeight)
                {
                    bitmapToBlur = Bitmap.CreateBitmap(scaledWidth, scaledHeight, Bitmap.Config.Argb8888);
                    if (bitmapToBlur == null)
                    {
                        return(false);
                    }

                    blurredBitmap = Bitmap.CreateBitmap(scaledWidth, scaledHeight, Bitmap.Config.Argb8888);
                    if (blurredBitmap == null)
                    {
                        return(false);
                    }
                }

                blurringCanvas = new Canvas(bitmapToBlur);
                blurringCanvas.Scale(1f / downsampleFactor, 1f / downsampleFactor);
                if (renderScript != null)
                {
                    blurInput  = Allocation.CreateFromBitmap(renderScript, bitmapToBlur, Allocation.MipmapControl.MipmapNone, Allocation.UsageScript);
                    blurOutput = Allocation.CreateTyped(renderScript, blurInput.Type);
                }
            }
            return(true);
        }
        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);
            }
        }
Пример #25
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();
        }
Пример #26
0
        private Bitmap CreateBlurredImage(int radius, Bitmap originalBitmap)
        {
            Bitmap blurredBitmap;

            blurredBitmap = Bitmap.CreateBitmap(originalBitmap);

            var rs     = RenderScript.Create(Forms.Context);
            var input  = Allocation.CreateFromBitmap(rs, originalBitmap, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);
            var output = Allocation.CreateTyped(rs, input.Type);

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

            script.SetInput(input);
            script.SetRadius(radius);
            script.ForEach(output);

            output.CopyTo(blurredBitmap);

            return(blurredBitmap);
        }
Пример #27
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);
        }
Пример #28
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);
        }
        void BlurImage(int radius)
        {
            // Disable the event handler and the Seekbar to prevent this from
            // happening multiple times.
            _seekbar.ProgressChanged -= BlurImageHandler;
            _seekbar.Enabled          = false;

            _imageView.SetImageDrawable(null);

            Task.Run(() =>
            {
                // Load a clean bitmap and work from that.
                var sentBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.dog_and_monkeys);

                var bitmap = sentBitmap.Copy(sentBitmap.GetConfig(), true);
                var rs     = RenderScript.Create(this);
                var input  = Allocation.CreateFromBitmap(rs, sentBitmap, Allocation.MipmapControl.MipmapNone, Allocation.UsageScript);
                var output = Allocation.CreateTyped(rs, input.Type);
                var script = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));
                script.SetRadius(radius);
                script.SetInput(input);
                script.ForEach(output);
                output.CopyTo(bitmap);
                // clean up renderscript resources
                rs.Destroy();
                input.Destroy();
                output.Destroy();
                script.Destroy();

                RunOnUiThread(() =>
                {
                    _imageView.SetImageBitmap(bitmap);
                    _seekbar.ProgressChanged += BlurImageHandler;
                    _seekbar.Enabled          = true;
                });
            });
        }
Пример #30
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);
            }
        }