Re: WebCam with wxEuphoria
- Posted by raseunew Mar 27, 2011
- 1355 views
I have been able to successfully map from an SDL image buffer
to a wxWidgets buffer and display images.
See below for code snippets.
wxWidgets requires images to be in BGR format, where
the list of bytes is a single sequence buffer.
so, an image buffer of
image = {{{R,G,B},{R,G,B},{R,G,B},{R,G,B}}, {{R,G,B},{R,G,B},{R,G,B},{R,G,B}}, {{R,G,B},{R,G,B},{R,G,B},{R,G,B}}}
would need to be 'flattened'. I think eu4 standard library
has a flatten() function
if your opencv image is RGB it will
need to be converted.
--// Convert SDL surface from RGB to BGR public enum ofsRED = 1, ofsGREEN, ofsBLUE, ofsALPHA public function RGBtoBGR(sequence rgbbuf, integer bytesPerPixel=3) --// swap B with R sequence bgrbuf = {} sequence rgb = {} integer l = length(rgbbuf) for i = 1 to l by bytesPerPixel do rgb = rgbbuf[i..(i + (bytesPerPixel - 1))] bgrbuf &= { rgb[ofsBLUE], rgb[ofsGREEN], rgb[ofsRED] } end for --// return buffer atom bgr = allocate(length( rgbbuf )) poke(bgr, bgrbuf) return bgr end function
something similar to the following will display
a BGR buffer to a wxWidgets DC.
--// convert sdl surface into wx compatible buffer -- atom bgr = RGBtoBGR(sdlcanvas, 3) /* you would probably need to flatten your image here and create your bgr surface */ atom bgr = RGBtoBGR( flatten(openCVImage), 3) --// create image from converted bgr surface atom img = wx:create(wxImage, { openCVImageWidth, openCVImageHeight, bgr }) --// free converted bgr surface free(bgr) --// create bitmap from image atom bmp = wx:create(wxBitmap,{ BM_FROM_IMAGE, img, -1 }) --// update client DC atom clientDC = wx:create(wxClientDC, this) wx:begin_drawing(clientDC) wx:draw_bitmap(clientDC, bmp, 0, 0, true) wx:end_drawing(clientDC) --// clean up wx:delete_instance(clientDC) wx:delete_instance(bmp) wx:delete_instance(img)
HTH