Пример #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;
        }
Пример #2
0
 protected void Blur()
 {
     _blurInput.CopyFrom(_bitmapToBlur);
     _blurScript.SetInput(_blurInput);
     _blurScript.ForEach(_blurOutput);
     _blurOutput.CopyTo(_blurredBitmap);
 }
Пример #3
0
 public void Blur(Bitmap input, Bitmap output)
 {
     _mBlurInput.CopyFrom(input);
     _mBlurScript.SetInput(_mBlurInput);
     _mBlurScript.ForEach(_mBlurOutput);
     _mBlurOutput.CopyTo(output);
 }
Пример #4
0
 protected void blur(Bitmap bitmapToBlur, Bitmap blurredBitmap)
 {
     mBlurInput.CopyFrom(bitmapToBlur);
     mBlurScript.SetInput(mBlurInput);
     mBlurScript.ForEach(mBlurOutput);
     mBlurOutput.CopyTo(blurredBitmap);
 }
Пример #5
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);
        }
Пример #7
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);
            }
        }
        //public void OnImageAvailable(ImageReader reader)
        //{
        //  var handler = PreviewFrameAvailable;
        //  if (handler == null) { return; }

        //  //handler(this, null);
        //  using (var image = reader.AcquireNextImage())
        //  {
        //    try
        //    {
        //      //var plane = reader.AcquireNextImage().GetPlanes()[0];
        //      if (image == null) { return; }

        //      //var planes = image.GetPlanes();
        //      //var plane = planes[0];
        //      //var buffer = plane.Buffer;

        //      //buffer.Rewind();
        //      //byte[] bytes = new byte[buffer.Remaining()];
        //      //buffer.Get(bytes);

        //      ////using (var output = new FileOutputStream(new Java.IO.File(activity.GetExternalFilesDir(null), "florin-pic.jpg")))
        //      ////{
        //      ////  try
        //      ////  {
        //      ////    output.Write(bytes);
        //      ////  }
        //      ////  catch (Java.IO.IOException e)
        //      ////  {
        //      ////    System.Diagnostics.Debug.WriteLine($"{e}");
        //      ////  }
        //      ////}

        //      //using (Bitmap bitmap = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length))
        //      //{
        //      //  var rawBuffer = ByteBuffer.Allocate(bitmap.ByteCount);
        //      //  bitmap.CopyPixelsToBuffer(rawBuffer);
        //      //  rawBuffer.Rewind();
        //      //  byte[] rawBytes = new byte[rawBuffer.Remaining()];
        //      //  rawBuffer.Get(rawBytes);

        //      //  handler(this, new CameraPreviewEventArgs { FrameData = rawBytes, Width = bitmap.Width, Height = bitmap.Height });
        //      //}

        //      using (Bitmap bitmap = YUV_420_888_toRGB(image, image.Width, image.Height))
        //      {
        //          var rawBuffer = ByteBuffer.Allocate(bitmap.ByteCount);
        //          bitmap.CopyPixelsToBuffer(rawBuffer);
        //          rawBuffer.Rewind();
        //          byte[] rawBytes = new byte[rawBuffer.Remaining()];
        //          rawBuffer.Get(rawBytes);

        //          handler(this, new CameraPreviewEventArgs { FrameData = rawBytes, Width = bitmap.Width, Height = bitmap.Height });
        //      }
        //    }
        //    finally
        //    {
        //      image?.Close();
        //    }
        //  }
        //}

        private Bitmap YUV_420_888_toRGB(Image image, int width, int height)
        {
            // Get the three image planes
            Image.Plane[] planes = image.GetPlanes();
            ByteBuffer    buffer = planes[0].Buffer;

            byte[] y = new byte[buffer.Remaining()];
            buffer.Get(y);

            buffer = planes[1].Buffer;
            byte[] u = new byte[buffer.Remaining()];
            buffer.Get(u);

            buffer = planes[2].Buffer;
            byte[] v = new byte[buffer.Remaining()];
            buffer.Get(v);

            // get the relevant RowStrides and PixelStrides
            // (we know from documentation that PixelStride is 1 for y)
            int yRowStride    = planes[0].RowStride;
            int uvRowStride   = planes[1].RowStride;   // we know from   documentation that RowStride is the same for u and v.
            int uvPixelStride = planes[1].PixelStride; // we know from   documentation that PixelStride is the same for u and v.


            // 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);
        }