• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

QRCode Encode SDK - Transalate C Header to PB

Started by Roy Chan, November 03, 2011, 04:39:18 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Roy Chan

I try to transalate the the following c header to PB. But I am not sure the result is correct or not.


#ifndef QRCODEENCODEDLL_H
#define QRCODEENCODEDLL_H


#include "stdafx.h"

#define ENC_ALPHA 0;
#define ENC_BYTE 1;
#define ENC_NUMERIC 2;
#define ENC_CHINESE 3;
#define ENC_AUTO 4;

#define FNC1_MODE_NO 0;
#define FNC1_MODE_FIRST 1;
#define FNC1_MODE_SECOND 2;

#define CORRECTION_LEVEL_L  0;
#define CORRECTION_LEVEL_M  1;
#define CORRECTION_LEVEL_Q  2;
#define CORRECTION_LEVEL_H  3;

#define MICROQR_VER_M1    0;
#define MICROQR_VER_M2    1;
#define MICROQR_VER_M3    2;
#define MICROQR_VER_M4    3;


typedef struct tagQRCODECONTEXT
{
int nVersion;
int nCorrectionLevel;
int nEncodeMode;
int nEci;
int nFnc1Mode;
int nStructuredAppendIndex;
int nStructuredAppendCounter;
int nApplicationIndicator;
bool bStructuredAppend;
bool bProcessTilde;
bool bAutoConfigurate;
char cData[7100]; // _$3342﹛勤趼睫揹輛俴悵湔;
int     nSize;
BYTE nMargin;
BYTE nPixelSize;
COLORREF clBackGround;
COLORREF clForeGround;
} QRCODECONTEXT,*LPQRCODECONTEXT;


void __stdcall InitWorkSpace(QRCODECONTEXT *pQRCodeCtx);
HBITMAP __stdcall QRCodeEncode2Bitmap(QRCODECONTEXT *pQRCodeCtx);
BOOL __stdcall QRCodeEncode2File(QRCODECONTEXT *pQRCodeCtx,LPCTSTR pImageFile);
BOOL __stdcall FreeWorkSpace();
BOOL __stdcall Encode2File(char *pData, int nCorrectionLevel,int nMargin, int nPixelSize,LPCTSTR pImageFile);
HBITMAP __stdcall MicroQRCodeEncode2Bitmap(QRCODECONTEXT *pQRCodeCtx);
BOOL __stdcall MicroQRCodeEncode2File(QRCODECONTEXT *pQRCodeCtx,LPCTSTR pImageFile);

#endif /*QRCODEENCODEDLL_H*/


PB Code


%ENC_ALPHA = 0
%ENC_BYTE = 1
%ENC_NUMERIC = 2
%ENC_CHINESE = 3
%ENC_AUTO = 4

%FNC1_MODE_NO = 0
%FNC1_MODE_FIRST = 1
%FNC1_MODE_SECOND = 2

%CORRECTION_LEVEL_L  = 0
%CORRECTION_LEVEL_M  = 1
%CORRECTION_LEVEL_Q  = 2
%CORRECTION_LEVEL_H  = 3

%MICROQR_VER_M1 = 0
%MICROQR_VER_M2 = 1
%MICROQR_VER_M3 = 2
%MICROQR_VER_M4 = 3


Type QRCODECONTEXT
     nVersion As Long
     nCorrectionLevel As Long
     nEncodeMode As Long
     nEci As Long
     nFnc1Mode As Long
     nStructuredAppendIndex As Long
     nStructuredAppendCounter As Long
     nApplicationIndicator As Long
     bStructuredAppend As Byte
     bProcessTilde As Byte
     bAutoConfigurate As Byte
     cData As Asciiz * 7100             
     nSize As Long
     nMargin As BYTE
     nPixelSize As BYTE
     clBackGround As Dword
     clForeGround As Dword
End Type

Declare Sub InitWorkSpace lib "QrCodeEncodeDLL.dll" alias "InitWorkSpace"(byref pQRCodeCtx as QRCODECONTEXT)
Declare Function FreeWorkSpace lib "QrCodeEncodeDLL.dll" alias "FreeWorkSpace" as byte
Declare Function QRCodeEncode2Bitmap lib "QrCodeEncodeDLL.dll" alias "QRCodeEncode2Bitmap" (byref pQRCodeCtx as QRCODECONTEXT) as dword
Declare Function QRCodeEncode2File lib "QrCodeEncodeDLL.dll" alias "QRCodeEncode2File" (byref pQRCodeCtx as QRCODECONTEXT, byref  pImageFile as stringz) as byte
Declare Function MicroQRCodeEncode2Bitmap lib "QrCodeEncodeDLL.dll" alias "MicroQRCodeEncode2Bitmap" (byref pQRCodeCtx as QRCODECONTEXT) as dword
Declare Function MicroQRCodeEncode2File lib "QrCodeEncodeDLL.dll" alias  "MicroQRCodeEncode2File" (byref pQRCodeCtx as QRCODECONTEXT,byref pImageFile as stringz) as byte
Declare Function Encode2File lib "QrCodeEncodeDLL.dll" alias "Encode2File" (byref pData as stringZ,byval nCorrectionLevel as long, byval nMargin as long,byval nPixelSize as long,byref pImageFile as stringz) as byte

