Android Question How to convert bitmap to raw byte[] ?

Marcob

Member
Licensed User
Longtime User
Hi,

I'm aware that a bitmap can be converted to a compressed PNG/JPEG data array for example by using bitmap.WriteToStream(). Does a way for getting just an uncompressed data array from a bitmap exist?
 

Star-Dust

Expert
Licensed User
Longtime User
B4X:
Public Sub BitmapToByte (SourceBitmap As Bitmap) As Byte()
    Dim mBuffer() As Byte
    Dim jo As JavaObject = SourceBitmap
    Dim format As Object = jo.RunMethod("getConfig", Null)
    If "ARGB_8888" <> format Then
        Log("Unsupported format: " & format)
        Return
    End If
    Dim bf As JavaObject
    bf = bf.InitializeStatic("java.nio.ByteBuffer").RunMethodJO("wrap", Array(mBuffer))
    jo.RunMethod("copyPixelsToBuffer", Array(bf))
    Return mBuffer
End Sub
 
Upvote 0
Top