Android Question SOLVED [B4X] Barcode Reader - Captured Images are stretched (Full screen)

Mike1970

Well-Known Member
Licensed User
Longtime User
Hello

I was changing the barcode reader I usually use.
with this one

Why the images are stretched?

Screenshot_20201204_122341_biquadro.tankyou.jpg


The image taken from Google Images (to have a reliable reference) its supposed to be a square, and actually looks like a rectangle

thanks
 
Last edited:

Brian Dean

Well-Known Member
Licensed User
Longtime User
If a post reaches the second page in the forum without a single reply it usually means that nobody can understand the question. In this case I think that the problem lies here ...
Why the images are stretched?
There are so many possible answers. Nobody is going to waste their time making guesses. You need to explain why you think the image should be square - show some code, for example.
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
If a post reaches the second page in the forum without a single reply it usually means that nobody can understand the question. In this case I think that the problem lies here ...

There are so many possible answers. Nobody is going to waste their time making guesses. You need to explain why you think the image should be square - show some code, for example.
Ahm... I don't quite understand what do you mean.

it is simple, no code needed. I copy/pasted the example I linked in the first post.

and I'm showing that by framing a square on my desktop screen, on the phone it look like a rectangle.


P.S. it should be a square simply because I searched on Google Images the word: "square",and by framing it with the barcode reader, is evident that the images captured from the phone are stretched using the bar-code reader

In the Erel's example looks stretched too, look at the qrcode, it does not seems to be a square:
1595341203504-png.97444
 
Last edited:
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
show some code, for example.

Ok, this is the code i used to show the reader

B4X:
'------------------------------------------- LETTORE BARCODE/QRCODE -----------------------------------
Private Sub CreateDetector (Codes As List)
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim builder As JavaObject
    builder.InitializeNewInstance("com/google/android/gms/vision/barcode/BarcodeDetector.Builder".Replace("/", "."), Array(ctxt))
    Dim barcodeClass As String = "com/google/android/gms/vision/barcode/Barcode".Replace("/", ".")
    Dim barcodeStatic As JavaObject
    barcodeStatic.InitializeStatic(barcodeClass)
    Dim format As Int
    For Each formatName As String In Codes
        format = Bit.Or(format, barcodeStatic.GetField(formatName))
    Next
    builder.RunMethod("setBarcodeFormats", Array(format))
    detector = builder.RunMethod("build", Null)
    Dim operational As Boolean = detector.RunMethod("isOperational", Null)
    If operational = False Then
        ToastMessageShow("Failed to create detector", True)
    End If
End Sub

Private Sub StopCamera
    Capturing = False

    If camEx.IsInitialized Then
        camEx.Release
    End If
End Sub

Private Sub StartCamera
    rp.CheckAndRequest(rp.PERMISSION_CAMERA)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow("No permission!", False)
        Return
    End If
    
    StartCameraShared
    camEx.Initialize(qrcodeReader, False, Me, "Camera1")
    Wait For Camera1_Ready (Success As Boolean)
    If Success Then
        camEx.SetContinuousAutoFocus
        camEx.CommitParameters
        camEx.StartPreview
        'Log(camEx.GetSupportedFlashModes)
        
        Sleep(200)
        qrcodeReader.Visible = True
    Else
        ToastMessageShow("Error opening camera", False)
        StopCamera
    End If
End Sub

Private Sub StartCameraShared
    qrcodeReader.Visible = True
    Capturing = True
End Sub

