BREW 中使用内存中的JPEG创建图像
我使用socket收到了数据流,里面的数据是JPEG格式的,现在想把它直接显示出来,使用了IMEMASTREAM_Set()接口和IIMAGE_SetStream()接口及IIMAGE_Draw()接口,代码如下:
/*图像的每一帧都以0101 0101 1010 1010为开始的标志*开始标志位后面的一个字节表示帧的长度*然后是帧的内容(主体)*帧内容(主体)后面的一个字节是校验码,由帧长度与 0x00F0 与而获得*/static void HandleRecvData(eMonitor * pMe){ IImage * pIImage = NULL; IMemAStream * pIStream = NULL; int nFontHeight = 0; byte * pFrameBuffer = NULL; //帧缓冲区 byte * pFrame = NULL; //帧缓冲区的指针 byte * pBuffer = NULL; //当前指针位置 int nFrameIndex1; int nFrameIndex2; int nParity = 0; //校验位的值 int nFrameLength = 0; //帧大小 int i = 0; //循环变量 nFontHeight = IDisplay_GetFontMetrics(pMe->a.m_pIShell, AEE_FONT_NORMAL, NULL, NULL); //建立处理JPEG图片格式的 IImage 接口 if (ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_JPEG, (void **)&pIImage) != SUCCESS) { DBGPRINTF("Create IImage fall"); return; } //MEMCPY(pBuffer, pData, size); //建立处理流的 IMenAStream 接口 if (ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_MEMASTREAM, (void **)&pIStream) != SUCCESS) { DBGPRINTF("Create IStream fall"); return; } //MEMCPY(pMe->m_pbDisplayBuffer, pMe->m_pbRecvBuffer, pMe->m_nRecvSize); //pBufferCurrent = pMe->m_pbDisplayBuffer; pBuffer = pMe->m_pbRecvBuffer; //0101 0101 1010 1010表示一帧图片的开始,后面一个byte表示一帧画面的长度 while (pBuffer != NULL) { nFrameIndex1 = * pBuffer; //nFrameIndex1 = GetHigh8Bits(* pBufferCurrent); nFrameIndex2 = *(++ pBuffer); //nFrameIndex2 = GetHigh8Bits(nFrameIndex2); //DBGPRINTF("Buffer Current: %x", pBufferCurrent); //if (nFrameIndex1 == 0x55 || nFrameIndex2 == 0xaa ) if ( nFrameIndex2 == 0xaa ) { ++ pBuffer; nFrameLength = * pBuffer; //获取帧的长度 //将图片缓冲的大小设置为图片的长度 pFrameBuffer = (byte *)MALLOC(nFrameLength * sizeof(byte)); //从 pBuffer 的后一个位置开始,复制 nFrameLength个byte到pFrameBuffer pFrame = pBuffer ++; MEMCPY(pFrameBuffer, pFrame, nFrameLength * sizeof(byte)); pFrame += nFrameLength; //pBuffer 指针后移 nFrameLength 个字节 nParity = * pFrame; //获取校验位 if ((nFrameLength & 0x00F0) == nParity) { //校验正确,显示 DBGPRINTF("success to get new frame image, size is %d", nFrameLength); //格式化数据为存储流 IMEMASTREAM_Set(pIStream, pFrameBuffer, nFrameLength, 0, FALSE); //把流设置为图片数据 //IIMAGE_SetStream()函数使 IAStream 接口与 IImage 接口对象关联,以使图像数据从文件或套接字流入 IIMAGE_SetStream(pIImage, pIStream); /*IIMAGE_SetStream(pIImage, pFrameBuffer);*/ IIMAGE_Draw(pIImage, 0, nFontHeight + 10); IMEMASTREAM_Release(pIStream); //释放变量 FREEIF(pFrameBuffer); } else { //丢弃该帧 FREEIF(pFrameBuffer); } } pBuffer ++; //缓冲区指针继续往后移动 i ++; /*if (i == 31) { DBGPRINTF("***%x", pBufferCurrent); DBGPRINTF("Find times %d", i); return; }*/ } DBGPRINTF("***%x", pBuffer); DBGPRINTF("Find times %d", i);}