Roy Chan
iniSoft System Technology Limited
Lenovo ThinkPad SL410 4GB Ram,
Windows XP SP3 / PBWin 10 & FireFly 3.5

José Roca

These need to be changed:


Declare Function FreeWorkSpace lib "QrCodeEncodeDLL.dll" alias "FreeWorkSpace" as Long
Declare Function QRCodeEncode2File lib "QrCodeEncodeDLL.dll" alias "QRCodeEncode2File" (byref pQRCodeCtx as QRCODECONTEXT, byref  pImageFile as stringz) as Long
Declare Function Encode2File lib "QrCodeEncodeDLL.dll" alias "Encode2File" (BYVAL pData as BYTE PTR,byval nCorrectionLevel as long, byval nMargin as long,byval nPixelSize as long,byref pImageFile as stringz) as Long
Declare Function MicroQRCodeEncode2File lib "QrCodeEncodeDLL.dll" alias  "MicroQRCodeEncode2File" (byref pQRCodeCtx as QRCODECONTEXT,byref pImageFile as stringz) as Long


bool is Byte, but BOOL is Long.

cData As Asciiz * 7100  needs also to be changed.

You can use cData(0 TO 7099) As Byte or cData As STRING * 7100.

Roy Chan

Thank you very much. I really appreciate your quickly reply.
Roy Chan
iniSoft System Technology Limited
Lenovo ThinkPad SL410 4GB Ram,
Windows XP SP3 / PBWin 10 & FireFly 3.5

Roy Chan

Except the DLL SDK. They also provide the "QRCodeDecodeLIB.lib" (C Static Library SDK)
Is it possible to translate the C Lib Files to PB Code ?
Roy Chan
iniSoft System Technology Limited
Lenovo ThinkPad SL410 4GB Ram,
Windows XP SP3 / PBWin 10 & FireFly 3.5

José Roca

 
No, you can't use static libraries from other languages.

Roy Chan

#5
How to translate the following line ? I still can not generate the QRCode Correctly.


sprintf(tQrCtx.cData,"http://www.aipsys.com");


Visual C++ Example

#include "QRCodeEncodeDLL.h"
.....
tagQRCODECONTEXT tQrCtx;
InitWorkSpace(&tQrCtx);
tQrCtx.preferredVersion = 2;
tQrCtx.selectedVersion = 2;
tQrCtx.correctionLevel = 0;
tQrCtx.encoding = 1;
tQrCtx.eci = 0;
tQrCtx.fnc1Mode = 0;
// tQrCtx.structuredAppendIndex;
// tQrCtx.structuredAppendCounter;
// tQrCtx.applicationIndicator;
tQrCtx.structuredAppend = false;
tQrCtx.processTilde = true;
tQrCtx.autoConfigurate = false;
tQrCtx.nMargin = 10;
tQrCtx.nPixelSize = 4;
// tQrCtx.clBackGround = RGB(255,255,255);
tQrCtx.clForeGround = RGB(255,0,0);
sprintf(tQrCtx.cData,"http://www.aipsys.com");
QRCodeEncode2File(&tQrCtx, "c:\\qrcode.bmp");
FreeWorkSpace();
....
LIBRARY for linking
QRCodeEncodeDLL.lib
RUNTIME LIBRARY
QRCodeEncodeDLL.DLL



My PB Code

        Dim ctx As QRCODECONTEXT
        Dim pctx As QRCODECONTEXT Pointer
        Dim aa As Dword
     
        pctx=VarPtr(ctx)
       
        initworkspace @pctx
       
        ctx.nVersion=2
        ctx.nCorrectionLevel=0                               
        ctx.nEncodeMode=0
        ctx.nFnc1Mode =0
        ctx.nMargin =10
        ctx.nEci =0
        ctx.nStructuredAppendIndex =0
        ctx.nStructuredAppendCounter=0               
        ctx.bAutoConfigurate=0
        ctx.cData ="http://www.apisys.com"   
        ctx.nPixelSize= 4
        ctx.bStructuredAppend =0
        ctx.clBackGround =Rgb(255,255,255)
        ctx.clForeGround =Rgb(255,0,0)
        ctx.bProcessTilde=-1

         aa= QRCodeEncode2Bitmap(@pctx)
         ztrace Str$(aa)        'I got the value 0
         SendMessage (HWND_FRMMAIN_PICTURE1, %STM_SETIMAGE, %IMAGE_BITMAP, aa)
