Hi,
Alaska provides a library for printing QR codes. However, I'd like to save them as a PNG file instead of printing them. How can I do this?
Regards
Piotr
Save QR as PNG file
Re: Save QR as PNG file
Try
StrFile(xvar,"abc.png")
Joe
StrFile(xvar,"abc.png")
Joe
Re: Save QR as PNG file
Piotr,
I assume you are familiar with the QR-Code capabilities of the 2d-barcode asset that you can install using the workbench.
Based on this asset you need to create a Presentation Space that you connect with a XbpBitmap object. Let the QRCode object generate the output to that Presentation Space and then use the XbpBitmap object to save the image to a file.
Please refer to the sample attached to this post.
Hope this helps.
With my best regards,
I assume you are familiar with the QR-Code capabilities of the 2d-barcode asset that you can install using the workbench.
Based on this asset you need to create a Presentation Space that you connect with a XbpBitmap object. Let the QRCode object generate the output to that Presentation Space and then use the XbpBitmap object to save the image to a file.
Please refer to the sample attached to this post.
Hope this helps.
With my best regards,
Pedro Alexandre
Re: Save QR as PNG file
Hi Pedro
many thanks for your help. I will try.
Regards
Piotr
many thanks for your help. I will try.
Regards
Piotr
Re: Save QR as PNG file
Alaska algorithm for drawing QR is very slow, because it draws every pixel with GraBox(). If you display QR on the screen, you can view drawing of each pixel. For larger QR with dimension of 400 and higher, it takes 2-3 seconds to draw.
Slavoljub Damnjanovic
SD-SoftDesign, Alaska Software Technology Partner
https://www.sd-softdesign.com
https://www.sd-softdesign.rs
SD-SoftDesign, Alaska Software Technology Partner
https://www.sd-softdesign.com
https://www.sd-softdesign.rs
Re: Save QR as PNG file
Hi Pedro,
your sample works fine, but if I change in:
oBmp:saveFile( "..\out.gif", XBPBMP_FORMAT_GIF )
to PNG format:
oBmp:saveFile( "..\out.gif", XBPBMP_FORMAT_PNG )
the the method returns false and file is not created. The file created with XBPBMP_FORMAT_GIF is for me enough, but creation with XBPBMP_FORMAT_PNG is not possible. Or maybe I should do something extra?
Regards
Piotr
your sample works fine, but if I change in:
oBmp:saveFile( "..\out.gif", XBPBMP_FORMAT_GIF )
to PNG format:
oBmp:saveFile( "..\out.gif", XBPBMP_FORMAT_PNG )
the the method returns false and file is not created. The file created with XBPBMP_FORMAT_GIF is for me enough, but creation with XBPBMP_FORMAT_PNG is not possible. Or maybe I should do something extra?
Regards
Piotr
Re: Save QR as PNG file
Saving to PNG (and JPG) needs the "compression"-parameter of XbpBitmap:SaveFile. It's the third one. 0 means no compression (max quality), 100 means max compression (lowest quality).
Code: Select all
oBmp:saveFile( "..\out.png", XBPBMP_FORMAT_PNG, 0 )Best regards,
Tom
"Did I offend you?"
"No."
"Okay, give me a second chance."
Tom
"Did I offend you?"
"No."
"Okay, give me a second chance."
Re: Save QR as PNG file
Hi Tom,
Using the third parameter does not help.
Below is the function:
FUNCTION QR2PNG(cTekst)
* The function saves the QR code as a PNG image but read and converted to base64
*************************************************************************************
Local oPS,oQR,oBmp,cTmpFile:='QR'+LTRIM(STR(Seconds()*100,8,0))+ '.PNG',cPNGbase64
Local nHandle,cBuffer,nX,nMargin,nPPSize
IF EMPTY(cTekst)
RETURN('')
ENDIF
oPS:= XbpPresSpace():new():create()
oQR := QRCode():New( cTekst, "QR_ECLEVEL_L" )
nX := oQR:getMinimumWidth()
nMargin := Int(nX/4)
nPSSize := Int(4*nX)+(2*nMargin)
oBmp:= XbpBitmap():New():Create()
oBmp:presSpace( oPS )
oBmp:make( nPSSize, nPSSize )
oPS:SetColor( GraMakeRGBColor({255,255,255}), 0)
GraBox(oPS,{0,0},{nPSSize,nPSSize},GRA_FILL,0,0)
oQR:Draw(oPS,{nMargin,nMargin},nPSSize )
oBmp:saveFile( 'Test1.PNG', XBPBMP_FORMAT_PNG,0 ) // return .F. , file is not created
oBmp:saveFile( 'Test2.PNG', XBPBMP_FORMAT_PNG,50 ) // return .F. , file is not created
oBmp:saveFile( 'Test3.PNG', XBPBMP_FORMAT_PNG,100 ) // return .F. , file is not created
IF oBmp:saveFile( cTmpFile, XBPBMP_FORMAT_GIF ) // return .T. , file is created
oBmp:destroy()
oPS:destroy()
nHandle:=FOpen(cTmpFile,FO_READ)
cPNGbase64:=''
i:=512
DO WHILE i=512
cBuffer:=SPACE(512)
i:=FRead(nHandle,@cBuffer,512)
cPNGbase64:=cPNGbase64+cBuffer
ENDDO
FClose(nHandle)
FErase(cTmpFile)
cPNGbase64:=Bin2Base64(cPNGbase64)
ELSE
*
cPNGbase64:=''
ENDIF
RETURN(cPNGbase64)
Regards,
Piotr
Using the third parameter does not help.
Below is the function:
FUNCTION QR2PNG(cTekst)
* The function saves the QR code as a PNG image but read and converted to base64
*************************************************************************************
Local oPS,oQR,oBmp,cTmpFile:='QR'+LTRIM(STR(Seconds()*100,8,0))+ '.PNG',cPNGbase64
Local nHandle,cBuffer,nX,nMargin,nPPSize
IF EMPTY(cTekst)
RETURN('')
ENDIF
oPS:= XbpPresSpace():new():create()
oQR := QRCode():New( cTekst, "QR_ECLEVEL_L" )
nX := oQR:getMinimumWidth()
nMargin := Int(nX/4)
nPSSize := Int(4*nX)+(2*nMargin)
oBmp:= XbpBitmap():New():Create()
oBmp:presSpace( oPS )
oBmp:make( nPSSize, nPSSize )
oPS:SetColor( GraMakeRGBColor({255,255,255}), 0)
GraBox(oPS,{0,0},{nPSSize,nPSSize},GRA_FILL,0,0)
oQR:Draw(oPS,{nMargin,nMargin},nPSSize )
oBmp:saveFile( 'Test1.PNG', XBPBMP_FORMAT_PNG,0 ) // return .F. , file is not created
oBmp:saveFile( 'Test2.PNG', XBPBMP_FORMAT_PNG,50 ) // return .F. , file is not created
oBmp:saveFile( 'Test3.PNG', XBPBMP_FORMAT_PNG,100 ) // return .F. , file is not created
IF oBmp:saveFile( cTmpFile, XBPBMP_FORMAT_GIF ) // return .T. , file is created
oBmp:destroy()
oPS:destroy()
nHandle:=FOpen(cTmpFile,FO_READ)
cPNGbase64:=''
i:=512
DO WHILE i=512
cBuffer:=SPACE(512)
i:=FRead(nHandle,@cBuffer,512)
cPNGbase64:=cPNGbase64+cBuffer
ENDDO
FClose(nHandle)
FErase(cTmpFile)
cPNGbase64:=Bin2Base64(cPNGbase64)
ELSE
*
cPNGbase64:=''
ENDIF
RETURN(cPNGbase64)
Regards,
Piotr