using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PaintDotNet.Base.SettingModel;
using System.Collections;
using PaintDotNet.ImageCollect;
using TUCamera;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
namespace PaintDotNet.Setting.LabelComponent
{
///
/// 相机设置,边框界面
///
public class FrameCameraControl1 : UserControl
{
#region 控件
#endregion
///
/// 相机参数的Model
///
private CameraParamModel m_cameraParamModel;
///
/// 相机设置的dialog
///
private Point m_startP; //画框的起始点
private bool m_blnDraw = false;
private Rectangle m_rect ;//初始设置一个拖拽的显示框
SolidBrush m_brush = new SolidBrush(Color.Black);
int m_handle = 0;
int m_targetX = 0; // 起始点X坐标 对应分辨率
int m_targetY = 0; // 起始点Y坐标 对应分辨率
int m_targetWidth = 320; //显示框的宽度 对应分辨率
int m_targetHeight = 200; //显示框的高度 对应分辨率
int m_tempX = 0;
int m_tempY = 0;
int m_tempW = 0;
int m_tempH = 0;
private TUCameraIFrameProcess m_process = null;
private Size m_resolution; // 分辨率
private TUCamera.TUCamera m_camera;
private bool m_use = false; // 设置是否立刻生效,设置到相机
private OpenCvSharp.Mat m_mat;
string[] m_binArray = new string[] { "1x1 Bin", "2x2 Bin", "3x3 Bin", "4x4 Bin" };
private GroupBox groupBox2;
private ComboBox cmbResolution;
private bool m_isWaiting;
private int m_selectedBin;
public FrameCameraControl1(CameraParamModel model , bool use)
{
this.m_use = use;
this.m_cameraParamModel = model;
this.m_camera = TUCameraManager.GetInstance().GetCurrentCamera();
InitializeComponent();
InitializeLanguageText();
InitializeControlData();
}
public void ResetCameraParamModel(CameraParamModel model)
{
m_cameraParamModel = model;
InitializeControlData();
//InitializeData();
}
///
/// 设置下拉等数据源
///
private void InitializeControlData()
{
int index = m_cameraParamModel.parame.BinningSumation -1;
if (index > m_binArray.Length || index < 0)
{
index = 0;
}
cmbResolution.Items.Clear();
List resolutions = this.m_camera.GetResolutionRange();
for(int i=0; i< resolutions.Count; i++)
{
cmbResolution.Items.Add(resolutions[i]);
}
if (resolutions.Count > 0)
{
cmbResolution.SelectedIndex = m_cameraParamModel.parame.Resolution;
}
}
///
/// 绑定样式数据
///
private void InitializeData()
{
m_targetX = this.m_cameraParamModel.parame.previewRectX;
m_targetY = this.m_cameraParamModel.parame.previewRectY;
m_targetWidth = this.m_cameraParamModel.parame.previewRectW;
m_targetHeight = this.m_cameraParamModel.parame.previewRectH;
SetROI();
m_rect = new Rectangle();
//drawingImg();
//if (m_resolution.Width > 0 && m_resolution.Height > 0)
//{
// this.pictureBox1.Height = (this.pictureBox1.Width * m_resolution.Height) / m_resolution.Width;
// m_rect.X = this.m_cameraParamModel.parame.previewRectX * this.pictureBox1.Width / m_resolution.Width;
// m_rect.Y = this.m_cameraParamModel.parame.previewRectY * this.pictureBox1.Height / m_resolution.Height;
// m_rect.Width = this.m_cameraParamModel.parame.previewRectW * this.pictureBox1.Width / m_resolution.Width;
// m_rect.Height = this.m_cameraParamModel.parame.previewRectH * this.pictureBox1.Height / m_resolution.Height;
// Console.WriteLine(m_rect);
//}
//else
//{
// m_rect.X = 0;
// m_rect.Y = 0;
// m_rect.Width = this.pictureBox1.Width;
// m_rect.Height = this.pictureBox1.Height;
//}
}
private void InitializeLanguageText()
{
this.groupBox2.Text = PdnResources.GetString("Menu.Resolution.text");
}
private void InitializeComponent()
{
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.cmbResolution = new System.Windows.Forms.ComboBox();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox2
//
this.groupBox2.Controls.Add(this.cmbResolution);
this.groupBox2.Location = new System.Drawing.Point(12, 12);
this.groupBox2.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Padding = new System.Windows.Forms.Padding(2, 2, 2, 2);
this.groupBox2.Size = new System.Drawing.Size(464, 66);
this.groupBox2.TabIndex = 3;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "分辨率";
//
// cmbResolution
//
this.cmbResolution.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbResolution.FormattingEnabled = true;
this.cmbResolution.Location = new System.Drawing.Point(19, 26);
this.cmbResolution.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
this.cmbResolution.Name = "cmbResolution";
this.cmbResolution.Size = new System.Drawing.Size(416, 20);
this.cmbResolution.TabIndex = 0;
this.cmbResolution.SelectedIndexChanged += new System.EventHandler(this.cmbResolution_SelectedIndexChanged);
//
// FrameCameraControl1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.groupBox2);
this.Name = "FrameCameraControl1";
this.Size = new System.Drawing.Size(488, 93);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
///
/// 绘制
///
///
///
public void CallbackDraw(byte[] pBuf, object obj, int channel)
{
lock (obj)
{
m_isWaiting = false;
Console.WriteLine("接收全图buf");
m_camera.m_drawAllHandler -= new DrawAllHandler(CallbackDraw);
OpenCvSharp.Mat mat;
if (channel == 1)
{
mat = new OpenCvSharp.Mat(m_resolution.Height, m_resolution.Width, OpenCvSharp.MatType.CV_8UC1, pBuf);
}
else
{
mat = new OpenCvSharp.Mat(m_resolution.Height, m_resolution.Width, OpenCvSharp.MatType.CV_8UC3, pBuf);
}
OpenCvSharp.Cv2.Flip(mat, mat, 0);
stopDrawing();
if (mat != null)
{
mat.Dispose();
}
if (m_mat != null)
{
m_mat.Dispose();
}
}
}
private void stopDrawing()
{
//创建一个委托,用于封装一个方法,在这里是封装了 控制更新控件 的方法
Action invokeAction = new Action(stopDrawing);
//判断操作控件的线程是否创建控件的线程
//调用方调用方位于创建控件所在的线程以外的线程中,如果在其他线程则对控件进行方法调用时必须调用 Invoke 方法
if (this.InvokeRequired)
{
//与调用线程不同的线程上创建(说明您必须通过 Invoke 方法对控件进行调用)
this.Invoke(invokeAction);
}
else
{
m_camera.StopDrawing(m_process);
m_camera.StopWaitForFrame();
////窗体线程,即主线程
//shuaxinButton.Enabled = true;
}
}
private void SetROI()
{
if (m_use)
{
m_camera.SetROI(ref m_targetX, ref m_targetY, ref m_targetWidth, ref m_targetHeight);
this.m_cameraParamModel.parame.previewRectX = m_targetX;
this.m_cameraParamModel.parame.previewRectY = m_targetY;
this.m_cameraParamModel.parame.previewRectW = m_targetWidth;
this.m_cameraParamModel.parame.previewRectH = m_targetHeight;
}
}
private void cmbResolution_SelectedIndexChanged(object sender, EventArgs e)
{
m_cameraParamModel.parame.Resolution = cmbResolution.SelectedIndex;
int resolutionId = m_camera.GetResolutionId();
if (resolutionId != m_cameraParamModel.parame.Resolution)
{
// 分辨率
m_camera.SetResolution(m_cameraParamModel.parame.Resolution);
}
m_camera.GetROI(ref m_cameraParamModel.parame.previewRectX, ref m_cameraParamModel.parame.previewRectY, ref m_cameraParamModel.parame.previewRectW, ref m_cameraParamModel.parame.previewRectH);
InitializeData();
//this.zuobiaoXTextBox.Text = m_cameraParamModel.parame.previewRectX.ToString();
//this.zuobiaoYTextBox.Text = m_cameraParamModel.parame.previewRectY.ToString();
//this.chicunWTextBox.Text = m_cameraParamModel.parame.previewRectW.ToString();
//this.chicunHTextBox.Text = m_cameraParamModel.parame.previewRectH.ToString();
}
}
}