Barcode on screen

jayel

Active Member
Licensed User
Longtime User
Hello,

How can I put an barcode on the screen of my android tablet?

Greets,

John
 

bluedude

Well-Known Member
Licensed User
Longtime User
That is pretty easy, use the webview and Google Charts API for that. You don't need libraries for every piece of functionality.

All depens of which barcode, do you talk about EAN stuff?

There is a rate limit but it is very high.
 
Upvote 0

jayel

Active Member
Licensed User
Longtime User
Hello,

Yes, EAN stuff.
I was thinking about, decoding the code into barcode on php side and then download the image to my Android.

Do you have en example how to do it your way?

Thanks !

Greets,

John
 
Upvote 0

urikupfer

Member
Licensed User
Longtime User
litle class to draw qr code on button

you can change the url and draw barcode.

'Class module
Sub Class_Globals
Dim hc As HttpClient
Dim out As OutputStream
Dim req As HttpRequest
Dim url As String
Dim iv As Button
End Sub

'Initializes the object. You can add parameters to this method if needed.
Sub Initialize()
End Sub

Public Sub generateqrcode(input As String,iv1 As Button)
Dim su As StringUtils
iv=iv1
iv.SetBackgroundImage(Null)
url="https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl="
iv.SetBackgroundImage(Null)
hc.Initialize("hc")
input=su.EncodeUrl(input,"UTF8")
req.InitializeGet(url&input)
hc.Execute(req, 1)
End Sub

Private Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
out = File.OpenOutput(File.DirRootExternal, "1.dat", False)
Log("Getting file asynchronously")
Response.GetAsynchronously("res", out, True, TaskId)
End Sub

Private Sub res_StreamFinish (Success As Boolean, TaskId As Int)
Log("StreamFinish: " & Success)
If Success = False Then
Log(LastException.Message)
Else
Dim bm As Bitmap
bm.Initialize(File.DirRootExternal, "1.dat")
iv.SetBackgroundImage(bm)
End If
End Sub

Private Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
Log("Error connecting: " & Reason & " " & StatusCode)
If Response <> Null Then
Log(Response.GetString("UTF8"))
Response.Release
End If
End Sub
 
Upvote 0

jayel

Active Member
Licensed User
Longtime User
Hello,

I tried to convert some vba code into Basic4Android code but I have some troubles.

I want to convert this into below.

But I have errors.
Can somedy help me?


B4X:
Sub URL_Cod39(Testo As String, CheckDigit As Boolean, Titolo As Boolean, Larghezza As Long , Altezza As Long ) As String

Dim myreturn As String 
Dim StrBin As String
Dim Url1 As String 
Dim Url2 As String 
Dim Url3 As String
Dim Url4 As String

Url1 = "http://chart.apis.google.com/chart?"
Url2 = "chbh=1,0,0&chs="
Url3 = "&cht=bvg&chco=000000&chds=0,1&chd=s:"
If Titolo Then
    Url4 = "&chtt=" & Testo.ToUpperCase
    Altezza = Altezza + 30
End If

If VerificaTestoCod39(Testo) Then

Else
   Dim mystring As String 
    If CheckDigit Then 
      mystring = CheckDigit39(Testo, True) 
      Testo = Testo & mystring 
   End If 
   
   Dim mytesto As String 
   mytesto =("*" & Testo & "*")
    StrBin = Cod39Codifica(mytesto.touppercase)
    myreturn = Url1 & Url2 & Larghezza & "x" & Altezza
    myreturn = myreturn & Url3 & StrBin
    myreturn = myreturn & Url4
   Return myreturn
End If
End Sub
Sub VerificaTestoCod39(Testo As String) As Boolean
'-------------------------------------------------------
'Nel caso un carattere NON sia valido la funzione
'restituisce VERO
'-------------------------------------------------------

Return Regex.IsMatch("[^ %&*+-./$0-9A-Z]",Testo)

're.Pattern = "[^ %&*+-./$0-9A-Z]"
're.ignorecase = False
're.Global = True
'
'If Len(Testo) = 0 Then
'    VerificaTestoCod39 = True
'Else
'    VerificaTestoCod39 = re.test(Testo)
'End If

