차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

다음 판
이전 판
ko:bs2_getimagelog [2018/10/31 13:03]
kkshin 만듦
ko:bs2_getimagelog [2021/12/30 13:02] (현재)
mark [반환값]
줄 27: 줄 27:
 ==== 반환값 ==== ==== 반환값 ====
 성공적으로 수행될 경우 ''​BS_SDK_SUCCESS''​를 반환하고,​ 에러가 발생할 경우 상응하는 에러 코드를 반환합니다. 성공적으로 수행될 경우 ''​BS_SDK_SUCCESS''​를 반환하고,​ 에러가 발생할 경우 상응하는 에러 코드를 반환합니다.
 +C++
 +<code cpp>
 +int getImageLog(void* context, BS2_DEVICE_ID id, BS2_EVENT_ID eventID, uint8_t* imageBuf, uint32_t&​ imageSize)
 +{
 + if (!imageBuf)
 + return BS_SDK_ERROR_NULL_POINTER;​
 +
 + uint8_t* imageObj = NULL;
 + uint32_t size(0);
 + int sdkResult = BS2_GetImageLog(context,​ id, eventID, &​imageObj,​ &size);
 + if (BS_SDK_SUCCESS == sdkResult)
 + {
 + memcpy(imageBuf,​ imageObj, size);
 + imageSize = size;
 + if (imageObj)
 + BS2_ReleaseObject(imageObj);​
 + }
 +
 + return sdkResult;
 +}
 +</​code>​
 +
 +C#
 +<code cpp>
 +void getImageLog(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice)
 +{
 +    BS2SimpleDeviceInfo deviceInfo;
 +    int structSize = Marshal.SizeOf(typeof(BS2Event));​
 +    UInt16 imageLogEventCode = (UInt16)BS2EventCodeEnum.DEVICE_TCP_CONNECTED;​
 +    BS2EventConfig eventConfig = Util.AllocateStructure<​BS2EventConfig>​();​
 +    eventConfig.numImageEventFilter = 1;
 +    eventConfig.imageEventFilter[0].mainEventCode = (byte)(imageLogEventCode >> 8);
 +    eventConfig.imageEventFilter[0].scheduleID = (UInt32)BS2ScheduleIDEnum.ALWAYS;​
 +
 +    Console.WriteLine("​Trying to get the device[{0}] information.",​ deviceID);
 +    BS2ErrorCode result = (BS2ErrorCode)API.BS2_GetDeviceInfo(sdkContext,​ deviceID, out deviceInfo);​
 +    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
 +    {
 +        Console.WriteLine("​Can'​t get device information(errorCode : {0}).",​ result);
 +        return;
 +    }
 +
 +    Console.WriteLine("​Trying to activate image log."​);​
 +    result = (BS2ErrorCode)API.BS2_SetEventConfig(sdkContext,​ deviceID, ref eventConfig);​
 +    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
 +    {
 +        Console.WriteLine("​Got error({0}).",​ result);
 +        return;
 +    }
 +
 +    Console.WriteLine("​Trying to clear log for quick test."​);​
 +    result = (BS2ErrorCode)API.BS2_ClearLog(sdkContext,​ deviceID);
 +    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
 +    {
 +        Console.WriteLine("​Got error({0}).",​ result);
 +        return;
 +    }
 +
 +    Console.WriteLine("​Trying to disconnect device[{0}] for quick test.",​ deviceID);
 +    result = (BS2ErrorCode)API.BS2_DisconnectDevice(sdkContext,​ deviceID);
 +    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
 +    {
 +        Console.WriteLine("​Got error({0}).",​ result);
 +        return;
 +    }
 +
 +    Thread.Sleep(500);​ //waiting for socket close
 +
 +    Console.WriteLine("​Trying to connect device[{0}].",​ deviceID);
 +    IntPtr ptrIPAddr = Marshal.StringToHGlobalAnsi(new IPAddress(BitConverter.GetBytes(deviceInfo.ipv4Address)).ToString());​
 +    //result = (BS2ErrorCode)API.BS2_ConnectDeviceViaIP(sdkContext,​ new IPAddress(BitConverter.GetBytes(deviceInfo.ipv4Address)).ToString(),​ deviceInfo.port,​ out deviceID);
 +    result = (BS2ErrorCode)API.BS2_ConnectDeviceViaIP(sdkContext,​ ptrIPAddr, deviceInfo.port,​ out deviceID);
 +    Marshal.FreeHGlobal(ptrIPAddr);​
 +    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
 +    {
 +        Console.WriteLine("​Got error({0}).",​ result);
 +        return;
 +    }
 +
 +    IntPtr outEventLogObjs = IntPtr.Zero;​
 +    UInt32 outNumEventLogs = 0;
 +
 +    if (outNumEventLogs > 0)
 +    {
 +        IntPtr curEventLogObjs = outEventLogObjs;​
 +        for (int idx = 0; idx < outNumEventLogs;​ idx++)
 +        {
 +            BS2Event eventLog = (BS2Event)Marshal.PtrToStructure(curEventLogObjs,​ typeof(BS2Event));​
 +            //if (Convert.ToBoolean(eventLog.image))
 +            bool hasImage = Convert.ToBoolean(eventLog.image & (byte)BS2EventImageBitPos.BS2_IMAGEFIELD_POS_IMAGE);​
 +            if (hasImage)
 +            {
 +                Console.WriteLine("​Trying to get image log[{0}].",​ eventLog.id);​
 +
 +                IntPtr imageObj = IntPtr.Zero;​
 +                UInt32 imageSize = 0;
 +
 +                result = (BS2ErrorCode)API.BS2_GetImageLog(sdkContext,​ deviceID, eventLog.id,​ out imageObj, out imageSize);
 +                if (result != BS2ErrorCode.BS_SDK_SUCCESS)
 +                {
 +                    Console.WriteLine("​Got error({0}).",​ result);
 +                }
 +                else
 +                {
 +                    int written = 0;
 +                    FileStream file = new FileStream(String.Format("​{0}.jpg",​ eventLog.id),​ FileMode.Create,​ FileAccess.Write);​
 +
 +                    Console.WriteLine("​Trying to save image log[{0}].",​ eventLog.id);​
 +                    WriteFile(file.Handle,​ imageObj, (int)imageSize,​ out written, IntPtr.Zero);​
 +                    file.Close();​
 +
 +                    if (written != imageSize)
 +                    {
 +                        Console.WriteLine("​Got error({0}).",​ result);
 +                    }
 +                    else
 +                    {
 +                        Console.WriteLine("​Successfully saved the image log[{0}].",​ eventLog.id);​
 +                        Process.Start(file.Name);​
 +                    }
 +                }
 +                break;
 +            }
 +
 +            curEventLogObjs = (IntPtr)((long)curEventLogObjs + structSize);​
 +        }
 +
 +        API.BS2_ReleaseObject(outEventLogObjs);​
 +    }
 +
 +    eventConfig.numImageEventFilter = 0;
 +
 +    Console.WriteLine("​Trying to deactivate image log."​);​
 +    result = (BS2ErrorCode)API.BS2_SetEventConfig(sdkContext,​ deviceID, ref eventConfig);​
 +    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
 +    {
 +        Console.WriteLine("​Got error({0}).",​ result);
 +        return;
 +    }
 +}
 +
 +</​code>​