Private Sub Camera1_Preview (data() As Byte)
    If DateTime.Now > LastPreview + IntervalBetweenPreviewsMs Then
        'Dim n As Long = DateTime.Now
        Dim frameBuilder As JavaObject
        Dim bb As JavaObject
        bb = bb.InitializeStatic("java.nio.ByteBuffer").RunMethod("wrap", Array(data))
        frameBuilder.InitializeNewInstance("com/google/android/gms/vision/Frame.Builder".Replace("/", "."), Null)
        Dim cs As CameraSize = camEx.GetPreviewSize
        frameBuilder.RunMethod("setImageData", Array(bb, cs.Width, cs.Height,  842094169))
        Dim frame As JavaObject = frameBuilder.RunMethod("build", Null)
        Dim SparseArray As JavaObject = detector.RunMethod("detect", Array(frame))
        LastPreview = DateTime.Now
        Dim Matches As Int = SparseArray.RunMethod("size", Null)
        If Matches > 0 Then
            StopCamera
            
            Dim Barcode As JavaObject = SparseArray.RunMethod("valueAt", Array(0))
            Dim raw As String = Barcode.GetField("rawValue")
            QrCodeOK(raw)
        End If
    End If
End Sub
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I must say that I originally thought that you were worried that the imageview did not look square on your 'phone screen, not that the image inside it was not square.

It looks to me that the image in Erel's post is "stretched" because the phone (camera) is not perfectly parallel to the code sample. Is this a problem? It is not enough to stop a QR-code being interpreted by a reader.

Let's see if someone else can understand the problem better than I.
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
Ok by searching on the forum for something else I undestand what was the problem (in the Erel's example too), thanks to this thread.

The problem is that the Panel size used for the preview is not considered.
You have to:
  1. Size the panel by youself to keep the correct aspect ratio.
    So in the "StartCamera" function i added the the simple calculation for the ratio
    Line 16-20 (This function is the same of the original example except for the Lines 16-20, 22-23)

    B4X:
    Private Sub StartCamera
        rp.CheckAndRequest(rp.PERMISSION_CAMERA)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result = False Then
            ToastMessageShow("No permission!", False)
            Return
        End If
       
        StartCameraShared
        camEx.Initialize(qrcodeReader, False, Me, "Camera1")
        Wait For Camera1_Ready (Success As Boolean)
        If Success Then
            camEx.SetContinuousAutoFocus
            camEx.CommitParameters
           
            camEx.StartPreview
            Dim cs As CameraSize = camEx.GetPreviewSize
            Dim ratio As Float = cs.Width/cs.Height
            qrcodeReader.Height = 100%y
            qrcodeReader.Width = qrcodeReader.Height/ratio
            'Log(camEx.GetSupportedFlashModes)
           
            Sleep(200)
            ComparsaLettoreQR
        Else
            ToastMessageShow("Error opening camera", False)
            StopCamera
        End If
    End Sub


  2. Then when it's time to make visibile the preview panel (that i called "qrcodeReader") change the .Left value to center it properly in the screen
    Line 7, there are many other instruction in this function becuase i want to animated a bit the things.

    B4X:
    Sub ComparsaLettoreQR
        btnNewOp.Enabled = False 'per evitare glitch grafico
        Sleep(10)
       
        pnlScanner.Top = 100%y
        qrcodeReader.Top = 100%y
        qrcodeReader.Left = 50%x-qrcodeReader.Width/2
       
        pnlScanner.Visible = True
        qrcodeReader.Visible = True
       
        pnlScanner.SetLayoutAnimated(100, pnlScanner.Left, 0, pnlScanner.Width, pnlScanner.Height)
        qrcodeReader.SetLayoutAnimated(100, qrcodeReader.Left, 0, qrcodeReader.Width, qrcodeReader.Height)
    
        Sleep(110)
    End Sub
 
Last edited:
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
I must say that I originally thought that you were worried that the imageview did not look square on your 'phone screen, not that the image inside it was not square.

It looks to me that the image in Erel's post is "stretched" because the phone (camera) is not perfectly parallel to the code sample. Is this a problem? It is not enough to stop a QR-code being interpreted by a reader.

Let's see if someone else can understand the problem better than I.

It does not compromises the correct operation, it was a pure aesthetic problem.
However i found a solution in post #6.

Thanks for your time :D
 
Upvote 0
Top