Android Code Snippet [B4X] [BitmapCreator] Blur Image

Discussion in 'Code Snippets' started by Erel, May 13, 2018.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Returns a blurred image:

    [​IMG]

    Code:
    Private Sub Blur (bmp As B4XBitmap) As B4XBitmap
       
    Dim n As Long = DateTime.Now
       
    Dim bc As BitmapCreator
       
    Dim ReduceScale As Int = 2
       bc.Initialize(bmp.Width / ReduceScale / bmp.Scale, bmp.Height / ReduceScale / bmp.Scale)
       bc.CopyPixelsFromBitmap(bmp)
       
    Dim count As Int = 3
       
    Dim clrs(3As ARGBColor
       
    Dim temp As ARGBColor
       
    Dim m As Int
       
    For steps = 1 To count
           
    For y = 0 To bc.mHeight - 1
               
    For x = 0 To 2
                   bc.GetARGB(x, y, clrs(x))
               
    Next
               SetAvg(bc, 
    1, y, clrs, temp)
               m = 
    0
               
    For x = 2 To bc.mWidth - 2
                   bc.GetARGB(x + 
    1, y, clrs(m))
                   m = (m + 
    1Mod clrs.Length
                   SetAvg(bc, x, y, clrs, temp)
               
    Next
           
    Next
           
    For x = 0 To bc.mWidth - 1
               
    For y = 0 To 2
                   bc.GetARGB(x, y, clrs(y))
               
    Next
               SetAvg(bc, x, 
    1, clrs, temp)
               m = 
    0
               
    For y = 2 To bc.mHeight - 2
                   bc.GetARGB(x, y + 
    1, clrs(m))
                   m = (m + 
    1Mod clrs.Length
                   SetAvg(bc, x, y, clrs, temp)
               
    Next
           
    Next
       
    Next
       
    Log(DateTime.Now - n)
       
    Return bc.Bitmap
    End Sub

    Private Sub SetAvg(bc As BitmapCreator, x As Int, y As Int, clrs() As ARGBColor, temp As ARGBColor)
       temp.Initialize
       
    For Each c As ARGBColor In clrs
           temp.r = temp.r + c.r
           temp.g = temp.g + c.g
           temp.b = temp.b + c.b
       
    Next
       temp.a = 
    255
       temp.r = temp.r / clrs.Length
       temp.g = temp.g / clrs.Length
       temp.b = temp.b / clrs.Length
       bc.SetARGB(x, y, temp)
    End Sub
    Usage example:

    Code:
    Dim bmp As B4XBitmap = xui.LoadBitmapResize(File.DirAssets, "bas-van-brandwijk-588535-unsplash.jpg", ImageView1.Width, ImageView1.Height, True)
    ImageView1.SetBitmap(Blur(bmp))
    Depends on: BitmapCreator and XUI libraries

    Update: The previous code incorrectly scaled the bitmap based on the device scale. It should be based on the bitmap scale instead:
    Code:
    bc.Initialize(bmp.Width / ReduceScale / bmp.Scale, bmp.Height / ReduceScale / bmp.Scale)
    B4XBitmap.Scale is included in XUI v1.70 which will be released in a day or two.
     
    Last edited: May 14, 2018
  2. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    can you enhance the blur effect? which value do I have to increase for this?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    There are two parameters that you can modify to change the result: ReduceScale and count.
    Start with lowering ReduceScale if you want to get a more sharp bitmap.
     
    Alexander Stolte likes this.
  4. Erel

    Erel Administrator Staff Member Licensed User

    See 'update' message in first post.
     
    Alexander Stolte likes this.
  5. Star-Dust

    Star-Dust Expert Licensed User

    We are wait XUI v1.70 :p
     
    Erel likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice