//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "MAKEPDF417Lib_TLB.cpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
String CurrentDir = GetCurrentDir();
/*typedef void (__stdcall *MAKE)(unsigned char* ucData, long nDataLen, char* szBmpFileName, long nClumn, long nErr, long nHLRatio,long nHeight);
typedef long (__stdcall *MIXTEXT)(char*  szSrcBmpFileName,char*  szDstBmpFileName,char*  szText,long lFontSize,long lTxtHeight,long lHmargin,long lVmargin,long lTxtCntOneLine);
typedef int  (__stdcall *SETCOLORDEPTH)(int nDepth);
typedef int  (__stdcall *GETCOLORDEPTH)();
MAKE make;
MIXTEXT  MixText;
SETCOLORDEPTH SetColorDepth;
GETCOLORDEPTH GetColorDepth;*/
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    String Str = Memo1->Lines->Text.Trim();
    String Len = Edit1->Text.Trim();
    String err = Edit2->Text.Trim();
    String hLRatio=Edit3->Text.Trim();
    String height=Edit5->Text.Trim();
    String color=Edit4->Text.Trim();
    String szText=Edit7->Text.Trim();
    String txtCntOneLine=Edit6->Text.Trim();
    String fontSize=Edit8->Text.Trim();
    String txtHeight=Edit9->Text.Trim();
    String hmargin=Edit10->Text.Trim();
    String vmargin=Edit11->Text.Trim();

    wchar_t* bmpFile = L"barcode.bmp";
    //wchar_t* zz = L(szText.c_str());

    CoInitialize(NULL);


    IBarCodePDF417Ptr pBar;
    CLSID clsid;
    CLSIDFromProgID(OLESTR("MakePdf417.BarCodePDF417"), &clsid);
    CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, __uuidof(IBarCodePDF417), (LPVOID*)&pBar);

    //封装数据进VARIANT
    VARIANT var;
    VariantInit(&var);

    SAFEARRAYBOUND sab[1];
    sab[0].cElements = Str.Length();
    sab[0].lLbound = 0;
    SAFEARRAY* psa = ::SafeArrayCreate(VT_UI1, 1, sab);
    BYTE* byData;
    ::SafeArrayAccessData(psa, (void**)& byData);
    for(int i = 0; i<Str.Length(); i++)
       {
            String a = Str.LoadStr(i);
            byData[i] = (unsigned char)a.c_str();
       }
    ::SafeArrayUnaccessData(psa);
    var.parray = psa;
    var.vt = VT_ARRAY|VT_UI1;

   // pBar->SetColorDepth((long)StrToInt(color));
    pBar->MakeBinary(var,bmpFile,long(StrToInt(Len)),long(StrToInt(err)),long(StrToInt(hLRatio)),long(StrToInt(height)));
    pBar->MixText(bmpFile,bmpFile,WideString(szText).c_bstr(),(long)StrToInt(fontSize),(long)StrToInt(txtHeight),(long)StrToInt(hmargin),(long)StrToInt(vmargin),(long)StrToInt(txtCntOneLine));
    VariantClear(&var);
    Image1->Picture->LoadFromFile("barcode.bmp");
    CoUninitialize();


}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
     Memo1->Lines->Strings[0] = "2004062100000014ST0005";
     Memo1->Lines->Strings[1] = "机电式";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
     String Len = Edit1->Text.Trim();
     String err = Edit2->Text.Trim();
     String hLRatio=Edit3->Text.Trim();
     String height=Edit5->Text.Trim();
     String color=Edit4->Text.Trim();
     String szText=Edit7->Text.Trim();
     String txtCntOneLine=Edit6->Text.Trim();
     String fontSize=Edit8->Text.Trim();
     String txtHeight=Edit9->Text.Trim();
     String hmargin=Edit10->Text.Trim();
     String vmargin=Edit11->Text.Trim();
     OpenDialog1->InitialDir = CurrentDir + "\\bmp\\";
     OpenDialog1->Execute();

     TFileStream * OpenFile = new TFileStream(OpenDialog1->FileName, fmOpenRead);
     int MemSize;
     char * Buffer;
     MemSize = OpenFile->Size+1;
     Buffer = new char[MemSize];
     OpenFile->Read(Buffer, MemSize);
     //Str=new String(Str);


      wchar_t* bmpFile = L"barcode.bmp";
    //wchar_t* zz = L(szText.c_str());

    CoInitialize(NULL);


    IBarCodePDF417Ptr pBar;
    CLSID clsid;
    CLSIDFromProgID(OLESTR("MakePdf417.BarCodePDF417"), &clsid);
    CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, __uuidof(IBarCodePDF417), (LPVOID*)&pBar);

    //封装数据进VARIANT
    VARIANT var;
    VariantInit(&var);

    SAFEARRAYBOUND sab[1];
    sab[0].cElements = MemSize;
    sab[0].lLbound = 0;
    SAFEARRAY* psa = ::SafeArrayCreate(VT_UI1, 1, sab);
    BYTE* byData;
    ::SafeArrayAccessData(psa, (void**)& byData);
    for(int i = 0; i<MemSize; i++)
       {
            String a = Buffer[i];
            //byData[i] = (unsigned char)a.c_str();
       }
    ::SafeArrayUnaccessData(psa);
    var.parray = psa;
    var.vt = VT_ARRAY|VT_UI1;

    pBar->SetColorDepth((long)StrToInt(color));
    pBar->MakeBinary(var,bmpFile,long(StrToInt(Len)),long(StrToInt(err)),long(StrToInt(hLRatio)),long(StrToInt(height)));
    pBar->MixText(bmpFile,bmpFile,WideString(szText).c_bstr(),(long)StrToInt(fontSize),(long)StrToInt(txtHeight),(long)StrToInt(hmargin),(long)StrToInt(vmargin),(long)StrToInt(txtCntOneLine));
    VariantClear(&var);
    Image1->Picture->LoadFromFile("barcode.bmp");
    CoUninitialize();


     Image1->Picture->LoadFromFile("barcode.bmp");
     delete Buffer;
     delete OpenFile;

     /*OpenDialog1->InitialDir = CurrentDir + "\\bmp\\";
     OpenDialog1->Execute();

     TFileStream * OpenFile = new TFileStream(OpenDialog1->FileName, fmOpenRead);
     int MemSize;
     char * Buffer;
     MemSize = OpenFile->Size+1;
     Buffer = new char[MemSize];
     OpenFile->Read(Buffer, MemSize);
     delete Buffer;
     delete OpenFile;

     HINSTANCE hIn;
     hIn = LoadLibrary("MakePDF417.dll");
     if(!hIn)
        ShowMessage("未发现comm.dll");
     make = (MAKE)GetProcAddress(hIn, "Make");
     make(Buffer, MemSize, "barcode.bmp", 3, 1,3, 3);
     FreeLibrary(hIn);
     Image1->Picture->LoadFromFile("barcode.bmp"); */

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
     OpenDialog1->InitialDir = CurrentDir + "\\bmp\\";
     if(!(FileExists(CurrentDir+"\\bmp\\barcode.bmp\\")))
     {
         CreateDir(CurrentDir+"\\bmp\\");
         //Memo2->Lines->LoadFromFile(CurrentDir+"\\barcode.bmp");
         //TMemoryStream * MyStream = new TMemoryStream();
         //MyStream->LoadFromFile(CurrentDir+"\\barcode.bmp");
         //MyStream->SaveToFile(CurrentDir+"\\bmp\\barcode.bmp");
         //delete MyStream;
     }
     //if(OpenDialog1->Execute())
     //{
     //     if(Memo2->Lines->Count >0) Memo2->Clear();
     //     SaveDialog1->FileName = OpenDialog1->FileName;
     //}
     SaveDialog1->InitialDir = CurrentDir + "\\bmp\\";
     SaveDialog1->FileName = "barcode.bmp";
     SaveDialog1->Filter = "位图文件 (*.bmp)|*.bmp|所有文件 (*.*)|*.*||";
     SaveDialog1->Title = "另存为";
     if(SaveDialog1->Execute())
     {
          //Memo2->Lines->SaveToFile(SaveDialog1->FileName);
          //Memo2->Lines->SaveToFile(CurrentDir+"\\bmp\\barcode.bmp\\");
          //TMemoryStream * MyStream = new TMemoryStream();
          //MyStream->LoadFromFile(CurrentDir+"\\barcode.bmp");
          //MyStream->SaveToFile(CurrentDir+"\\bmp\\barcode.bmp");
          TFileStream * MyFile = new TFileStream(CurrentDir+"\\barcode.bmp", fmOpenRead);
          TFileStream * NewFile = new TFileStream(CurrentDir+"\\bmp\\barcode.bmp", fmCreate);
          NewFile->CopyFrom(MyFile, MyFile->Size);
          //MyStream->CopyFrom(CurrentDir+"\\barcode.bmp\\", 12);
          delete MyFile;
          delete NewFile;
     }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)
{
     //QuickRep1->Preview();
}
//---------------------------------------------------------------------------


