5

我一直在编写一个程序时遇到一些问题,希望能得到一些帮助或输入。对于某些背景,我使用 Python 2.7 和 wxPython 来做一个流式网络摄像头客户端。客户端在自己的线程中从服务器获取图像,并将它们放入队列中。GUI 线程然后从队列中获取这些图像并将它们转换为wxBitmap对象。这每 0.5 秒发生一次,效果很好。我能够将wxBitmap对象保存为文件,因此我知道一切正常。

我遇到的问题实际上是让wxBitmap对象显示在我的 GUI 上。我似乎能够让 GUI 做的唯一一件事就是在网络摄像头图像应该出现的地方显示一个灰色矩形。

这是我onPaint()想刷新屏幕时调用的方法:

    def onPaint(self,e):
            ## this is the function that actually draws and redraws the window
            ## to be displayed. I think it is something similar to blit()
            ## in other graphical display frameworks
            print "in onPaint"

            ## create the device context object (graphics painter)
            dc = wx.PaintDC(self)
            dc.BeginDrawing()

            ## draw the bitmap to the screen
            dc.DrawBitmap(self.imageBit,0,0,True)
            dc.EndDrawing()            

            ## test code.
            ## the following works and updates, which means that
            ## everything is being converted properly and updated.
            ## not sure why the dc won't paint it to the window. 
            self.imageBit.SaveFile("bit.bmp", wx.BITMAP_TYPE_BMP)

简而言之,我不知道为什么它不起作用。根据我的研究,我发现因为我在 Windows 机器上我需要BeginDrawing()andEndDrawing()函数,所以我添加了它们。还是不行。没有抛出错误或异常。

其他可能有助于解决此问题的问题:

  • 我正在更新一个wxFrame对象。也许wxPaintDC需要在另一种类型的容器中运行才能工作?
  • ?

实际上,也许我的__init__功能是问题所在。我是否正确设置?

class viewWindow(wx.Frame):
    imgSizer = (480,360)
    def __init__(self, *args, **kw):
            ## this is called when an instance of this class is created
            super(viewWindow,self).__init__(*args,**kw)

            ## here is where the actual stuff inside the frame is set up.

            self.pnl = wx.Panel(self)

            ## create a button that opens up a Connection Window
            #test = wx.Button(self.pnl, label='Connection Settings')
            ## test.Bind(wx.EVT_BUTTON, self.openConnectionWindow)

            ## create the wxImage for the web cam pic
            self.image = wx.EmptyImage(self.imgSizer[0],self.imgSizer[1])

            ## create the wxBitmap so that the wxImage can be displayed
            self.imageBit = wx.BitmapFromImage(self.image)

            ## create a timer that will update the window based of frame rate
            self.timex = wx.Timer(self, wx.ID_OK)
            self.timex.Start(500)
            self.Bind(wx.EVT_TIMER, self.redraw, self.timex)

            ## need to do the following in order to display images in wxPython:
            self.Bind(wx.EVT_PAINT, self.onPaint)

            self.SetSize(self.imgSizer)
            self.SetTitle('View Window')
            self.Show()

无论如何,提前感谢您的帮助。

编辑:我通过删除该行意外地解决了这个问题self.pnl = wx.Panel(self)

显然它正在正确渲染,但位图位于面板下方。也许?我不太确定。我对整个 wxPython 的事情都是新手。

4

3 回答 3

1

此代码有效。它每次都显示图像。不过,它确实倾向于“闪烁”。因此,我可能不知道有更好的方法来做到这一点。

class viewWindow(wx.Frame):
    imgSizer = (480,360)
    def __init__(self, parent, title="View Window"):
            super(viewWindow,self).__init__(parent)
            ## create the menu and its sub trees
            menubar = wx.MenuBar()
            filemenu = wx.Menu()
            menubar.Append(filemenu, 'File')
            self.fitem = filemenu.Append(wx.ID_ANY, 'Open Connection Window')
            self.Bind(wx.EVT_MENU, self.openConnectionWindow, self.fitem)
            self.SetMenuBar(menubar)

            ## here is where the actual stuff inside the frame is set up.
            self.pnl = wx.Panel(self)
            self.vbox = wx.BoxSizer(wx.VERTICAL)

            ## create the wxImage for the web cam pic
            self.image = wx.EmptyImage(self.imgSizer[0],self.imgSizer[1])

            ## create the wxBitmap so that the wxImage can be displayed
            self.imageBit = wx.BitmapFromImage(self.image)
            self.staticBit = wx.StaticBitmap(self.pnl,wx.ID_ANY, self.imageBit)

            ## add the staticBit to the sizer so it is rendered properly on resizes and such
            ## note: not actually needed to get the image to display, but reccommended for ease
            ## of layout
            self.vbox.Add(self.staticBit)

            ## register the sizer with the panel so the panel knows to use it.
            self.pnl.SetSizer(self.vbox)

            ## create a timer that will update the window based on frame rate
            self.timex = wx.Timer(self, wx.ID_OK)
            self.timex.Start(1000/framerate)
            self.Bind(wx.EVT_TIMER, self.redraw, self.timex)

            ## set the size of the frame itself when it is first opened
            self.SetSize(self.imgSizer)
            self.Show()

    def openConnectionWindow(self, e):
            ## this will open a new connection window
            connect = connectionWindow(None)

    def redraw(self,e):
            ## this function updates the frame with the latest web cam image that has been
            ## retrieved by the client thread from the server.

            ## get the newest image in the queue 
            if not imgQ.empty():                        
                    picz = imgQ.get()
                    ## convert the image from a string to something usable (wxImage)
                    self.image.SetData(picz)
                    ## from wxImage to wxBitmap
                    self.imageBit = wx.BitmapFromImage(self.image)
                    self.staticBit = wx.StaticBitmap(self.pnl,wx.ID_ANY, self.imageBit)
                    ## refresh the frame
                    self.Refresh()
于 2012-08-27T23:24:53.237 回答
1

这似乎也是 wxPython 演示正在做的事情:dc.DrawBitmap。它适用于 Windows!至少,这是他们在 AlphaDrawing 演示中所做的。在 DrawImage 演示中,他们使用 dc.Blit()。你可以试试。

但是,我想知道您是否不能像我使用照片查看器那样做到这一点。我不使用 DC 进行绘制,而是使用我更新的 wx.StaticBitmap。

于 2012-08-27T14:32:26.763 回答
0

几个小时后,我研究了一个不同的问题,我发现:

如何检测两个 PIL 图像之间的运动?(包括 wxPython 网络摄像头集成示例)

其中包含运行良好的示例代码。

于 2012-08-28T01:12:54.220 回答