iOS Question Convert image to black and white

angel_

Well-Known Member
Licensed User
Longtime User
I use this @klaus code with @Erel grayscale (B4XBitmapEffects), in B4A it looks pretty good but in B4i the definition is not very good, Any ideas to improve it?

Klaus code:

B4X:
Public Sub BlackAndWhite (Image As B4XBitmap, Threshold As Int) As B4XBitmap
    Private x, y, Mean, BWCol As Int
    Private col0, col1 As ARGBColor
    Private bmcImage, bmcResult As BitmapCreator
 
    bmcImage.Initialize(Image.Width, Image.Height)
    bmcImage.CopyPixelsFromBitmap(Image)
    bmcResult.Initialize(Image.Width, Image.Height)
 
    For y = 0 To Image.Height -1
        For x = 0 To Image.Width -1
            bmcImage.GetARGB(x, y, col0)
            Mean = (col0.r + col0.g + col0.b) / 3
            If Mean > Threshold Then
                BWCol = 255
            Else If Mean > 0.75 * Threshold Then
                BWCol = col0.r * 0.21 + col0.g * 0.72 + 0.07 * col0.b
            Else
                BWCol = 0
            End If
            col1.r = BWCol
            col1.g = BWCol
            col1.b = BWCol
            col1.a = 255
            bmcResult.SetARGB(x, y, col1)
        Next
    Next
   
    Return bmcResult.Bitmap
End Sub
 

Attachments

  • Project.zip
    16.9 KB · Views: 117

Erel

B4X founder
Staff member
Licensed User
Longtime User
The image file is missing.

Try this:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private img As B4XImageView
    Private effects As BitmapCreatorEffects
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    effects.Initialize
    effects.ScaleDownImages = False
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    Dim bmp As B4XBitmap = xui.LoadBitmapResize(File.DirAssets, "imagen.png", img.mBase.Width, img.mBase.Height, True)
    
    img.Clear
    img.Bitmap = BlackAndWhite(bmp, 200)
End Sub

'calculates the mean scale value to generate a mean scaled image
Public Sub BlackAndWhite (Image As B4XBitmap, Threshold As Int) As B4XBitmap
    Private x, y, Mean, BWCol As Int
    Private col0, col1 As ARGBColor
    Private bmcImage, bmcResult As BitmapCreator
       bmcImage = effects.CreateBC(Image)
    bmcResult.Initialize(bmcImage.mWidth, bmcImage.mHeight)
    For y = 0 To bmcImage.mHeight - 1
        For x = 0 To bmcImage.mWidth - 1
            bmcImage.GetARGB(x, y, col0)
'            col1.a = col0.a
            Mean = (col0.r + col0.g + col0.b) / 3
            If Mean > Threshold Then
                BWCol = 255
            Else
                BWCol = 0
            End If
            col1.a = 255
            col1.r = BWCol
            col1.g = BWCol
            col1.b = BWCol
            bmcResult.SetARGB(x, y, col1)
        Next
    Next
    Return bmcResult.Bitmap
End Sub
Depends on B4XBitmapEffects
 
Upvote 0

angel_

Well-Known Member
Licensed User
Longtime User
Thank you very much, I tried it but the quality is not good, there is no sharpness at the edges it is as if it had less resolution
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…