Android Code Snippet [B4X] [BitmapCreator] Blur Image

Erel

Administrator
Staff member
Licensed User
Returns a blurred image:



B4X:
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(3) As 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 + 1) Mod 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 + 1) Mod 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:

B4X:
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:
B4X:
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:

Star-Dust

Expert
Licensed User
Returns a blurred image:
B4X:
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(3) As 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 + 1) Mod 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 + 1) Mod 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:

B4X:
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:
B4X:
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.
We are wait XUI v1.70 :p
 
Top