Public Class Form1

    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Int32, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Int32) As Int32
    Dim isopen As Boolean = False

    Dim CamID As Int32 = 0  '此例程默认以第一个摄像头打开
    Dim SupportFormat As Int32   '设备支持的视频格式代号：1->MJPG; 2->YUY2 
    Dim OpenFormat As Int32   '打开格式：0->YUY；1->MJPG
    Dim flag As Boolean = False

    Dim imgpath As String

    '''窗体关闭
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If isopen = True Then AxBaucheOcx1.CAM_Close() '关闭摄像头
    End Sub

    '''窗体加载
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Dim k As Int32
        Dim FormatSum As Int32 = 0 '设备支持的视频格式代号总和
        FormatSum = AxBaucheOcx1.GetFormatCount(CamID)   '获取设备支持的视频格式数目       

        '判断视频格式代号总和的值
        Select Case FormatSum

            Case 1 '（设备只支持MJPG格式）
                OpenFormat = 1      '打开格式为MJPG
                SupportFormat = 1
            Case 2  '（设备只支持YUY2格式）
                OpenFormat = 0      '打开格式为YUY
                SupportFormat = 2
            Case 3  '（设备支持MJPG与YUY2两种格式）
                OpenFormat = 1      '默认打开格式为MJPG
                SupportFormat = 1
        End Select

        Dim i As Int32 = AxBaucheOcx1.GetResolutionCount(CamID, SupportFormat) '获取设备支持的分辨率数目
        For j = 0 To i - 1
            ComboBox1.Items.Add(AxBaucheOcx1.GetResolution(j))  '获取分辨率        
        Next
        If i > 0 Then ComboBox1.SelectedIndex = 0

        i = AxBaucheOcx1.GetDevCount()  '获取设备数目
        For j = 0 To i - 1
            ComboBox2.Items.Add(AxBaucheOcx1.GetDevFriendName(j))  '获取设备名称
        Next
        If i > 0 Then ComboBox2.SelectedIndex = 0


        ComboBox3.SelectedIndex = 0 '默认为彩色
        ComboBox4.SelectedIndex = 0 '默认文件格式为jpg

        flag = True

    End Sub

    Private Sub AxBaucheOcx1_MessageCallback(ByVal sender As System.Object, ByVal e As AxBaucheOcxLib._DBaucheOcxEvents_MessageCallbackEvent) Handles AxBaucheOcx1.MessageCallback
        If e.type = 1 Then
            imgpath = e.str
        End If

    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim SizeStr As String = ComboBox1.SelectedItem.ToString
        Dim pos, width, height As Int32
        pos = SizeStr.LastIndexOf("*")
        width = CInt(SizeStr.Substring(0, pos))  '宽
        height = CInt(SizeStr.Substring(pos + 1, SizeStr.Length - (pos + 1)))  '高

        Dim i As Int32 = AxBaucheOcx1.CAM_Open(CamID, OpenFormat, width, height)  '开启摄像头
        If i = 0 Then
            Debug.WriteLine("开启成功")
            isopen = True
        Else
            Debug.WriteLine("开启失败！")
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If isopen = True Then
            AxBaucheOcx1.CAM_Close()
            isopen = False
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        AxBaucheOcx1.CAM_zoomin()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        AxBaucheOcx1.CAM_zoomout()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        AxBaucheOcx1.CAM_RotateL()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        AxBaucheOcx1.CAM_RotateR()
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        AxBaucheOcx1.BestSize()
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        AxBaucheOcx1.TrueSize()
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Dim str As String = AxBaucheOcx1.CaptureImageJava()
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click

        ShellExecute(0, vbNullString, imgpath, vbNullString, vbNullString, AppWinStyle.NormalFocus)
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        If RadioButton1.Checked = True Then
            AxBaucheOcx1.SetAutoCut(0) '取消自动裁切

        End If
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        If RadioButton2.Checked = True Then
            AxBaucheOcx1.SetAutoCut(1)
       
        End If
    End Sub

    Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
        If RadioButton3.Checked = True Then
            AxBaucheOcx1.CusCrop(1)
        End If
    End Sub

    Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
        AxBaucheOcx1.SetImageColorMode(ComboBox3.SelectedIndex)
    End Sub


    Private Sub ComboBox4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox4.SelectedIndexChanged
        AxBaucheOcx1.SetFileType(ComboBox4.SelectedIndex)
    End Sub

End Class
