Imports System.IO
Imports System.Text

'=========================================================================
'クラス名称：遠隔監視アプリケーションメインクラス
'機能      ：
'備考      ：なし
'=========================================================================
Public Class frmMain
    Inherits System.Windows.Forms.Form

#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。
    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

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    'Windows フォーム デザイナを使って変更してください。  
    ' コード エディタを使って変更しないでください。
    Friend WithEvents btnStart As System.Windows.Forms.Button
    Friend WithEvents btnStop As System.Windows.Forms.Button
    Friend WithEvents barThreshold As System.Windows.Forms.TrackBar
    Friend WithEvents menuMain As System.Windows.Forms.MainMenu
    Friend WithEvents dlgSavePicture As System.Windows.Forms.SaveFileDialog
    Friend WithEvents dlgSaveLog As System.Windows.Forms.SaveFileDialog
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
    Friend WithEvents pctBox As System.Windows.Forms.PictureBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.pctBox = New System.Windows.Forms.PictureBox
        Me.btnStart = New System.Windows.Forms.Button
        Me.btnStop = New System.Windows.Forms.Button
        Me.barThreshold = New System.Windows.Forms.TrackBar
        Me.menuMain = New System.Windows.Forms.MainMenu
        Me.dlgSavePicture = New System.Windows.Forms.SaveFileDialog
        Me.dlgSaveLog = New System.Windows.Forms.SaveFileDialog
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.GroupBox3 = New System.Windows.Forms.GroupBox
        CType(Me.barThreshold, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox3.SuspendLayout()
        Me.SuspendLayout()
        '
        'pctBox
        '
        Me.pctBox.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.pctBox.Location = New System.Drawing.Point(16, 24)
        Me.pctBox.Name = "pctBox"
        Me.pctBox.Size = New System.Drawing.Size(320, 240)
        Me.pctBox.TabIndex = 1
        Me.pctBox.TabStop = False
        '
        'btnStart
        '
        Me.btnStart.Location = New System.Drawing.Point(384, 304)
        Me.btnStart.Name = "btnStart"
        Me.btnStart.Size = New System.Drawing.Size(88, 24)
        Me.btnStart.TabIndex = 3
        Me.btnStart.Text = "開始"
        '
        'btnStop
        '
        Me.btnStop.Location = New System.Drawing.Point(384, 344)
        Me.btnStop.Name = "btnStop"
        Me.btnStop.Size = New System.Drawing.Size(88, 24)
        Me.btnStop.TabIndex = 4
        Me.btnStop.Text = "終了"
        '
        'barThreshold
        '
        Me.barThreshold.Location = New System.Drawing.Point(16, 16)
        Me.barThreshold.Maximum = 30
        Me.barThreshold.Name = "barThreshold"
        Me.barThreshold.Size = New System.Drawing.Size(328, 45)
        Me.barThreshold.TabIndex = 6
        Me.barThreshold.Value = 10
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.pctBox)
        Me.GroupBox1.Location = New System.Drawing.Point(8, 8)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(352, 280)
        Me.GroupBox1.TabIndex = 10
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "カメラ画像"
        '
        'GroupBox3
        '
        Me.GroupBox3.Controls.Add(Me.barThreshold)
        Me.GroupBox3.Location = New System.Drawing.Point(8, 296)
        Me.GroupBox3.Name = "GroupBox3"
        Me.GroupBox3.Size = New System.Drawing.Size(352, 72)
        Me.GroupBox3.TabIndex = 12
        Me.GroupBox3.TabStop = False
        Me.GroupBox3.Text = "感度設定"
        '
        'frmMain
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(616, 401)
        Me.Controls.Add(Me.GroupBox3)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.btnStop)
        Me.Controls.Add(Me.btnStart)
        Me.Menu = Me.menuMain
        Me.Name = "frmMain"
        Me.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.Text = "遠隔監視システム"
        CType(Me.barThreshold, System.ComponentModel.ISupportInitialize).EndInit()
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox3.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    ' CAOエンジン
    Private m_caoEng As CaoEngine
    ' CAOワークスペース
    Private m_caoWS As CaoWorkspace
    ' USBカメラのCAOコントローラ
    Private WithEvents m_ctrlCamera As CaoController
    ' カメラ画像のCAOファイル
    Private m_filePicture As CaoFile
    ' カメラの感度設定CAO変数
    Private m_varThreshold As CaoVariable
    ' 画像のバイナリデータ
    Private m_bArray() As Byte

    '*********************************************************************
    '関数名称  ：初期化処理
    '機能      ：アプリケーションの初期化をおこなう
    '引数      ：sender
    '            e
    '戻り値    ：なし
    '備考      ：フォームが生成されたときに呼び出される
    '*********************************************************************
    Private Sub frmMain_Load(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            'CAOエンジンとCAOワークスペースの作成
            m_caoEng = New CaoEngine
            m_caoWS = m_caoEng.Workspaces.Item(0)
            btnStart.Enabled = True
            btnStop.Enabled = False
        Catch ex As Exception
            MsgBox("初期化処理に失敗しました" & vbCrLf & ex.Message)
            Me.Close()
        End Try
    End Sub

    '*********************************************************************
    '関数名称  ：計測開始処理
    '機能      ：カメラプロバイダに接続をおこない画像計測を開始する
    '引数      ：sender
    '            e
    '戻り値    ：なし
    '備考      ：スタートボタンが押されたときに呼び出される
    '*********************************************************************
    Private Sub btnStart_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles btnStart.Click
        Try
            ' USBカメラプロバイダへの接続
            m_ctrlCamera = m_caoWS.AddController( _
                        "camera", "CaoProv.Samples.Camera", "", "")
            ' イベントハンドラの設定
            AddHandler m_ctrlCamera.OnMessage, _
                        AddressOf m_ctrlCamera_OnMessage
            ' CAOファイルの取得
            m_filePicture = m_ctrlCamera.AddFile("Picture")
            ' CAO変数の取得
            m_varThreshold = m_ctrlCamera.AddVariable("Threshold")
            btnStart.Enabled = False
            btnStop.Enabled = True
            setPicture()
        Catch ex As Exception
            MsgBox("プロバイダにアクセスできません" & vbCrLf & ex.Message)
        End Try
    End Sub

    '*********************************************************************
    '関数名称  ：計測終了処理
    '機能      ：画像計測を終了しカメラプロバイダのリソースを開放する
    '引数      ：sender
    '            e
    '戻り値    ：なし
    '備考      ：ストップボタンが押されたときに呼び出される
    '*********************************************************************
    Private Sub btnStop_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles btnStop.Click
        Try
            releaseCAO()
            btnStart.Enabled = True
            btnStop.Enabled = False
        Catch ex As Exception
            MsgBox("プロバイダを開放できません" & vbCrLf & ex.Message)
        End Try
    End Sub


    '*********************************************************************
    '関数名称  ：感度変更処理
    '機能      ：カメラの検出感度を変更する
    '引数      ：sender
    '            e
    '戻り値    ：なし
    '備考      ：感度用スクロールバーをスクロールさせたときに呼び出される
    '*********************************************************************
    Private Sub barThreshold_Scroll(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles barThreshold.Scroll
        Try
            ' 感度の設定
            m_varThreshold.Value = barThreshold.Value
        Catch ex As Exception

        End Try
    End Sub

    '*********************************************************************
    '関数名称  ：イベント処理
    '機能      ：カメラプロバイダからのイベントを受付け画像を表示する
    '引数      ：sender
    '            e
    '戻り値    ：なし
    '備考      ：カメラプロバイダがイベントを発生させたときに呼び出される
    '*********************************************************************
    Private Sub m_ctrlCamera_OnMessage(ByVal pICaoMess As CaoMessage)
        Try
            setPicture()
        Catch ex As Exception
            MsgBox("失敗しました" & vbCrLf & ex.Message)
            releaseCAO()
            btnStart.Enabled = True
            btnStop.Enabled = False
        End Try
    End Sub

    '*********************************************************************
    '関数名称  ：CAOの開放処理
    '機能      ：取得していたCAOのリソースを開放する
    '引数      ：なし
    '戻り値    ：なし
    '備考      ：なし
    '*********************************************************************
    Private Sub releaseCAO()
        Try
            ' CAO変数の開放
            If Not m_varThreshold Is Nothing Then
                m_ctrlCamera.Variables.Remove(m_varThreshold.Index)
                System.Runtime.InteropServices.Marshal. _
                            ReleaseComObject(m_varThreshold)
                m_varThreshold = Nothing
            End If
            ' CAOファイルの開放
            If Not m_filePicture Is Nothing Then
                m_ctrlCamera.Files.Remove(m_filePicture.Index)
                System.Runtime.InteropServices.Marshal. _
                            ReleaseComObject(m_filePicture)
                m_filePicture = Nothing
            End If
            ' CAOコントローラの開放
            If Not m_ctrlCamera Is Nothing Then
                ' イベントハンドラの開放
                RemoveHandler m_ctrlCamera.OnMessage, _
                            AddressOf m_ctrlCamera_OnMessage
                m_caoWS.Controllers.Remove(m_ctrlCamera.Index)
                System.Runtime.InteropServices.Marshal. _
                            ReleaseComObject(m_ctrlCamera)
                m_ctrlCamera = Nothing
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    '*********************************************************************
    '関数名称  ：画像取得処理
    '機能      ：カメラプロバイダから画像を取得しピクチャボックスに表示
    '引数      ：なし
    '戻り値    ：なし
    '備考      ：なし
    '*********************************************************************
    Private Sub setPicture()
        Try
            ' 画像データの取得
            m_bArray = m_filePicture.Value
            ' バイナリデータからMemoryStreamオブジェクトを作成する
            Dim mstreamCur As New MemoryStream(m_bArray)
            ' ストリームからImageオブジェクトを作成し，ピクチャボックスに表示
            pctBox.Image = Image.FromStream(mstreamCur)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

End Class

