Android Question Problem on bitmap OutputStream

Richard Goh

Active Member
Licensed User
Longtime User
I have a method to output the image stream to printer. The method managed to output part of the image but hit into error, as shown below, when output the last part of the image (as in bold). I don't understand how the outputstream lenght works? I checked the bitmap image buffer always [999]. In this case, how can i print the last part of the image? Thanks in advance.

bmp width = 180, bmp height=120
bmpRow =4

row (i) = 0, width = 23, line =30
Offset = 0, Lenght = 689

row (i) = 1, width = 23, line =30
Offset = 690, Lenght = 1379

row (i) = 2, width = 23, line =30
Offset = 1380, Lenght = 2069

Error occurred on line: 199 (Main)
java.lang.IndexOutOfBoundsException: invalid offset or length
at android.bluetooth.BluetoothOutputStream.write(BluetoothOutputStream.java:83)
at anywheresoftware.b4a.objects.streams.File$OutputStreamWrapper.WriteBytes(File.java:455)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:680)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:308)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at b4a.example.main.afterFirstLayout(main.java:100)
at b4a.example.main.access$100(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:78)


B4X:
Sub print(newBm As Bitmap)

    OS=Serial1.OutputStream

    Dim xL As Byte = ((newBm.Width + 7) / 8 mod 256)
    Dim xH As Byte = ((newBm.Width + 7) / 8 / 256)
    Dim line As Byte = 10
    Dim bmpRow As Int = (newBm.Height + line - 1) / line
    Dim width As Int = (newBm.Width + 7) / 8
    Dim pixels() As Byte
    Dim bcode() As Byte = Array As Byte(29, 118, 48, 0)
    Dim i As Int
    Log("bmp width = " & newBm.width & ", bmp height=" & newBm.Height)
    Log("bmpRow =" & bmpRow) 
    Dim jo As JavaObject
    pixels = jo.InitializeStatic("driver.BitmapConvertUtil").RunMethod("convert", Array(newBm))

    Dim offSet, osLenght As Int

    For i = 0 To bmpRow
        Dim yL As Byte
        Dim yH As Byte
        Dim buf() As Byte
       
        If i <> bmpRow - 1 Then
            yL = line
            yH = 0
            buf = Array As Byte(bcode(0), bcode(1), bcode(2), bcode(3), xL, xH, yL, yH)
            OS.WriteBytes(buf,0,buf.Length-1)

            offSet = (width * i * line)
            osLenght = (width * line * (i + 1) - 1)   

            Log( "row (i) = " & i & ", width = " & width & ", line =" & line) 
            Log( "Offset = " & offSet & ", Lenght = " & osLenght)                      
                                                          
            OS.WriteBytes(pixels, offSet, osLenght)
            buf = Array As Byte(10)
            OS.WriteBytes(buf,0,buf.Length-1)
        Else
            yL = (newBm.Width mod line)
            yH = 0
            buf = Array As Byte(bcode(0), bcode(1), bcode(2), bcode(3), xL, xH, yL, yH)
            OS.WriteBytes(buf,0,buf.Length-1)       
            offSet = (width * i * line)
            osLenght = pixels.length - 1      
            OS.WriteBytes(pixels, offSet, osLenght)
            buf = Array As Byte(10)
            OS.WriteBytes(buf,0,buf.Length-1)           
        End If
    Next

    Return
End Sub
 

strat

Active Member
Licensed User
Longtime User
Problem may be at the line:
B4X:
For i = 0 To bmpRow

Replace it by
B4X:
For i = 0 To bmpRow-1
 
Upvote 0
Top