
| 语言: | Visual Basic 6 |
| 版本: | 3.0.3 |
| 发布于: | 2005年4月7日 |
| 作者: | IC Imaging Control 技术支持部 |
| 系统要求: | IC Imaging Control >2.1 由WDM数据流类驱动程序驱动的相机、视频转换器或图像采集卡 |
| |
下面的Visual Basic示例代码将演示如何使用IC Imaging Control 从视频设备中读取实时图像数据流、 写入缓存、同时显示鼠标所在点的RGB值。
示例程序的窗口如图:

程序的第一条语句用来激活内置对话框(.ShowDeviceSettingsDialog),从而选择视频设备。 而后,使用.LiveStart开始传入设备发来的视频数据流。因为.LiveCaptureContinuous = True, 图像数据流将同时被写入内部环形缓存。.MemoryCurrentGrabberColorformat = ICRGB24语句确保图像数据 为RGB24格式:
Private Sub Form_Load() ICImagingControl1.ShowDeviceSettingsDialog If Not ICImagingControl1.DeviceValid Then Unload Me Exit Sub End If ICImagingControl1.LiveCaptureContinuous = True ICImagingControl1.MemoryCurrentGrabberColorformat = ICRGB24 ICImagingControl1.LiveStart End Sub
用户移动鼠标时,程序新建图像缓存引用ib,将其设为环形缓存中的当前图像(ImageBuffers.CurrentIndex), 同时锁定图像。如果鼠标在图像内,GetImageColorValueRGB24 将读取当前像素的RGB值,而后写入对话框中:
Private Sub ICImagingControl1_MouseMove(ByVal Button As Long, ByVal Shift As Long, ByVal XPos As Integer, ByVal YPos As Integer) lblCoords.Caption = XPos & "," & YPos Dim ib As ImageBuffer Set ib = ICImagingControl1.ImageBuffers(ICImagingControl1.ImageBuffers.CurrentIndex) ib.Lock If XPos < ib.PixelPerLine And YPos < ib.Lines Then Dim red As Byte, green As Byte, blue As Byte GetImageColorValueRGB24 ib, XPos, YPos, red, green, blue txtImageData.Text = "Red" & vbTab & "= " & red & vbCrLf & _ "Green" & vbTab & "= " & green & vbCrLf & _ "Blue" & vbTab & "= " & blue Else txtImageData.Text = "<Outside of image>" End If ib.Unlock End Sub
GetImageColorValueRGB24 使用图像缓存引用ib 读取当前像素。请注意在RGB24图像中,其数据存储顺序正好与图像 显示上下颠倒,即 0 行中的数据实际是图像最底行的数据,1行的数据是图象中自下而上的第二行中的像素数据。 第一列存储第一个像素的蓝色分量值,第二个存储绿色、第三个存储红色。以此类推,第四列存储的是第二个像素的蓝色值:
Private Sub GetImageColorValueRGB24(ib As ImageBuffer, x As Integer, y As Integer, ByRef red As Byte, ByRef green As Byte, ByRef blue As Byte) Dim imageData As Variant imageData = ib.GetImageData Dim indexXred As Integer Dim indexXgreen As Integer Dim indexXblue As Integer Dim indexY As Integer indexXred = x * 3 + 2 indexXgreen = x * 3 + 1 indexXblue = x * 3 indexY = ib.Lines - y - 1 red = imageData(indexXred, indexY) green = imageData(indexXgreen, indexY) blue = imageData(indexXblue, indexY) ib.ReleaseImageData imageData End Sub
责任声明
IC Imaging Control 源代码库中的所有代码均只用于教学目的,The Imaging Source Europe GmbH 作为IC Imaging Control的开发制造商,不对任何由于使用本文或其中源代码所产生的后果承担责任。