iOS Question Needed: Code Snippet to Pixelate Bitmaps

Mashiane

Expert
Licensed User
Hi

Here is source code to pixelate bitmaps that I got here in B4a.

B4X:
Sub PixelateBitMap(B0 As Bitmap, factor As Float) As Bitmap
  'Dim factor As Float=8    'Change it to get different pixellations
  Dim new_w As Int = B0.Width/factor
  Dim new_h As Int = B0.Height/factor
  If new_w>=1 And new_h>=1 Then
    'Draw the original onto a smaller one
    Dim B_small As Bitmap
    B_small.InitializeMutable(new_w,new_h)
    Dim cv As Canvas
    cv.Initialize2(B_small)
    Dim DestRect As Rect
    DestRect.Initialize(0,0,B_small.Width,B_small.Height)
    cv.DrawBitmap(B0,Null,DestRect)
   
    'Create a new bitmap wth the same dimensions as the original
    Dim B_pixelated As Bitmap
    B_pixelated.initializemutable(B0.width,B0.Height)
    cv.Initialize2(B_pixelated)
    Dim DestRect As Rect
    DestRect.Initialize(0,0,B_pixelated.Width,B_pixelated.Height)
    cv.DrawBitmap(B_small,Null,DestRect)
    Return B_pixelated
  Else    
   Return B0
  End If
End Sub

I kindly need similar for my b4i app.

Can someone please help.

Thanks in advance
 

strat

Active Member
Licensed User
This works for me.

B4X:
Sub PixelateBitMap(B0 As Bitmap, factor As Float) As Bitmap
  'Dim factor As Float=8    'Change it to get different pixellations
  Dim ww As Int = B0.Width/factor
  Dim hh As Int = B0.Height/factor
  'Msgbox("1","2")
  If ww>=1 And hh>=1 Then
      Private iv2 As ImageView
      iv2.Initialize("iv2")
    Page1.RootPanel.AddView(iv2,0,0,iv.Width,iv.Height)
    'Draw the original onto a smaller one
    iv2.Width=ww
    iv2.Height=hh
    Dim cv As Canvas
    cv.Initialize(iv2)
    Dim DestRect As Rect
    DestRect.Initialize(0,0,ww,hh)
    cv.DrawBitmap(B0,DestRect)
    iv2.Bitmap=cv.CreateBitmap
    cv.Refresh
    'Create a new bitmap wth the same dimensions as the original
    'iv.Width=B0.Width
    'iv.Height=Button1.Height
    cv.Initialize(iv)
    Dim DestRect As Rect
    DestRect.Initialize(0,0,iv.Width,iv.Height)
    cv.DrawBitmap(iv2.Bitmap,DestRect)
    iv.Bitmap=cv.CreateBitmap
    cv.Refresh
    iv2.RemoveViewFromParent
    Return iv.Bitmap
  Else   
   Return B0
  End If
End Sub

Sub Button1_Click
   iv.Bitmap=PixelateBitMap(ImageView1.Bitmap,15)
End Sub
 
Upvote 0

Mashiane

Expert
Licensed User
This works for me.

B4X:
Sub PixelateBitMap(B0 As Bitmap, factor As Float) As Bitmap
  'Dim factor As Float=8    'Change it to get different pixellations
  Dim ww As Int = B0.Width/factor
  Dim hh As Int = B0.Height/factor
  'Msgbox("1","2")
  If ww>=1 And hh>=1 Then
      Private iv2 As ImageView
      iv2.Initialize("iv2")
    Page1.RootPanel.AddView(iv2,0,0,iv.Width,iv.Height)
    'Draw the original onto a smaller one
    iv2.Width=ww
    iv2.Height=hh
    Dim cv As Canvas
    cv.Initialize(iv2)
    Dim DestRect As Rect
    DestRect.Initialize(0,0,ww,hh)
    cv.DrawBitmap(B0,DestRect)
    iv2.Bitmap=cv.CreateBitmap
    cv.Refresh
    'Create a new bitmap wth the same dimensions as the original
    'iv.Width=B0.Width
    'iv.Height=Button1.Height
    cv.Initialize(iv)
    Dim DestRect As Rect
    DestRect.Initialize(0,0,iv.Width,iv.Height)
    cv.DrawBitmap(iv2.Bitmap,DestRect)
    iv.Bitmap=cv.CreateBitmap
    cv.Refresh
    iv2.RemoveViewFromParent
    Return iv.Bitmap
  Else  
   Return B0
  End If
End Sub

Sub Button1_Click
   iv.Bitmap=PixelateBitMap(ImageView1.Bitmap,15)
End Sub
Thanks a lot, will try it out...
 
Upvote 0

Mashiane

Expert
Licensed User
This works for me.

