Page 1 of 3

Create QRCode (2D-Barcode)

Posted: Fri Oct 21, 2011 7:09 am
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

Re: Create QRCode (2D-Barcode)

Posted: Fri Oct 21, 2011 1:17 pm
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

Re: Create QRCode (2D-Barcode)

Posted: Sat Oct 22, 2011 6:39 am
by idle
nice thanks

Re: Create QRCode (2D-Barcode)

Posted: Sat Oct 22, 2011 9:35 am
by MachineCode
Is there a way to do it without relying on external files like qrcodelib.lib and qrcodelib.dll?

Re: Create QRCode (2D-Barcode)

Posted: Sat Oct 22, 2011 12:38 pm
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:

Re: Create QRCode (2D-Barcode)

Posted: Sun Oct 23, 2011 12:06 am
by electrochrisso
Good one dige. :)

Re: Create QRCode (2D-Barcode)

Posted: Sun Oct 23, 2011 9:08 pm
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"

Re: Create QRCode (2D-Barcode)

Posted: Tue Jul 30, 2013 5:46 am
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"

Re: Create QRCode (2D-Barcode)

Posted: Wed Jul 31, 2013 3:46 am
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.

Re: Create QRCode (2D-Barcode)

Posted: Wed Jul 31, 2013 12:21 pm
by jack
you can use polib to make the lib
polib qrcodelib.dll /machine:ix86 /out:qrcodelib.lib

Re: Create QRCode (2D-Barcode)

Posted: Fri Aug 02, 2013 4:33 am
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. :)

Re: Create QRCode (2D-Barcode)

Posted: Sat Aug 03, 2013 3:59 pm
by NoahPhense
Image

5.20 b 8

Re: Create QRCode (2D-Barcode)

Posted: Tue Aug 06, 2013 11:41 am
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

Re: Create QRCode (2D-Barcode)

Posted: Tue Aug 06, 2013 2:53 pm
by NoahPhense
Image

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

- np

Re: Create QRCode (2D-Barcode)

Posted: Tue Aug 06, 2013 3:29 pm
by jassing
did you create the lib as indicated before your 1st post?