B4J Question Read image pixel-by-pixel without JavaFX?

techknight

Well-Known Member
Licensed User
Longtime User
I have a program that I use to read in a gif image, and it converts it into an indexed-color binary byte array, pixel by pixel at a time. Like, for use in a microcontroller, etc.

But it relies on JavaFx since it uses the image library.

Is there a way to do this without JavaFX? I am not displaying it or anything. Just opening it and reading it one pixel at a time, the ARGB values and converting them into binary indexes (against a palette).

Any ideas? Thanks.
 

Daestrum

Expert
Licensed User
Longtime User
Have a look at awt graphics, they work even in a non-ui program.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
I was doing searching but not really coming up with anything on that. I found 2 threads, one of them talking about binary, but its some weird JPEG binary which wont work for what I need.

Here is some code of what I am doing in Windows with a GUI App:
B4X:
    'Load 20x36 Proportional Font into Pixel Memory.
    Dim Descriptor(96) As FontDescriptor
    Dim RollingDescriptorCounter As Int = 0
    Dim GlyphTable As String
    Dim i2 As Int
    For i2 = 0 To 95 Step 1
        Dim img As Image = fx.LoadImage(File.DirApp, "font03\" & i2 & ".gif")
        Dim buffer() As Byte = GetPixels(img)
        Dim width As Int = img.Width
        Dim height As Int = img.Height
        Descriptor(i2).FontWidth = width
        Descriptor(i2).FontPointer = RollingDescriptorCounter
        'RollingDescriptorCounter = RollingDescriptorCounter + (width * height)
        
        For y = 0 To height - 1
            For x = 0 To width - 1
                Dim i As Int = y * width * 4 + x * 4
                Dim b As Int = Bit.And(0xFF, buffer(i))
                Dim g As Int = Bit.And(0xFF, buffer(i + 1))
                Dim r As Int = Bit.And(0xFF, buffer(i + 2))
                Dim a As Int = Bit.And(0xFF, buffer(i + 3))
                If A = 255 And R = 127 And g = 127 And b = 127 Then 'This is a Gray color (We key this out with whatever is in the background, So save as a 0)
                    'PixelArray(x, y) = 0
                    GlyphTable = GlyphTable & "00"
                else if A = 255 And R = 0x0a And b = 0x0a And G = 0x0a Then 'We have a black color. So we make this black, or 1.
                    'PixelArray(x, y) = 1
                    GlyphTable = GlyphTable & "01"
                Else if A = 255 And R = 255 And G = 255 And B = 255 Then 'We have a white color. So this is the typeface color. or, 2.
                    'PixelArray(x, y) = 2
                    GlyphTable = GlyphTable & "02"
                Else
                    'Log("You're gonna have a problem with this file")
                    'Log(R & " " & G & " " & B)
                    GlyphTable = GlyphTable & "00"
                End If
                RollingDescriptorCounter = RollingDescriptorCounter + 1
            Next
        Next
    Next

I need to figure out how to do this in a non-UI console app.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Looks like the way to go possibly.

Is there a wrapper to use this thing?

my Java knowledge is next to 0.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
No there is no wrapper, Try this
B4X:
Private Sub ManipulatePixels
 
    Dim Img As JavaObject
 
 
    Dim ImageIO As JavaObject
    ImageIO.InitializeStatic("javax.imageio.ImageIO")
 

    Dim jFile As JavaObject
    jFile.InitializeNewInstance("java.io.File",Array("FileNameHere"))
    Img = ImageIO.RunMethod("read",Array(jFile))
   
    Dim Width As Int = Img.RunMethod("getWidth",Null)
    Dim Height As Int = Img.RunMethod("getHeight",Null)
   
    Log($"Dimensions - ${Width} x ${Height}"$)

    'Pixel Value
    Dim P As Int = Img.RunMethod("getRGB",Array(10,10))
    Dim A As Int = Bit.And(Bit.ShiftRight(P,24),0Xff)
    Dim R As Int = Bit.And(Bit.ShiftRight(P,16),0Xff)
    Dim G As Int = Bit.And(Bit.ShiftRight(P,8),0Xff)
    Dim B As Int = Bit.And(P,0Xff)
 
    Log(P)
    Log(A)
    Log(R)
    Log(G)
    Log(B)
 
    'Nothing is done with this info
 
 
    'New Values
    A = 0XFF
    R = 0xFF
    G = 0XFF
    B = 0XFF
 
    P = Bit.Or(Bit.ShiftLeft(A,24),Bit.ShiftLeft(R,16))
    P = Bit.Or(P,Bit.ShiftLeft(G,8))
    P = Bit.Or(P,B)
 
    Img.RunMethod("setRGB",Array(0,0,P))
 
    jFile.InitializeNewInstance("java.io.File",Array("D:\Output.jpg"))
    ImageIO.RunMethod("write",Array(Img,"jpg",jFile))
 
End Sub

Edit: Added get Width and Height as you will probably need those.
 
Last edited:
Upvote 0
Top