Create QRCode (2D-Barcode)

Share your advanced PureBasic knowledge/code with the community.
dige
Addict
Addict
Posts: 1247
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Create QRCode (2D-Barcode)

Post by dige »

Image
Link to http://www.purebasic.com

Code: Select all

; by Dige 10/2011
; http://www.purebasic.fr/english/viewtopic.php?f=12&t=47938
; Create 2D Barcodes (QRCode) based on qrencode-win32
; Requires: qrcodelib.lib, qrcodelib.dll
; http://code.google.com/p/qrencode-win32/downloads/list

Structure QRCode
  Version.l
  Width.l
  pSymbolData.l
EndStructure

Enumeration
  #QR_ECLEVEL_L = 0 ; lowest
  #QR_ECLEVEL_M
  #QR_ECLEVEL_Q
  #QR_ECLEVEL_H     ; highest
EndEnumeration

ImportC "..\Lib\qrcodelib.lib"
  QRcode_encodeString8bit(Text.p-ascii, Version.l, QRecLevel.l) As "_QRcode_encodeString8bit"
  QRcode_free(*Qrcode.QRCode) As "_QRcode_free"
EndImport

Procedure CreateQRCode (content.s, ImgID = #PB_Any, EC_Level = #QR_ECLEVEL_L, Size=4 )
  Protected *Qrcode.QRCode, QRImg
  
  *Qrcode = QRcode_encodeString8bit(content, 0, EC_Level)
  
  With *Qrcode
    If *Qrcode = 0 Or \Width = 0
      ProcedureReturn #Null
    Else
      *mem = \pSymbolData
      w    = \Width
    EndIf
  EndWith
  
  QRImg  = CreateImage(ImgID, w, w)
  
  If QRImg
    If ImgID = #PB_Any
      ImgID = QRImg
    EndIf
  EndIf
  
  If StartDrawing(ImageOutput(ImgID))
      ; White Background
      Box (0, 0, ImageWidth(ImgID), ImageHeight(ImgID), #White)  
      
      ; Draw Black Dots
      For y = 0 To w - 1
        For x = 0 To w - 1
          b = PeekB(*mem) & $FF
          If b & 1
            Plot( x, y, #Black)
          EndIf
          *mem + 1
        Next
      Next
      
    StopDrawing()
    
    w * Size
    ResizeImage( ImgID, w, w, #PB_Image_Raw)
  EndIf
  
  QRcode_free(*Qrcode)
  
  ProcedureReturn ImgID
EndProcedure

; Example, how to use it:
CreateImage(0, 200, 200)

OpenWindow(#Null, 0, 0, 450, 400, "2D Barcode Creator", #WS_OVERLAPPEDWINDOW|#PB_Window_ScreenCentered )
TextGadget(#PB_Any, 10, 4, 50, 16, "Text" )
StringGadget(0, 10, 20, 200, 20, "Feel the Pure Power!" )

TextGadget(#PB_Any, 10, 44, 50, 16, "EC_Level" )
TrackBarGadget(1, 10, 60, 100, 20, #QR_ECLEVEL_L, #QR_ECLEVEL_H, #PB_TrackBar_Ticks)

TextGadget(#PB_Any, 110, 44, 50, 16, "Size" )
TrackBarGadget(2, 110, 60, 100, 20, 1, 10, #PB_TrackBar_Ticks)

ImageGadget (3, 10, 90, 430, 550, ImageID(0), #PB_Image_Border)

Repeat
  
  Event = WaitWindowEvent()
  
  If Event = #PB_Event_Gadget
    Select EventGadget()
      Case 0, 1, 2
        ImgID = CreateQRCode(GetGadgetText(0), #Null, GetGadgetState(1), GetGadgetState(2))
        If IsImage(ImgID)
          SetGadgetState(3, ImageID(ImgID))
        EndIf
    EndSelect
  EndIf

Until Event = #PB_Event_CloseWindow
Last edited by dige on Wed Oct 03, 2012 3:17 pm, edited 1 time in total.
"Daddy, I'll run faster, then it is not so far..."
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Create QRCode (2D-Barcode)

Post by Kwai chang caine »

Thanks a lot DIGE for sharing 8)
That's works fine

Perhaps a day you can create the scanner, like for normal Barcode :wink:
http://www.purebasic.fr/german/viewtopi ... 74#p288174

Good day
ImageThe happiness is a road...
Not a destination
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Create QRCode (2D-Barcode)

Post by idle »

nice thanks
Windows 11, Manjaro, Raspberry Pi OS
Image
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Create QRCode (2D-Barcode)

Post by MachineCode »

Is there a way to do it without relying on external files like qrcodelib.lib and qrcodelib.dll?
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Create QRCode (2D-Barcode)

Post by ts-soft »

MachineCode wrote:Is there a way to do it without relying on external files like qrcodelib.lib and qrcodelib.dll?
Translate the c-source to pb or write your own code :mrgreen:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
electrochrisso
Addict
Addict
Posts: 980
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: Create QRCode (2D-Barcode)

Post by electrochrisso »

Good one dige. :)
PureBasic! Purely one of the best 8)
User avatar
fiver
User
User
Posts: 36
Joined: Wed May 05, 2004 8:21 pm
Location: An outer spiral arm of the Milky Way

Re: Create QRCode (2D-Barcode)

Post by fiver »

Thanks for sharing! This will be really useful, these seem to be everywhere nowadays.
I managed to compile static libraries for linux and osx, see link below, on osx the images seem blurred which may be something to do with the 2D drawing lib in RC2 - the osx qrencode console version produced pngs that were fine.
I tried to build the static lib for windows but while it compiled ok, polink complained about missing references to strdup. This is a known problem though it seems, with mingw not properly exporting some functions and will likely be addressed in the next release of qrencode.

Link for the linux and osx libs:

http://goo.gl/xlJ9m

On linux change the import statement in Dige's code to:

ImportC "libqrencode.a"
QRcode_encodeString8bit(Text.p-ascii, Version.l, QRecLevel.l) As "QRcode_encodeString8bit"
QRcode_free(*Qrcode.QRCode) As "QRcode_free"
EndImport

For osx just change the first line of the import to:

ImportC "libqrencode.a"
zefiro_flashparty
User
User
Posts: 74
Joined: Fri Mar 04, 2005 7:46 pm
Location: argentina

Re: Create QRCode (2D-Barcode)

Post by zefiro_flashparty »

https://docs.google.com/leaf?id=0B_EW83 ... ist&num=50

https://docs.google.com/leaf?id=0B_EW83 ... ist&num=50
download QrcodeXtra.7z

unpack
qrcodelib.dll
qrcodelib.lib

and copy to
"C:\Program Files\PureBasic\Compilers"
Amd Vishera fx8350 ,16Gbram, Gtx650 ti, 2gb,Win 10pro. 13tbs. 8)
User avatar
electrochrisso
Addict
Addict
Posts: 980
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: Create QRCode (2D-Barcode)

Post by electrochrisso »

zefiro_flashparty wrote:https://docs.google.com/leaf?id=0B_EW83 ... ist&num=50

https://docs.google.com/leaf?id=0B_EW83 ... ist&num=50
download QrcodeXtra.7z

unpack
qrcodelib.dll
qrcodelib.lib

and copy to
"C:\Program Files\PureBasic\Compilers"
Where is the qrcodelib.lib :?:
I can not see it in the download.
PureBasic! Purely one of the best 8)
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: Create QRCode (2D-Barcode)

Post by jack »

you can use polib to make the lib
polib qrcodelib.dll /machine:ix86 /out:qrcodelib.lib
User avatar
electrochrisso
Addict
Addict
Posts: 980
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: Create QRCode (2D-Barcode)

Post by electrochrisso »

jack wrote:you can use polib to make the lib
polib qrcodelib.dll /machine:ix86 /out:qrcodelib.lib
Too easy, thanks for the info Jack. :)
PureBasic! Purely one of the best 8)
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: Create QRCode (2D-Barcode)

Post by NoahPhense »

Image

5.20 b 8
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Create QRCode (2D-Barcode)

Post by applePi »

thanks, very beautiful, after adding SaveImage(0,"pic.bmp") at the last line ,i have pasted the text in my post here to the text box, then uploading the png version of the bmp to http://www.onlinebarcodereader.com/ , it display exactly this same post. impressive. but the text should be continuous ie no new line.
Image
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: Create QRCode (2D-Barcode)

Post by NoahPhense »

Image

What am I doing wrong that I cannot get this to work with 5.20 b8?

- np
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: Create QRCode (2D-Barcode)

Post by jassing »

did you create the lib as indicated before your 1st post?
Post Reply