End Sub
Sub Cod39Codifica(Testo As String) As String
'-------------------------------------------------------
'Tabella di decodifica del codice 39
'
'Assegna ad ogni carattere del testo da decodificare
'una combinazione binaria di 9+1 caratteri restituendo
'una stringa di testo binaria
'
'Da chiamare dopo aver verificato la validità di Testo
'con la funzione VerificaTestoCod39
'-------------------------------------------------------

Dim arrB() As Byte
Dim arrC(90) As String
Dim L As Long
Dim StrTemp As String, NewStr As String, s As String
Dim b As Int

arrC(32) = "0110001000" 'Spazio
arrC(36) = "0101010000" '$
arrC(37) = "0001010100" '%
arrC(42) = "0100101000" '*
arrC(43) = "0100010100" '+
arrC(45) = "0100001010" '-
arrC(46) = "1100001000" '.
arrC(47) = "0101000100" '/
arrC(48) = "0001101000" '0
arrC(49) = "1001000010" '1
arrC(50) = "0011000010" '2
arrC(51) = "1011000000" '3
arrC(52) = "0001100010" '4
arrC(53) = "1001100000" '5
arrC(54) = "0011100000" '6
arrC(55) = "0001001010" '7
arrC(56) = "1001001000" '8
arrC(57) = "0011001000" '9
arrC(65) = "1000010010" 'A
arrC(66) = "0010010010" 'B
arrC(67) = "1010010000" 'C
arrC(68) = "0000110010" 'D
arrC(69) = "1000110000" 'E
arrC(70) = "0010110000" 'F
arrC(71) = "0000011010" 'G
arrC(72) = "1000011000" 'H
arrC(73) = "0010011000" 'I
arrC(74) = "0000111000" 'J
arrC(75) = "1000000110" 'K
arrC(76) = "0010000110" 'L
arrC(77) = "1010000100" 'M
arrC(78) = "0000100110" 'N
arrC(79) = "1000100100" 'O
arrC(80) = "0010100100" 'P
arrC(81) = "0000001110" 'Q
arrC(82) = "1000001100" 'R
arrC(83) = "0010001100" 'S
arrC(84) = "0000101100" 'T
arrC(85) = "1100000010" 'U
arrC(86) = "0110000010" 'V
arrC(87) = "1110000000" 'W
arrC(88) = "0100100010" 'X
arrC(89) = "1100100000" 'Y
arrC(90) = "0110100000" 'Z

    arrB = Testo.GetBytes("UTF8")
   Dim j() As Byte 
   j = "a".GetBytes("UTF8")
        
    For L = 0 To arrB.Length Step j.Length 
        
        StrTemp = StrTemp & arrC(arrB(L))

    Next
    
    For L = 1 To StrTemp.Length - 1
      
      
      b = Bit.Xor(b,1) 
        'b = b Xor True
      s = StrTemp.charat(L-1)
        'S = Mid$(StrTemp, L, 1)
        If b = 1 Then
            If s = "0" Then
                s = "9"
            Else
                s = "999"
            End If
        Else
            If s = "0" Then
                s = "A"
            Else
                s = "AAA"
            End If
        End If
        NewStr = NewStr & s
    Next
    
    
    Return NewStr
    
End Sub 
Sub CheckDigit39(Testo As String, _
         Verifica As Boolean) As String 
'-------------------------------------------------------
'Se il parametro opzionale è omesso o ha valore False
'la funzione restituisce il check digit(per codice 39)
'ovvero restituisce un carattere (string) che deve
'essere accodato a Testo.
'
'La funzione può viceversa essere utilizzata per il
'controllo del check digit (ultima cifra), in questo
'caso il parametro Verifica deve avere valore True
'e Testo deve essere comprensivo di carattere di
'controllo.
'La funzione una volta eseguita la verifica  restituirà
'True se il check digit è corretto oppure False se è
'errato (come tipi boolean).
'
'La funzione è quindi utilizzabile sia in fase di
'lettura del codice a barre (Verifica = True) sia in
'scrittura (Verifica = False e conseguente restituzione
'del solo carattere di controllo)
'-------------------------------------------------------

