[SDK de dispositivo BioStar 2] Cómo obtener el registro de imágenes

Creado por: Loberi Loest

Fecha de modificación: Sáb., May. 13, 2023 a las 4:05 A. M.


Si desea obtener el registro de imágenes a través de F2 o FSF2, podemos usar esta función y método.

Asegúrese de que compruebe la función BS2_Getimagelog.

 

Medio ambiente.

Estaba bien hasta el SDK 2.4.x, pero el problema ocurre con SDK 2.5.0 o una versión superior

 

Código de ejemplo

El orden es BS2_GetDeviceInfo  ->BS2_SetEventConfig -> BS2_Getimagelog


  

 private void button4_Click(object sender, EventArgs e)        {            IntPtr versionPtr = API.BS2_Version();            textBox1.Text = "SDK version : {0}" + Marshal.PtrToStringAnsi(versionPtr);
             IntPtr context = IntPtr.Zero;            context = API.BS2_AllocateContext();            if (context == null) //Returns NULL when there is not enough system memory            {
                textBox1.Text = "No memory!";            }            else // if not, returns the allocated Context.            {                textBox1.Text = "Allocate context successfully";
            }            BS2ErrorCode result = (BS2ErrorCode)API.BS2_Initialize(context);            if (result == BS2ErrorCode.BS_SDK_SUCCESS)            {                textBox1.Text = "Initialization succeeded";
            }            else            {                textBox1.Text = "failed to initialize error code";
            }            string ipAddress = textBox2.Text;// "192.168.13.195";            IntPtr ptrIPAddr = Marshal.StringToHGlobalAnsi(ipAddress);            ushort port = 51211;            uint deviceId = 0;
            result = (BS2ErrorCode)API.BS2_ConnectDeviceViaIP(context, ptrIPAddr, port, out deviceId);
            BS2AuthConfig authConfig;             if (result == BS2ErrorCode.BS_SDK_SUCCESS)            {                textBox1.Text = "Connecting to device succeeded";            }            else            {                textBox1.Text = "Connecting to device failed" + result;            }
 

            #region GetImageLog            //#region GetImageLog
            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);             result = (BS2ErrorCode)API.BS2_GetDeviceInfo(context, 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(context, 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(context, 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(context, 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(context, 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;
            result = (BS2ErrorCode)API.BS2_GetLog(context, deviceId, 0, 1024, out outEventLogObjs, out outNumEventLogs);            if (result != BS2ErrorCode.BS_SDK_SUCCESS)            {                Console.WriteLine("Got error({0}).", result);                return;            }
            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(context, 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);            }            result = (BS2ErrorCode)API.BS2_GetEventConfig(context, deviceId, out eventConfig);            if (result != BS2ErrorCode.BS_SDK_SUCCESS)            {                textBox1.Text = "Get Error" + result;                return;            }
            result = (BS2ErrorCode)API.BS2_SetEventConfig(context, deviceId, ref eventConfig);            if (result != BS2ErrorCode.BS_SDK_SUCCESS)            {                textBox1.Text = "Get Error" + result;                return;            }
            //#endregion            #endregion
        }


L
Loberi es el autor de este artículo de solución.

¿Le resultó útil? No

Enviar comentarios
Lamentamos no haberle podido ayudar más. Sus comentarios nos ayudarían a mejorar este artículo.