Roy Chan
iniSoft System Technology Limited
Lenovo ThinkPad SL410 4GB Ram,
Windows XP SP3 / PBWin 10 & FireFly 3.5

José Roca


        Dim ctx As QRCODECONTEXT
        Dim aa As Dword
     
        initworkspace ctx
       
        ctx.nVersion=2
        ctx.nCorrectionLevel=0                               
        ctx.nEncodeMode=0
        ctx.nFnc1Mode =0
        ctx.nMargin =10
        ctx.nEci =0
        ctx.nStructuredAppendIndex =0
        ctx.nStructuredAppendCounter=0               
        ctx.bAutoConfigurate=0
        ctx.cData ="http://www.apisys.com"   
        ctx.nPixelSize= 4
        ctx.bStructuredAppend =0
        ctx.clBackGround =Rgb(255,255,255)
        ctx.clForeGround =Rgb(255,0,0)
        ctx.bProcessTilde=-1

         aa= QRCodeEncode2Bitmap(ctx)
         ztrace Str$(aa)        'I got the value 0
         SendMessage (HWND_FRMMAIN_PICTURE1, %STM_SETIMAGE, %IMAGE_BITMAP, aa)


Roy Chan

Thank you very much. I modified the code according to your sample. The program will crash at the following line. The result same as before.  I already send a mail to ask the supplier. They just sent me a VB.NET sample and told me not to use the pointer.

aa= QRCodeEncode2Bitmap(ctx)


VB.Net Sample

Imports System
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices


Module Module1

    <StructLayout(LayoutKind.Sequential)> _
    Structure tagQRCODECONTEXT
        Dim nVersion As Integer
        Dim nCorrectionLevel As Integer
        Dim nEncodeMode As Integer
        Dim nEci As Integer
        Dim nFnc1Mode As Integer
        Dim nStructuredAppendIndex As Integer
        Dim nStructuredAppendCounter As Integer
        Dim nApplicationIndicator As Integer
        Dim bStructuredAppend As Byte
        Dim bProcessTilde As Byte
        Dim bAutoConfigurate As Byte
        <MarshalAs(UnmanagedType.ByValArray, sizeconst:=7100)> _
        Public cData() As Byte
        Public Sub Initialize()
            ReDim cData(7100)
        End Sub

        '<VBFixedString(7100), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=7100)> Public cData() As Char
        'Dim strBuff(256) As Byte
        Dim nSize As Integer
        Dim nMargin As Byte
        Dim nPixelSize As Byte
        Dim clBackGround As Integer
        Dim clForeGround As Integer
    End Structure

    Public Class LibWrap
        <DllImport("QRCodeEncodeDll.dll", CallingConvention:=CallingConvention.StdCall)> _
        Overloads Shared Sub InitWorkSpace(ByRef ctxQr As tagQRCODECONTEXT)
        End Sub
        <DllImport("QRCodeEncodeDll.dll", CallingConvention:=CallingConvention.StdCall)> _
        Overloads Shared Function QRCodeEncode2File(ByRef ctxQr As tagQRCODECONTEXT, ByVal sFile As String) As Boolean
        End Function
        <DllImport("QRCodeEncodeDll.dll", CallingConvention:=CallingConvention.Cdecl)> _
        Overloads Shared Function QRCodeEncode2Bitmap(ByRef ctxQr As tagQRCODECONTEXT) As Long
        End Function
        <DllImport("QRCodeEncodeDll.dll", CallingConvention:=CallingConvention.Cdecl)> _
        Overloads Shared Function FreeWorkSpace(ByRef ctxQr As tagQRCODECONTEXT) As Boolean

        End Function
        <DllImport("QRCodeEncodeDll.dll", CallingConvention:=CallingConvention.Cdecl)> _
        Overloads Shared Function Encode2File(ByVal pData As String, ByVal nCorrectionLevel As Integer, ByVal nMargin As Integer, ByVal nPixelSize As Integer, ByVal pImageFile As String) As Boolean

        End Function

    End Class 'LibWrap