B4X:
Sub PixelateBitMap(B0 As Bitmap, factor As Float) As Bitmap
  'Dim factor As Float=8    'Change it to get different pixellations
  Dim ww As Int = B0.Width/factor
  Dim hh As Int = B0.Height/factor
  'Msgbox("1","2")
  If ww>=1 And hh>=1 Then
      Private iv2 As ImageView
      iv2.Initialize("iv2")
    Page1.RootPanel.AddView(iv2,0,0,iv.Width,iv.Height)
    'Draw the original onto a smaller one
    iv2.Width=ww
    iv2.Height=hh
    Dim cv As Canvas
    cv.Initialize(iv2)
    Dim DestRect As Rect
    DestRect.Initialize(0,0,ww,hh)
    cv.DrawBitmap(B0,DestRect)
    iv2.Bitmap=cv.CreateBitmap
    cv.Refresh
    'Create a new bitmap wth the same dimensions as the original
    'iv.Width=B0.Width
    'iv.Height=Button1.Height
    cv.Initialize(iv)
    Dim DestRect As Rect
    DestRect.Initialize(0,0,iv.Width,iv.Height)
    cv.DrawBitmap(iv2.Bitmap,DestRect)
    iv.Bitmap=cv.CreateBitmap
    cv.Refresh
    iv2.RemoveViewFromParent
    Return iv.Bitmap
  Else  
   Return B0
  End If
End Sub

Sub Button1_Click
   iv.Bitmap=PixelateBitMap(ImageView1.Bitmap,15)
End Sub
Hi

Just a little help again, this line...

B4X:
Page1.RootPanel.AddView(iv2,0,0,iv.Width,iv.Height)

you have passed a variable called iv. Is that an imageview in your layout and what size should be it? does anysize work.
 
Upvote 0

Mashiane

Expert
Licensed User
This works for me.

B4X:
Sub PixelateBitMap(B0 As Bitmap, factor As Float) As Bitmap
  'Dim factor As Float=8    'Change it to get different pixellations
  Dim ww As Int = B0.Width/factor
  Dim hh As Int = B0.Height/factor
  'Msgbox("1","2")
  If ww>=1 And hh>=1 Then
      Private iv2 As ImageView
      iv2.Initialize("iv2")
    Page1.RootPanel.AddView(iv2,0,0,iv.Width,iv.Height)
    'Draw the original onto a smaller one
    iv2.Width=ww
    iv2.Height=hh
    Dim cv As Canvas
    cv.Initialize(iv2)
    Dim DestRect As Rect
    DestRect.Initialize(0,0,ww,hh)
    cv.DrawBitmap(B0,DestRect)
    iv2.Bitmap=cv.CreateBitmap
    cv.Refresh
    'Create a new bitmap wth the same dimensions as the original
    'iv.Width=B0.Width
    'iv.Height=Button1.Height
    cv.Initialize(iv)
    Dim DestRect As Rect
    DestRect.Initialize(0,0,iv.Width,iv.Height)
    cv.DrawBitmap(iv2.Bitmap,DestRect)
    iv.Bitmap=cv.CreateBitmap
    cv.Refresh
    iv2.RemoveViewFromParent
    Return iv.Bitmap
  Else  
   Return B0
  End If
End Sub

Sub Button1_Click
   iv.Bitmap=PixelateBitMap(ImageView1.Bitmap,15)
End Sub
Hi strat

I have tweaked your code to avoid the usage of the page view... this is also working perfectly. However my B4A app used 30 as a factor and my b4i will have to use 60 to pixelate properly. Thanks a lot for your help, very much appreciated.

B4X:
Sub PixelateBitMap(B0 As Bitmap, factor As Float) As Bitmap
  'Dim factor As Float=8    'Change it to get different pixellations
  Dim ww As Int = B0.Width/factor
  Dim hh As Int = B0.Height/factor
  If ww>=1 And hh>=1 Then
      Private iv2 As ImageView
      Private iv As ImageView
      iv2.Initialize("iv2")
      iv2.Width=ww
      iv2.Height=hh
      iv.Initialize("iv")
      iv.Width = B0.Width
      iv.Height = B0.Height
      'Draw the original onto a smaller one
      Dim cv As Canvas
      cv.Initialize(iv2)
      Dim DestRect As Rect
      DestRect.Initialize(0,0,ww,hh)
      cv.DrawBitmap(B0,DestRect)
      iv2.Bitmap=cv.CreateBitmap
      cv.Refresh
      'Create a new bitmap wth the same dimensions as the original
      cv.Initialize(iv)
      Dim DestRect As Rect
      DestRect.Initialize(0,0,iv.Width,iv.Height)
      cv.DrawBitmap(iv2.Bitmap,DestRect)
      iv.Bitmap=cv.CreateBitmap
      cv.Refresh
      Return iv.Bitmap
  Else  
      Return B0
  End If
End Sub
 
Upvote 0

strat

Active Member
Licensed User
iv is imageview that I created runtime. It may be same as at your b4A sample.
iv will work at any size.

Don't forget to load a picture to Imageview1 before run. I made it in the designer.

B4X:
Sub Process_Globals
    Private iv As ImageView
End Sub

Private Sub Application_Start (Nav As NavigationController)
    ..
    ..
    ..

    iv.Initialize("iv")
    Page1.RootPanel.AddView(iv,0,0,250,250)
   
End Sub
 
Upvote 0

Mashiane

Expert
Licensed User
iv is imageview that I created runtime. It may be same as at your b4A sample.
iv will work at any size.

Don't forget to load a picture to Imageview1 before run. I made it in the designer.

B4X:
Sub Process_Globals
    Private iv As ImageView
End Sub

Private Sub Application_Start (Nav As NavigationController)
    ..
    ..
    ..

    iv.Initialize("iv")
    Page1.RootPanel.AddView(iv,0,0,250,250)
  
End Sub
Thanks a lot, see post #5 above. Already sorted. This is perfect!
 
Upvote 0
Top