Android Question ZbarBarcodeScanner save image

Hello everybody,

I'm trying this lib to read some QR CODE but I also need to store the photo used by the lib to process the result.

Is it possibile?
Thanks

-----Example----
B4X:
Sub zb1_scan_result(scantext As String, scanformat As String)
    zb1.SaveToFile( File.DirDefaultExternal, "qrcode.jpg")  ' <<<<<<<<<<<<< something like this

    zb1.stopScanner
    zb1.toggleFlash
    If scanformat ="QRCODE" Then
        Msgbox(scantext, "QRCODE")
    End If

    Log("B4A scantext = " & scantext)
    Log("B4A scanformat = " & scanformat)

End Sub
 

Johan Schoeman

Expert
Licensed User
Longtime User
Probably two options:
1. Take a screenshot and then crop and save the Bitmap (check the forum for how to do it)
2. I add code to the library to return the preview frame (bitmap) that has been used for decoding the QR code - you will then have to add the code to save the Bitmap.
 
Upvote 0
Thanks for the reply .
About option 1 I've tried all the examples found in the forum but I always get a black image.
The best result I get is doing as described in this thread (see attached file).
The capture runs while the preview is visible.
B4X:
Sub zb1_scan_result(scantext As String, scanformat As String)
    
    PanelCapture(zb1)
    If zb1.Visible Then
        zb1.Visible = False
        zb1.toggleFlash
    End If
    
    zb1.stopScanner
    zb1.toggleFlash
    '
    '
    'EditText1.Text = scantext
    Log("B4A scantext = " & scantext)
    Log("B4A scanformat = " & scanformat)
End Sub
B4X:
Sub PanelCapture(pnl As Panel)

    Dim Obj1, Obj2 As Reflector
    Dim bmp As Bitmap
    Dim c As Canvas
    Dim Testimage As Panel
    
    Testimage.Initialize("")
    Testimage.SetLayout(0,0,pnl.Width, pnl.Height)
    
    Obj1.Target = Obj1.GetActivityBA
    Obj1.Target = Obj1.GetField("vg")
    bmp.InitializeMutable(pnl.left + pnl.Width, pnl.Top + pnl.Height)


    c.Initialize2(bmp)
    Dim args(1) As Object
    Dim types(1) As String
    Obj2.Target = c
    Obj2.Target = Obj2.GetField("canvas")
    args(0) = Obj2.Target
    types(0) = "android.graphics.Canvas"
    Obj1.RunMethod4("draw", args, types)

    '--------------------------

    'draw from image to canavas
    Dim canvas1 As Canvas
    canvas1.Initialize(Testimage)
    Dim scrt As Rect
    scrt.Initialize(pnl.left, pnl.top,  pnl.Width, pnl.Height)
    'scrt.Initialize(0, 0,  680, 998)

    Dim rectPanel1 As Rect
    rectPanel1.Initialize(pnl.left, pnl.top,  pnl.Width, pnl.Height)
    canvas1.DrawBitmap(bmp, scrt , rectPanel1)
    Testimage.Invalidate

    'here writing canvas to file

    Dim Out As OutputStream
    Dim MyFileName As String

    Log(DateTime.Now)
    MyFileName = DateTime.Now & ".jpg"
    Out = File.OpenOutput(File.DirDefaultExternal , MyFileName, False)
    canvas1.Bitmap.WriteToStream(Out, 100, "JPEG")
    Out.Close
    Log(MyFileName)
End Sub
Option 2 will be indeed a better and cleaner solution, but also a hint to get canvas capture would be too.
Thanks
Antonello
 

Attachments

  • 1607445795949.jpg
    1607445795949.jpg
    16.8 KB · Views: 230
Upvote 0
Update: searching on other android forums I've learned that the camera view is rendered using hardware acceleration and is not simple, if not possible, to get a screenshot of the preview.
So I choose for a workaround: after reading the QR I open the camera and shoot the photo automatically. It is not the perfect solution, but it works.

If in future the ZBar lib will be able to save the image it will be easy to change my code.
 
Upvote 0
Top