End Module



Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim qrCtx As New tagQRCODECONTEXT
        qrCtx.Initialize()
        LibWrap.InitWorkSpace(qrCtx)
        'qrCtx.nVersion = -1

        qrCtx.nCorrectionLevel = 2
        qrCtx.nEncodeMode = 4
        qrCtx.bAutoConfigurate = True
        'qrCtx.cData = "1234567890"

        qrCtx.cData(0) = 57
        qrCtx.cData(1) = 48
        qrCtx.cData(2) = 49
        qrCtx.cData(3) = 50
        qrCtx.cData(4) = 51
        qrCtx.cData(5) = 52
        qrCtx.cData(6) = 53
        qrCtx.cData(7) = 54
        qrCtx.cData(8) = 55
        qrCtx.cData(9) = 56

        qrCtx.nSize = 10
        qrCtx.nMargin = 20
        qrCtx.nPixelSize = 4   'Auto mode
        'qrCtx.clBackGround = &HFFFFFFFF
        'qrCtx.clForeGround = &H0


        'If (LibWrap.Encode2File("1234567890", 2, 10, 4, "c:\pic4.bmp")) Then
        If (LibWrap.QRCodeEncode2File(qrCtx, "c:\pic4.bmp")) Then
            MsgBox("SUccess, Trial version will replace some char of input with '*' randomly")
        Else
            MsgBox("failed")
        End If

    End Sub
End Class
Roy Chan
iniSoft System Technology Limited
Lenovo ThinkPad SL410 4GB Ram,
Windows XP SP3 / PBWin 10 & FireFly 3.5

José Roca

You must use proper alignment for the structure:


Type QRCODECONTEXT DWORD FILL
     nVersion As Long
     nCorrectionLevel As Long
     nEncodeMode As Long
     nEci As Long
     nFnc1Mode As Long
     nStructuredAppendIndex As Long
     nStructuredAppendCounter As Long
     nApplicationIndicator As Long
     bStructuredAppend As Byte
     bProcessTilde As Byte
     bAutoConfigurate As Byte
     cData (0 TO 7099) As Byte
     nSize As Long
     nMargin As BYTE
     nPixelSize As BYTE
     clBackGround As Dword
     clForeGround As Dword
End Type


Roy Chan

This is the final version. I can generate QRCode correctly.


'  ====================================================================
'                                                               
'  QrCodeEncodeDLL Contants
'
'  ====================================================================

%ENC_ALPHA = 0
%ENC_BYTE = 1
%ENC_NUMERIC = 2
%ENC_CHINESE = 3
%ENC_AUTO = 4

%FNC1_MODE_NO = 0
%FNC1_MODE_FIRST = 1
%FNC1_MODE_SECOND = 2

%CORRECTION_LEVEL_L  = 0
%CORRECTION_LEVEL_M  = 1
%CORRECTION_LEVEL_Q  = 2
%CORRECTION_LEVEL_H  = 3

%MICROQR_VER_M1 = 0
%MICROQR_VER_M2 = 1
%MICROQR_VER_M3 = 2
%MICROQR_VER_M4 = 3


Type QRCODECONTEXT DWORD FILL
     nVersion As Long
     nCorrectionLevel As Long
     nEncodeMode As Long
     nEci As Long
     nFnc1Mode As Long
     nStructuredAppendIndex As Long
     nStructuredAppendCounter As Long
     nApplicationIndicator As Long
     bStructuredAppend As Byte
     bProcessTilde As Byte
     bAutoConfigurate As Byte
     cData (1 TO 7100) As Byte
     nSize As Long
     nMargin As BYTE
     nPixelSize As BYTE
     clBackGround As Dword
     clForeGround As Dword
End Type

'============================<[ Declares ]>============================

Declare Sub InitWorkSpace lib "QrCodeEncodeDLL.dll" alias "InitWorkSpace"(byref pQRCodeCtx as QRCODECONTEXT)
Declare Function FreeWorkSpace lib "QrCodeEncodeDLL.dll" alias "FreeWorkSpace" as long
Declare Function QRCodeEncode2Bitmap lib "QrCodeEncodeDLL.dll" alias "QRCodeEncode2Bitmap" (byref pQRCodeCtx as QRCODECONTEXT) as dword
Declare Function QRCodeEncode2File lib "QrCodeEncodeDLL.dll" alias "QRCodeEncode2File" (byref pQRCodeCtx as QRCODECONTEXT, byref  pImageFile as stringz) as long


How can I copy the string to User Define Type (ctx.cData) ? Can I user Memory Copy or FF_MemCopy to copy the string to ctx.data ?
I use the following code but got error.

Dim cc As String

cc="http://www.apisys.com"
FF_MemCopy(StrPtr(cc),VarPtr(ctx.cData) , Len(cc))
Roy Chan
iniSoft System Technology Limited
Lenovo ThinkPad SL410 4GB Ram,
Windows XP SP3 / PBWin 10 & FireFly 3.5

José Roca

Use FF_MemCopy(StrPtr(cc),VarPtr(ctx.cData(1)) , Len(cc)).

Roy Chan

Thank you very much. I learned a lot of thing here.
Roy Chan
iniSoft System Technology Limited
Lenovo ThinkPad SL410 4GB Ram,
Windows XP SP3 / PBWin 10 & FireFly 3.5