0

I'm struggling with HikVision SDK, trying to get it work in Delphi. There's some samples in C++, but I've stuck with pointers issues while translating to Delphi. Till that moment code seems to work, as I get correct plate number.
Is this correct equivalent: BYTE *pBuffer -> PByte ?
How to extract picture from pBuffer?

C++ code:

typedef struct tagNET_ITS_PICTURE_INFO
{
    DWORD   dwDataLen;            //Media data length
    BYTE    byType;                
    BYTE    byAbsTime[32];
    NET_VCA_RECT   struPlateRect;
    BYTE    *pBuffer;              //Data pointer
    DWORD   dwUTCTime;
    //...
}NET_ITS_PICTURE_INFO, *LPNET_ITS_PICTURE_INFO;


typedef struct tagNET_ITS_PLATE_RESULT
{
    //...
    NET_DVR_PLATE_INFO  struPlateInfo;
    NET_ITS_PICTURE_INFO struPicInfo[6];
}NET_ITS_PLATE_RESULT, *LPNET_ITS_PLATE_RESULT;


void CALLBACK MSesGCallback(LONG lCommand, NET_DVR_ALARMER *pAlarmer, char *pAlarmInfo, DWORD dwBufLen, void* pUser)
{
    char filename[100];
    FILE *fSnapPic = NULL;

    switch (lCommand) {

        case COMM_ITS_PLATE_RESULT: {
            NET_ITS_PLATE_RESULT struITSPlateResult = { 0 };
            memcpy(&struITSPlateResult, pAlarmInfo, sizeof(struITSPlateResult));

            printf("Plate: %s\n", struITSPlateResult.struPlateInfo.sLicense);
            if (struITSPlateResult.struPicInfo[0].dwDataLen != 0)
            {
                sprintf(filename, "./pic/%d.jpg", 0);
                fSnapPic = fopen(filename, "wb");
                fwrite(struITSPlateResult.struPicInfo[0].pBuffer, struITSPlateResult.struPicInfo[0].dwDataLen, 1, fSnapPic);
                fclose(fSnapPic);
            }
        }
    }
    return;
}

Delphi code:

  LPNET_ITS_PICTURE_INFO = ^NET_ITS_PICTURE_INFO;
  NET_ITS_PICTURE_INFO = record
    dwDataLen: DWORD;
    byType: BYTE;
    byAbsTime: array [0..31] of BYTE;
    struPlateRect: NET_VCA_RECT;
    pBuffer: PByte;                  // ????????
    dwUTCTime: DWORD;
    //(...)
  end;


  LPNET_ITS_PLATE_RESULT = ^NET_ITS_PLATE_RESULT;
  NET_ITS_PLATE_RESULT = record
    //(...)
    struPlateInfo: NET_DVR_PLATE_INFO;
    struPicInfo: Array [0..5] of NET_ITS_PICTURE_INFO;
  end;


procedure MessageCallBack(lCommand:longint; pAlarmer:LPNET_DVR_ALARMER; pAlarmInfo:PAnsiChar; dwBufLen:LongInt; pUser:pointer); stdcall;
var
 struAlarmInfo: LPNET_DVR_ALARMINFO_V30;
 struPlateResult: LPNET_ITS_PLATE_RESULT;
begin
  case lCommand of
    COMM_ITS_PLATE_RESULT:
      begin
        New(struPlateResult);
        FillChar(struPlateResult^, SizeOf(NET_ITS_PLATE_RESULT), 0);
        Move(pAlarmInfo^, struPlateResult^, Sizeof(NET_ITS_PLATE_RESULT));
        Form1.memoOut.Lines.Add('sLicense: ' + struPlateResult.struPlateInfo.sLicense);

        if (struPlateResult.struPicInfo[0].dwDataLen >0) then
          ??? how to get the picture from struPlateResult.struPicInfo[0].pBuffer ?

      end;
  end;
end;

Regards

tomek
  • 43
  • 1
  • 5
  • 1
    See: https://stackoverflow.com/questions/100596/best-resources-for-converting-c-c-dll-headers-to-delphi – Brian Jan 16 '19 at 15:18
  • 1
    Don't use `New`. If you need to allocate a record, use a local variable. If you need a pointer to that record, use `@` to get its address. It is more complicated to have to manage heap allocations that to use automatic allocation on the stack. As witnessed by your code leaking that allocation. – David Heffernan Jan 16 '19 at 15:37
  • In fact you don't need to copy the record. You can cast the pointer. The C++ code is wasteful in that regard too. – David Heffernan Jan 16 '19 at 15:52
  • if you meant `struPlateResult := LPNET_ITS_PLATE_RESULT(pAlarmInfo);` instead of `New/FillChar/Move` then it works too :) – tomek Jan 17 '19 at 08:38

1 Answers1

1

Yes, PByte or PByteArray is correct translation.

If buffer contains valid file contents, you can save it into file:

FS := TFileStream.Create('test.jpg', fmCreate);
try
   FS.Write(struITSPlateResult.struPicInfo[0].pBuffer^,
            struITSPlateResult.struPicInfo[0].dwDataLen);
finally
  FS.Free;
end;
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
MBo
  • 77,366
  • 5
  • 53
  • 86