Imports System.Drawing Imports System.Drawing.Image
'--------------Imports System.Threading-----------------------------------
'--------Code By Ken Tucker-------------
'-------------------------------------------------
Public Class Form1 Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New() MyBase.New()
'This call is required by the Windows Form Designer. InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub
'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents picCapture As System.Windows.Forms.PictureBox Friend WithEvents lstDevices As System.Windows.Forms.ListBox Friend WithEvents lblDevice As System.Windows.Forms.Label Friend WithEvents btnStart As System.Windows.Forms.Button Friend WithEvents btnSave As System.Windows.Forms.Button Friend WithEvents btnStop As System.Windows.Forms.Button Friend WithEvents PicB1 As System.Windows.Forms.PictureBox Friend WithEvents Timer1 As System.Windows.Forms.Timer Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox Friend WithEvents TrackBar1 As System.Windows.Forms.TrackBar Friend WithEvents TrackBar2 As System.Windows.Forms.TrackBar Friend WithEvents TrackBar4 As System.Windows.Forms.TrackBar Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox Public WithEvents TrackBar3 As System.Windows.Forms.TrackBar <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.components = New System.ComponentModel.Container Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1)) Me.picCapture = New System.Windows.Forms.PictureBox Me.lstDevices = New System.Windows.Forms.ListBox Me.lblDevice = New System.Windows.Forms.Label Me.btnStart = New System.Windows.Forms.Button Me.btnSave = New System.Windows.Forms.Button Me.btnStop = New System.Windows.Forms.Button Me.PicB1 = New System.Windows.Forms.PictureBox Me.Timer1 = New System.Windows.Forms.Timer(Me.components) Me.GroupBox1 = New System.Windows.Forms.GroupBox Me.TrackBar2 = New System.Windows.Forms.TrackBar Me.TrackBar1 = New System.Windows.Forms.TrackBar Me.TrackBar3 = New System.Windows.Forms.TrackBar Me.Label4 = New System.Windows.Forms.Label Me.Label3 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.Label1 = New System.Windows.Forms.Label Me.TrackBar4 = New System.Windows.Forms.TrackBar Me.GroupBox2 = New System.Windows.Forms.GroupBox Me.GroupBox3 = New System.Windows.Forms.GroupBox Me.GroupBox1.SuspendLayout() CType(Me.TrackBar2,System.ComponentModel.ISupportInitialize).BeginInit() CType(Me.TrackBar1,System.ComponentModel.ISupportInitialize).BeginInit() CType(Me.TrackBar3,System.ComponentModel.ISupportInitialize).BeginInit() CType(Me.TrackBar4,System.ComponentModel.ISupportInitialize).BeginInit() Me.GroupBox2.SuspendLayout() Me.GroupBox3.SuspendLayout() Me.SuspendLayout() ' 'picCapture ' Me.picCapture.BackColor = System.Drawing.Color.FromArgb(CType(255,Byte),CType(128,CType(0,Byte)) Me.picCapture.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.picCapture.Location = New System.Drawing.Point(16,16) Me.picCapture.Name = "picCapture" Me.picCapture.Size = New System.Drawing.Size(16,16) Me.picCapture.TabIndex = 0 Me.picCapture.TabStop = False ' 'lstDevices ' Me.lstDevices.BackColor = System.Drawing.SystemColors.InactiveCaptionText Me.lstDevices.ItemHeight = 12 Me.lstDevices.Location = New System.Drawing.Point(8,40) Me.lstDevices.Name = "lstDevices" Me.lstDevices.Size = New System.Drawing.Size(152,28) Me.lstDevices.TabIndex = 1 ' 'lblDevice ' Me.lblDevice.Location = New System.Drawing.Point(16,16) Me.lblDevice.Name = "lblDevice" Me.lblDevice.Size = New System.Drawing.Size(80,18) Me.lblDevice.TabIndex = 2 Me.lblDevice.Text = "选择视频设备" Me.lblDevice.TextAlign = System.Drawing.ContentAlignment.TopCenter ' 'btnStart ' Me.btnStart.Location = New System.Drawing.Point(48,16) Me.btnStart.Name = "btnStart" Me.btnStart.Size = New System.Drawing.Size(96,24) Me.btnStart.TabIndex = 3 Me.btnStart.Text = "开始采集图像" ' 'btnSave ' Me.btnSave.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right),System.Windows.Forms.AnchorStyles) Me.btnSave.Location = New System.Drawing.Point(48,88) Me.btnSave.Name = "btnSave" Me.btnSave.Size = New System.Drawing.Size(96,24) Me.btnSave.TabIndex = 4 Me.btnSave.Text = "保存图像" ' 'btnStop ' Me.btnStop.Location = New System.Drawing.Point(48,48) Me.btnStop.Name = "btnStop" Me.btnStop.Size = New System.Drawing.Size(96,24) Me.btnStop.TabIndex = 5 Me.btnStop.Text = "停止采集图像" ' 'PicB1 ' Me.PicB1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.PicB1.Location = New System.Drawing.Point(200,16) Me.PicB1.Name = "PicB1" Me.PicB1.Size = New System.Drawing.Size(224,200) Me.PicB1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage Me.PicB1.TabIndex = 6 Me.PicB1.TabStop = False ' 'Timer1 ' Me.Timer1.Enabled = True Me.Timer1.Interval = 125 ' 'GroupBox1 ' Me.GroupBox1.Controls.Add(Me.TrackBar2) Me.GroupBox1.Controls.Add(Me.TrackBar1) Me.GroupBox1.Controls.Add(Me.TrackBar3) Me.GroupBox1.Controls.Add(Me.Label4) Me.GroupBox1.Controls.Add(Me.Label3) Me.GroupBox1.Controls.Add(Me.Label2) Me.GroupBox1.Controls.Add(Me.Label1) Me.GroupBox1.Controls.Add(Me.TrackBar4) Me.GroupBox1.ForeColor = System.Drawing.SystemColors.Desktop Me.GroupBox1.Location = New System.Drawing.Point(16,216) Me.GroupBox1.Name = "GroupBox1" Me.GroupBox1.Size = New System.Drawing.Size(408,112) Me.GroupBox1.TabIndex = 11 Me.GroupBox1.TabStop = False ' 'TrackBar2 ' Me.TrackBar2.LargeChange = 2 Me.TrackBar2.Location = New System.Drawing.Point(48,64) Me.TrackBar2.Maximum = 50 Me.TrackBar2.Minimum = -50 Me.TrackBar2.Name = "TrackBar2" Me.TrackBar2.Size = New System.Drawing.Size(144,45) Me.TrackBar2.TabIndex = 1 Me.TrackBar2.TickFrequency = 0 Me.TrackBar2.TickStyle = System.Windows.Forms.TickStyle.None ' 'TrackBar1 ' Me.TrackBar1.BackColor = System.Drawing.SystemColors.Control Me.TrackBar1.Cursor = System.Windows.Forms.Cursors.Hand Me.TrackBar1.LargeChange = 2 Me.TrackBar1.Location = New System.Drawing.Point(48,24) Me.TrackBar1.Maximum = 50 Me.TrackBar1.Minimum = -50 Me.TrackBar1.Name = "TrackBar1" Me.TrackBar1.RightToLeft = System.Windows.Forms.RightToLeft.Yes Me.TrackBar1.Size = New System.Drawing.Size(144,45) Me.TrackBar1.SmallChange = 5 Me.TrackBar1.TabIndex = 0 Me.TrackBar1.TickFrequency = 0 Me.TrackBar1.TickStyle = System.Windows.Forms.TickStyle.None ' 'TrackBar3 ' Me.TrackBar3.Location = New System.Drawing.Point(256,64) Me.TrackBar3.Maximum = 50 Me.TrackBar3.Minimum = -50 Me.TrackBar3.Name = "TrackBar3" Me.TrackBar3.Size = New System.Drawing.Size(144,45) Me.TrackBar3.TabIndex = 3 Me.TrackBar3.TickFrequency = 0 Me.TrackBar3.TickStyle = System.Windows.Forms.TickStyle.None ' 'Label4 ' Me.Label4.Location = New System.Drawing.Point(216,64) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(40,23) Me.Label4.TabIndex = 7 Me.Label4.Text = "亮度" ' 'Label3 ' Me.Label3.Location = New System.Drawing.Point(216,24) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(24,23) Me.Label3.TabIndex = 6 Me.Label3.Text = "蓝" ' 'Label2 ' Me.Label2.Location = New System.Drawing.Point(16,64) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(32,23) Me.Label2.TabIndex = 5 Me.Label2.Text = "绿" ' 'Label1 ' Me.Label1.Location = New System.Drawing.Point(16,24) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(16,23) Me.Label1.TabIndex = 4 Me.Label1.Text = "红" ' 'TrackBar4 ' Me.TrackBar4.BackColor = System.Drawing.SystemColors.Control Me.TrackBar4.Cursor = System.Windows.Forms.Cursors.Hand Me.TrackBar4.LargeChange = 2 Me.TrackBar4.Location = New System.Drawing.Point(256,24) Me.TrackBar4.Maximum = 50 Me.TrackBar4.Minimum = -50 Me.TrackBar4.Name = "TrackBar4" Me.TrackBar4.Size = New System.Drawing.Size(144,45) Me.TrackBar4.SmallChange = 2 Me.TrackBar4.TabIndex = 2 Me.TrackBar4.TickFrequency = 0 Me.TrackBar4.TickStyle = System.Windows.Forms.TickStyle.None ' 'GroupBox2 ' Me.GroupBox2.Controls.Add(Me.btnStart) Me.GroupBox2.Controls.Add(Me.btnStop) Me.GroupBox2.Controls.Add(Me.btnSave) Me.GroupBox2.Controls.Add(Me.picCapture) Me.GroupBox2.ForeColor = System.Drawing.SystemColors.Desktop Me.GroupBox2.Location = New System.Drawing.Point(16,88) Me.GroupBox2.Name = "GroupBox2" Me.GroupBox2.Size = New System.Drawing.Size(168,128) Me.GroupBox2.TabIndex = 12 Me.GroupBox2.TabStop = False ' 'GroupBox3 ' Me.GroupBox3.Controls.Add(Me.lblDevice) Me.GroupBox3.Controls.Add(Me.lstDevices) Me.GroupBox3.ForeColor = System.Drawing.SystemColors.Desktop Me.GroupBox3.Location = New System.Drawing.Point(16,8) Me.GroupBox3.Name = "GroupBox3" Me.GroupBox3.Size = New System.Drawing.Size(168,80) Me.GroupBox3.TabIndex = 13 Me.GroupBox3.TabStop = False ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(6,14) Me.ClientSize = New System.Drawing.Size(432,358) Me.Controls.Add(Me.GroupBox3) Me.Controls.Add(Me.GroupBox2) Me.Controls.Add(Me.PicB1) Me.Controls.Add(Me.GroupBox1) Me.Icon = CType(resources.GetObject("$this.Icon"),System.Drawing.Icon) Me.Name = "Form1" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "照相采集程序" Me.GroupBox1.ResumeLayout(False) CType(Me.TrackBar2,System.ComponentModel.ISupportInitialize).EndInit() CType(Me.TrackBar1,System.ComponentModel.ISupportInitialize).EndInit() CType(Me.TrackBar3,System.ComponentModel.ISupportInitialize).EndInit() CType(Me.TrackBar4,System.ComponentModel.ISupportInitialize).EndInit() Me.GroupBox2.ResumeLayout(False) Me.GroupBox3.ResumeLayout(False) Me.ResumeLayout(False)
End Sub
#End Region
Const WM_CAP As Short = &H400S
Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10 Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11 Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 100 Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52 Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53 Const WS_CHILD As Integer = &H40000000 Const WS_VISIBLE As Integer = &H10000000 Const SWP_NOMOVE As Short = &H2S Const SWP_NOSIZE As Short = 1 Const SWP_NOZORDER As Short = &H4S Const HWND_BOTTOM As Short = 1
Const TV_FIRST = &H1100 Const TVM_SETBKCOLOR = TV_FIRST + 29
Dim iDevice As Integer = 0 ' Current device ID Dim hHwnd As Integer ' Handle to preview window
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Integer,ByVal wMsg As Integer,ByVal wParam As Integer,_ ByVal lParam As Object) As Integer
Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer,_ ByVal hWndInsertAfter As Integer,ByVal x As Integer,ByVal y As Integer,_ ByVal cx As Integer,ByVal cy As Integer,ByVal wFlags As Integer) As Integer
Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
Declare Function capCreateCaptureWindowA Lib "avicap32.dll" _ (ByVal lpszWindowName As String,ByVal dwStyle As Integer,_ ByVal x As Integer,ByVal nWidth As Integer,_ ByVal nHeight As Short,ByVal hWndParent As Integer,_ ByVal nID As Integer) As Integer
Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short,_ ByVal lpszName As String,ByVal cbName As Integer,ByVal lpszVer As String,_ ByVal cbVer As Integer) As Boolean Declare Function CreateCompatibleDC Lib "gdi32" Alias "CreateCompatibleDC" (ByVal hdc As Long) As Long Declare Function CreateCompatibleBitmap Lib "gdi32" Alias "CreateCompatibleBitmap" (ByVal hdc As Long,ByVal nWidth As Long,ByVal nHeight As Long) As Long
' Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Integer,ByVal y As Integer) As Long 'Declare Function SetPixel Lib "gdi32" Alias "SetPixel" (ByVal hdc As Integer,ByVal y As Integer) As Long '变量声明 Private i,j,x,y As Integer Private rgbpoint(1024,784) As rgbp Private bmap As Image Private data As IDataObject Private R,G,B,APL As Integer
Public Structure rgbp Dim r As Integer Dim g As Integer Dim b As Integer End Structure
Private Sub CopyClipboard()
' Copy image to clipboard ' SendMessage(hHwnd,WM_CAP_EDIT_COPY,0) ' Get image from clipboard and convert it to a bitmap hHwnd ' data = Clipboard.GetDataObject() If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)),Image) 'picCapture.Image = bmap End If
End Sub Private Sub savefile() '保存文件 Dim savedia As SaveFileDialog
Dim savebitmap As Bitmap If bmap Is Nothing Then MsgBox("保存失败",MsgBoxStyle.OKOnly) Exit Sub End If savedia = New SaveFileDialog savedia.Filter = "jpg文件|*.jpg|bmp文件|*.bmp" savedia.ShowDialog() '显示“保存”对话框 Dim tempfile = savedia.FileName savebitmap = bmap If savedia.ShowDialog() = DialogResult.OK Then If savedia.FilterIndex = 1 Then savebitmap.Save(tempfile,Imaging.ImageFormat.Jpeg) End If If savedia.FilterIndex = 2 Then savebitmap.Save(tempfile,Imaging.ImageFormat.Bmp) End If End If End Sub
Private Sub getrgbp() Dim tempcolor As Color Dim tempbitmap As Bitmap = bmap.Clone() x = tempbitmap.Width y = tempbitmap.Height
For i = 0 To x - 1 Step 1 For j = 0 To y - 1 Step 1 tempcolor = tempbitmap.GetPixel(i,j) rgbpoint(i,j).r = tempcolor.R rgbpoint(i,j).g = tempcolor.G rgbpoint(i,j).b = tempcolor.B Next Next End Sub
Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load LoadDeviceList() If lstDevices.Items.Count > 0 Then btnStart.Enabled = True lstDevices.SelectedIndex = 0 btnStart.Enabled = True Else lstDevices.Items.Add("无视频设备") btnStart.Enabled = False End If RGBA() Me.Timer1.Stop() btnStop.Enabled = False btnSave.Enabled = False picCapture.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
Private Sub LoadDeviceList() Dim strName As String = Space(100) Dim strVer As String = Space(100) Dim bReturn As Boolean Dim x As Integer = 0
' ' Load name of all avialable devices into the lstDevices '
Do ' ' Get Driver name and version ' bReturn = capGetDriverDescriptionA(x,strName,100,strVer,100)
''连接
' If there was a device add device name to the list ' If bReturn Then lstDevices.Items.Add(strName.Trim) x += 1 Loop Until bReturn = False End Sub
Private Sub OpenPreviewWindow() Dim iHeight As Integer = picCapture.Height Dim iWidth As Integer = picCapture.Width
' Open Preview window in picturebox '建立采集窗口(不显示窗口,处理后显示到PIC控件)
'创建一个与特定设备场景一致的内存设备场景
hHwnd = capCreateCaptureWindowA(iDevice,WS_VISIBLE Or WS_CHILD,240,_ 320,picCapture.Handle.ToInt32,0)
'连接 ' Connect to device ' If SendMessage(hHwnd,WM_CAP_DRIVER_CONNECT,iDevice,0) Then ' 'Set the preview scale ' SendMessage(hHwnd,WM_CAP_SET_SCALE,True,0) '允许或禁止preview视频的缩放比例. 'Scale开 Call SendMessage(hHwnd,TVM_SETBKCOLOR,RGB(0,0)) ' 'Set the preview rate in milliseconds ' SendMessage(hHwnd,WM_CAP_SET_PREVIEWRATE,125,0) '当帧在preview模式显示时设置速度.
' 'Start previewing the image from the camera ' ' SendMessage(hHwnd,WM_CAP_SET_PREVIEW,False,0)
' Resize window to fit in picturebox ' SetWindowPos(hHwnd,HWND_BOTTOM,picCapture.Width,picCapture.Height,_ SWP_NOMOVE Or SWP_NOZORDER)
btnSave.Enabled = True btnStop.Enabled = True btnStart.Enabled = False Else ' ' Error connecting to device close window ' DestroyWindow(hHwnd)
btnSave.Enabled = False End If
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnStart.Click iDevice = lstDevices.SelectedIndex OpenPreviewWindow() Me.Timer1.Start() End Sub
Private Sub ClosePreviewWindow() ' ' Disconnect from device ' SendMessage(hHwnd,WM_CAP_DRIVER_DISCONNECT,0) ' ' close window ' DestroyWindow(hHwnd) End Sub
Private Sub btnStop_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnStop.Click ClosePreviewWindow() btnSave.Enabled = False btnStart.Enabled = True btnStop.Enabled = False End Sub
Private Sub btnSave_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnSave.Click savefile()
End Sub
Private Sub Form1_Closing(ByVal sender As Object,ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing If btnStop.Enabled Then ClosePreviewWindow() End If End Sub
Private Sub pccc()
Dim temprgb As rgbp Dim tempcolor As Color Dim newbitmap As Bitmap
CopyClipboard() newbitmap = bmap.Clone Call getrgbp() For i = 1 To x - 2 Step 1 For j = 1 To y - 2 Step 1 temprgb.r = rgbpoint(i,j).r + R + APL temprgb.g = rgbpoint(i,j).g + G + APL temprgb.b = rgbpoint(i,j).b + B + APL
If temprgb.r < 0 Then temprgb.r = 0 End If
If temprgb.g < 0 Then temprgb.g = 0 End If
If temprgb.b < 0 Then temprgb.b = 0 End If If temprgb.r > 255 Then temprgb.r = 255 End If
If temprgb.g > 255 Then temprgb.g = 255 End If
If temprgb.b > 255 Then temprgb.b = 255 End If newbitmap.SetPixel(i,_ Color.FromArgb(temprgb.r,temprgb.g,temprgb.b))
Next Next PicB1.Image = newbitmap End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Timer1.Tick pccc() End Sub Private Sub RGBA() R = TrackBar1.Value G = TrackBar2.Value B = TrackBar4.Value APL = TrackBar3.Value
End Sub
Private Sub TrackBar1_MouseUp(ByVal sender As Object,ByVal e As System.Windows.Forms.MouseEventArgs) Handles TrackBar1.MouseUp RGBA()
End Sub
Private Sub TrackBar2_MouseUp(ByVal sender As Object,ByVal e As System.Windows.Forms.MouseEventArgs) Handles TrackBar2.MouseUp RGBA()
End Sub
Private Sub TrackBar3_MouseUp(ByVal sender As System.Object,ByVal e As System.Windows.Forms.MouseEventArgs) Handles TrackBar3.MouseUp RGBA()
End Sub
Private Sub TrackBar4_MouseUp(ByVal sender As Object,ByVal e As System.Windows.Forms.MouseEventArgs) Handles TrackBar4.MouseUp RGBA()
End Sub
Private Sub TrackBar3_Scroll(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles TrackBar3.Scroll
End SubEnd Class (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|