Dim arrB() As Byte
Dim arrC(90) As String
Dim L As Long
Dim P As Long
Dim D As Long
Dim S As Long
Dim Temp As Byte
Dim StrTemp As String
Dim Pd As Boolean

    arrC(48) = 0 '0
    arrC(49) = 1 '1
    arrC(50) = 2 '2
    arrC(51) = 3 '3
    arrC(52) = 4 '4
    arrC(53) = 5 '5
    arrC(54) = 6 '6
    arrC(55) = 7 '7
    arrC(56) = 8 '8
    arrC(57) = 9 '9
    arrC(65) = 10 'A
    arrC(66) = 11 'B
    arrC(67) = 12 'C
    arrC(68) = 13 'D
    arrC(69) = 14 'E
    arrC(70) = 15 'F
    arrC(71) = 16 'G
    arrC(72) = 17 'H
    arrC(73) = 18 'I
    arrC(74) = 19 'J
    arrC(75) = 20 'K
    arrC(76) = 21 'L
    arrC(77) = 22 'M
    arrC(78) = 23 'N
    arrC(79) = 24 'O
    arrC(80) = 25 'P
    arrC(81) = 26 'Q
    arrC(82) = 27 'R
    arrC(83) = 28 'S
    arrC(84) = 29 'T
    arrC(85) = 30 'U
    arrC(86) = 31 'V
    arrC(87) = 32 'W
    arrC(88) = 33 'X
    arrC(89) = 34 'Y
    arrC(90) = 35 'Z
    arrC(45) = 36 '-
    arrC(46) = 37 '.
    arrC(32) = 38 'SPAZIO
    arrC(36) = 39 '$
    arrC(47) = 40 '/
    arrC(43) = 41 '+
    arrC(37) = 42 '%

    If Verifica Then
        StrTemp = Right(Testo, 1)
        Testo = Left(Testo, Testo.Length - 1)
    End If

    arrB = Testo.GetBytes("UTF8")
        
    S = "A".GetBytes("UTF8").Length 
         
    For L = 0 To arrB.Length  Step S
        Temp = arrB(L)
        
        Select Temp
            Case 32, 36, 37, 42, 43
            Case 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57
            Case 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90
            Case Else
         
            Msgbox(Chr(Temp) & " is geen juiste karakter" & Chr(13) & _
                "Welke karakters zijn juist : " & _
                Chr(13) & _
                "Spatie $ % + - . /" & _
                Chr(13) & _
                "0 1 2 3 4 5 6 7 8 9" & _
                Chr(13) & _
                "A B C D E F G H I J " & _
                "K L M N O P Q R S " & _
                "T U V W X Y Z","error")
        End Select
        
            D = D + arrC(Temp)
            
    Next
    
    P = D Mod 43
    
    If Verifica Then
        If CStr(P) = StrTemp Then
            Return 
            'Return
        Else
            Return 
            'Return
        End If
    End If
    
    D = arrC.Length 
   
    
    For S = 32 To D
        If arrC(S) = CStr(P) Then
            Return Chr(S)
            'return
        End If
    Next
    
End Sub
Sub Right(Text As String, Length As Long) As String
   If Length>Text.Length Then Length=Text.Length
   Return Text.SubString(Text.Length-Length)
End Sub
Sub Left(Text As String, Length As Long)As String
   If Length>Text.Length Then Length=Text.Length
   Return Text.SubString2(0, Length)
End Sub
Sub CStr(i As Int) As String
  Return "" & i
End Sub
 
Upvote 0

urikupfer

Member
Licensed User
Longtime User
Try the class above

call generateqrcode(input As String,iv1 As Button) , the input is the string you want to wright on the qr code or the bare code and iv1 is the button you want to draw on.
For bar code ,use the sample urls above , it will work fine
 
Upvote 0

Bill Norris

Active Member
Licensed User
Longtime User
RE: Barcode

What about simply adding the appropriate barcode font to your files folder, then using it as the typeface of a label or text view?
 
Upvote 0

jayel

Active Member
Licensed User
Longtime User
Hey Bill,

As simple as it can be !

Thanks for the hint, it works with a barcoode font !

Greets ,

John
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…