0
votes

I need to implement in Delphi 2006 an algoritm that work in .net

Basicaly I need to do the next steps:

  1. Create an XML and validate aganist the XSD
  2. Serialize the XML string into an array of bytes UTF-8 encoded and compress it with zip
  3. The compressed info must be stored again into a array of bytes using base256 format
  4. Create an image using Datamatrix 2D BarCode from this array of bytes and put this image on a report

For step 1, I create the XML using NativeXML library that work ok. In this library exist a metod SaveToBinaryFile but don't work ok. In my tests I used this function to create a binary file. I was forced to use a binary file becouse my Zip component work only with files not with strings or aray of bytes from memory.

I compressed this binary file with the Zip component and loaded this compresed file into a blob file. At the moment when I need to create the DataMatrix image I load this blob file into an ansistring and I create the image.

After many tests I found that my fault is when I save my XML into the binary file. Now I need to found another way to save my xml (utf-8) string to a binarry file. Please sorry for my english. Can anyone help me?

1
You can omit the step of converting to binary completely. When you store the file with TNativeXML.SaveToFile it is already UTF-8 encoded unless you didn't specify another encoding. - Uwe Raabe
yes but the result is not ok. The DataMatrix image can't be converted again to xml from app that are created with other languages - sorin
when I use SavetoBinaryfile some parts of the XML can be obtained from the DataMatrix image. - sorin
The built-in zip component can save to a stream rather than a file - David Heffernan
As for the details of solving your problem, it's pretty much impossible for us to help without you telling us some details. At the moment we have "After many tests I found that my fault is when I save my XML into the binary file" and that's really not anywhere close to enough detail. - David Heffernan

1 Answers

0
votes
    procedure CreateXMLBarcodeș
    var
     BinarySize: Integer;
     InputString: utf8string;
     StringAsBytes: array of Byte;
     F : FIle of Byte;
     pTemp : Pointer;


    begin
     InputString := '';
     ADoc := TNativeXml.CreateName(rootName);
     try
       ... //create the XML
       InputString := ADoc.WriteToLocalString; //utf8string
       if InputString > '' then begin
          //serialize the XML string to array of bytes
          BinarySize := (Length(InputString) + 1) * SizeOf(Char);
          SetLength(StringAsBytes, BinarySize);
          Move(InputString[1], StringAsBytes[0], BinarySize);
          //save the array of bytes to file
          AssignFile( F, xmlFilename );
          Rewrite(F);
          try
           BinarySize := Length( StringAsBytes );
           pTemp := @StringAsBytes[0];
           BlockWrite(F, pTemp^, BinarySize );
          finally
           CloseFile( F );
          end;
       end;
       finally
        ADoc.Free;
       end;
     end

    ...
    //in other procedure:
         DataSet1XMLBarCode.LoadFromFile(CreateXMLBarcode);
    ...

    //when I want to create the report
    //create the DataMatrix image
    procedure xyz(Sender: TfrxReportComponent);
    var  AWidth, AHeight: Integer;
     function GetStringFromBlob: AnsiString;
     var BlobStream: TStream;
     begin
      Result := '';
      DataSet.Open;
      BlobStream := DataSet1.
                       CreateBlobStream(DataSet1XMLBarCode, bmRead);
      try
       SetLength(Result, BlobStream.Size);
       BlobStream.Position := 0;
       BlobStream.Read(Result[1], BlobStream.Size);
      finally
       BlobStream.Free;
      end;
      DataSet.Close;
     end;
    begin
     Barcode2D_DataMatrixEcc2001.Locked := True;
     Barcode2D_DataMatrixEcc2001.Barcode := GetStringFromBlob;
     Barcode2D_DataMatrixEcc2001.Module := 1;
     Barcode2D_DataMatrixEcc2001.DrawToSize(AWidth, AHeight);
     with TfrxPictureView(frxReport1.FindObject('Picture1')).Picture.Bitmap do
     begin
      Width := AWidth;
      Height := AHeight;
      Barcode2D_DataMatrixEcc2001.DrawTo(Canvas, 0, 0);
     end;
     Barcode2D_DataMatrixEcc2001.Locked := False;
    end;

    //the code was 'çopied'and adapted from the internet