Android Question Google Vision Barcode vs Camera2 module

Cainsoft

Member
Licensed User
Longtime User
Hi,
My application is based on camera2 tutorial: https://www.b4x.com/android/forum/threads/camera2-still-images-and-videos.83920/
I would like to use the Google Vision Barcode: https://www.b4x.com/android/forum/threads/barcode-reader-based-on-google-play-services-vision.89705/

I looked at this solution, but the "Camera_Preview" call that exists in the Camera module (CameraExClass):

B4X:
Sub Camera_Preview (Data() As Byte)
    If SubExists(target, event & "_preview") Then
        CallSub2(target, event & "_preview", Data)
    End If
End Sub

was not found in Camera2.

Is there any way I can get the "preview" data() in Camera2?

Thank you,
Attila
 

drgottjr

Expert
Licensed User
Longtime User
getpreviewbitmap is, more or less, the corresponding camera2
version. you call it with a timer. but you'll have an issue with the
barcode reader: it wants the preview's original raw byte array, not
a bitmap. i don't know how comfortable you are modifying that
particular sub with its mix of b4a and javaobject code.

(for example,
where it says:
B4X:
frameBuilder.RunMethod("setImageData", Array(bb, cs.Width, cs.Height,  ...
you could change that to:
B4X:
frameBuilder.runmethod("setBitmap", array( bitmap ))
to handle the bitmap from getpreviewbitmap. of course, the
name of the sub would have to be changed, as well as a couple other
minor changes. i don't know what you're up to handling.)


if your app is a barcode reading app, you should be using the mobile
vision version with the old camera api. if your app uses camera2 and
you suddenly got the brilliant idea to do barcodes, you have a problem.

either you decode barcodes from a static image or you add the barcode
reader as a second activity. there is no reason why you can't use
camera2 in a main activity, release it and start your barcode activity.
if the main purpose of your app is to read barcodes, then you need to
return to the drawingboard. if your app only needs to read a barcode
occasionally, then the 2 options mentioned above should do the trick.
 
Last edited:
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
 
Upvote 0

Cainsoft

Member
Licensed User
Longtime User
getpreviewbitmap is, more or less, the corresponding camera2
version. you call it with a timer. but you'll have an issue with the
barcode reader: it wants the preview's original raw byte array, not
a bitmap. i don't know how comfortable you are modifying that
particular sub with its mix of b4a and javaobject code.

(for example,
where it says:
B4X:
frameBuilder.RunMethod("setImageData", Array(bb, cs.Width, cs.Height,  ...
you could change that to:
B4X:
frameBuilder.runmethod("setBitmap", array( bitmap ))
to handle the bitmap from getpreviewbitmap. of course, the
name of the sub would have to be changed, as well as a couple other
minor changes. i don't know what you're up to handling.)


if your app is a barcode reading app, you should be using the mobile
vision version with the old camera api. if your app uses camera2 and
you suddenly got the brilliant idea to do barcodes, you have a problem.

either you decode barcodes from a static image or you add the barcode
reader as a second activity. there is no reason why you can't use
camera2 in a main activity, release it and start your barcode activity.
if the main purpose of your app is to read barcodes, then you need to
return to the drawingboard. if your app only needs to read a barcode
occasionally, then the 2 options mentioned above should do the trick.

The original purpose of the app is to photograph and send documents. The barcode/qr code request came up as an afterthought, and I have to deal with several options, scanning the barcode/qrcode on the document, and reading other "separate" barcode before sending.
Thanks for the idea, I will try it.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
if the original purpose of the app was to photograph documents, there is absolutely no reason why you cannot photograph a barcode and decode it (without needing to launch a separate activity). it's very easy. "live" scanning is convenient and techy, but you're already set up to capture images. you simply call a sub that decodes a barcode in such an image. no shame using a static image, unless you feel diminished because your app doesn't decode live previews. i use both methods, and i even use static images of a live scan for further evaluation in the event the live scan fails (which happens often for barcodes not generated in utf8).
 
Upvote 0

Cainsoft

Member
Licensed User
Longtime User
Of course, there's no shame in reading the barcode from a picture, but I needed a solution through the preview. Because it looks good in the presentation. :)
Your suggestion works, thanks.
 
Upvote 0
Top