0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

cscの作法 その388

Last updated at Posted at 2023-07-08

概要

cscの作法、調べてみた。
練習問題、やってみた。

練習問題

aforgeでカメラをキャプチャして表示せよ。
aforgeでaviファイルを再生せよ。

写真

image.png

サンプルコード



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using AForge.Video;
using AForge.Video.DirectShow;

namespace Player
{
	public partial class MainForm: Form {
		private System.ComponentModel.IContainer components = null;
		private MenuStrip mainMenuStrip;
		private ToolStripMenuItem fileToolStripMenuItem;
		private StatusStrip statusStrip;
		private Panel mainPanel;
		private ToolStripMenuItem exitToolStripMenuItem;
		private ToolStripMenuItem localVideoCaptureDeviceToolStripMenuItem;
		private ToolStripSeparator toolStripMenuItem1;
		private AForge.Controls.VideoSourcePlayer videoSourcePlayer;
		private Timer timer;
		private ToolStripStatusLabel fpsLabel;
		private ToolStripMenuItem openVideofileusingDirectShowToolStripMenuItem;
		private OpenFileDialog openFileDialog;
		private ToolStripMenuItem openJPEGURLToolStripMenuItem;
		private ToolStripMenuItem openMJPEGURLToolStripMenuItem;
		private ToolStripMenuItem capture1stDisplayToolStripMenuItem;
		private Stopwatch stopWatch = null;
		public MainForm() {
			InitializeComponent();
		}
		private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
			CloseCurrentVideoSource();
		}
		private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
			this.Close();
		}
		private void localVideoCaptureDeviceToolStripMenuItem_Click(object sender, EventArgs e) {
			VideoCaptureDeviceForm form = new VideoCaptureDeviceForm();
			if (form.ShowDialog(this) == DialogResult.OK)
			{
				VideoCaptureDevice videoSource = form.VideoDevice;
				OpenVideoSource(videoSource);
			}
		}
		private void openVideofileusingDirectShowToolStripMenuItem_Click(object sender, EventArgs e) {
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				FileVideoSource fileSource = new FileVideoSource(openFileDialog.FileName);
				OpenVideoSource(fileSource);
			}
		}
		private void openJPEGURLToolStripMenuItem_Click(object sender, EventArgs e) {
			//URLForm form = new URLForm();
			//form.Description = "Enter URL of an updating JPEG from a web camera:";
			//form.URLs = new string[] {
			//	"http://195.243.185.195/axis-cgi/jpg/image.cgi?camera=1",
			//};
			//if (form.ShowDialog(this) == DialogResult.OK)
			//{
			//	JPEGStream jpegSource = new JPEGStream(form.URL);
			//	OpenVideoSource(jpegSource);
			//}
		}
		private void openMJPEGURLToolStripMenuItem_Click(object sender, EventArgs e) {
			//URLForm form = new URLForm();
			//form.Description = "Enter URL of an MJPEG video stream:";
			//form.URLs = new string[] {
			//	"http://195.243.185.195/axis-cgi/mjpg/video.cgi?camera=4",
			//	"http://195.243.185.195/axis-cgi/mjpg/video.cgi?camera=3",
			//};
			//if (form.ShowDialog(this) == DialogResult.OK)
			//{
			//	MJPEGStream mjpegSource = new MJPEGStream(form.URL);
			//	OpenVideoSource(mjpegSource);
			//}
		}
		private void capture1stDisplayToolStripMenuItem_Click(object sender, EventArgs e) {
			OpenVideoSource(new ScreenCaptureStream(Screen.AllScreens[0].Bounds, 100));
		}
		private void OpenVideoSource(IVideoSource source) {
			this.Cursor = Cursors.WaitCursor;
			CloseCurrentVideoSource();
			videoSourcePlayer.VideoSource = source;
			videoSourcePlayer.Start();
			stopWatch = null;
			timer.Start();
			this.Cursor = Cursors.Default;
		}
		private void CloseCurrentVideoSource() {
			if (videoSourcePlayer.VideoSource != null)
			{
				videoSourcePlayer.SignalToStop();
				for (int i = 0; i < 30; i++)
				{
					if (!videoSourcePlayer.IsRunning)
						break;
					System.Threading.Thread.Sleep(100);
				}
				if (videoSourcePlayer.IsRunning)
				{
					videoSourcePlayer.Stop();
				}
				videoSourcePlayer.VideoSource = null;
			}
		}
		private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image) {
			DateTime now = DateTime.Now;
			Graphics g = Graphics.FromImage(image);
			SolidBrush brush = new SolidBrush(Color.Red);
			g.DrawString(now.ToString(), this.Font, brush, new PointF(5, 5));
			brush.Dispose();
			g.Dispose();
		}
		private void timer_Tick(object sender, EventArgs e) {
			IVideoSource videoSource = videoSourcePlayer.VideoSource;
			if (videoSource != null)
			{
				int framesReceived = videoSource.FramesReceived;
				if (stopWatch == null)
				{
					stopWatch = new Stopwatch();
					stopWatch.Start();
				}
				else
				{
					stopWatch.Stop();
					float fps = 1000.0f * framesReceived / stopWatch.ElapsedMilliseconds;
					fpsLabel.Text = fps.ToString("F2") + " fps";
					stopWatch.Reset();
					stopWatch.Start();
				}
			}
		}
		protected override void Dispose(bool disposing) {
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent() {
			this.components = new System.ComponentModel.Container();
			this.mainMenuStrip = new MenuStrip();
			this.fileToolStripMenuItem = new ToolStripMenuItem();
			this.localVideoCaptureDeviceToolStripMenuItem = new ToolStripMenuItem();
			this.openVideofileusingDirectShowToolStripMenuItem = new ToolStripMenuItem();
			this.openJPEGURLToolStripMenuItem = new ToolStripMenuItem();
			this.openMJPEGURLToolStripMenuItem = new ToolStripMenuItem();
			this.toolStripMenuItem1 = new ToolStripSeparator();
			this.exitToolStripMenuItem = new ToolStripMenuItem();
			this.statusStrip = new StatusStrip();
			this.fpsLabel = new ToolStripStatusLabel();
			this.mainPanel = new Panel();
			this.videoSourcePlayer = new AForge.Controls.VideoSourcePlayer();
			this.timer = new Timer(this.components);
			this.openFileDialog = new OpenFileDialog();
			this.capture1stDisplayToolStripMenuItem = new ToolStripMenuItem();
			this.mainMenuStrip.SuspendLayout();
			this.statusStrip.SuspendLayout();
			this.mainPanel.SuspendLayout();
			this.SuspendLayout();
			this.mainMenuStrip.Items.AddRange(new ToolStripItem[] {
			this.fileToolStripMenuItem});
			this.mainMenuStrip.Location = new Point(0, 0);
			this.mainMenuStrip.Name = "mainMenuStrip";
			this.mainMenuStrip.Size = new Size(434, 24);
			this.mainMenuStrip.TabIndex = 0;
			this.mainMenuStrip.Text = "menuStrip1";
			this.fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] {
				this.localVideoCaptureDeviceToolStripMenuItem,
				this.openVideofileusingDirectShowToolStripMenuItem,
				this.openJPEGURLToolStripMenuItem,
				this.openMJPEGURLToolStripMenuItem,
				this.capture1stDisplayToolStripMenuItem,
				this.toolStripMenuItem1,
				this.exitToolStripMenuItem
			});
			this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
			this.fileToolStripMenuItem.Size = new Size(35, 20);
			this.fileToolStripMenuItem.Text = "&File";
			this.localVideoCaptureDeviceToolStripMenuItem.Name = "localVideoCaptureDeviceToolStripMenuItem";
			this.localVideoCaptureDeviceToolStripMenuItem.Size = new Size(250, 22);
			this.localVideoCaptureDeviceToolStripMenuItem.Text = "Local &Video Capture Device";
			this.localVideoCaptureDeviceToolStripMenuItem.Click += new System.EventHandler(this.localVideoCaptureDeviceToolStripMenuItem_Click);
			this.openVideofileusingDirectShowToolStripMenuItem.Name = "openVideofileusingDirectShowToolStripMenuItem";
			this.openVideofileusingDirectShowToolStripMenuItem.Size = new Size(250, 22);
			this.openVideofileusingDirectShowToolStripMenuItem.Text = "Open video &file (using DirectShow)";
			this.openVideofileusingDirectShowToolStripMenuItem.Click += new System.EventHandler(this.openVideofileusingDirectShowToolStripMenuItem_Click);
			this.openJPEGURLToolStripMenuItem.Name = "openJPEGURLToolStripMenuItem";
			this.openJPEGURLToolStripMenuItem.Size = new Size(250, 22);
			this.openJPEGURLToolStripMenuItem.Text = "Open JPEG &URL";
			this.openJPEGURLToolStripMenuItem.Click += new System.EventHandler(this.openJPEGURLToolStripMenuItem_Click);
			this.openMJPEGURLToolStripMenuItem.Name = "openMJPEGURLToolStripMenuItem";
			this.openMJPEGURLToolStripMenuItem.Size = new Size(250, 22);
			this.openMJPEGURLToolStripMenuItem.Text = "Open &MJPEG URL";
			this.openMJPEGURLToolStripMenuItem.Click += new System.EventHandler(this.openMJPEGURLToolStripMenuItem_Click);
			this.toolStripMenuItem1.Name = "toolStripMenuItem1";
			this.toolStripMenuItem1.Size = new Size(247, 6);
			this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
			this.exitToolStripMenuItem.Size = new Size(250, 22);
			this.exitToolStripMenuItem.Text = "E&xit";
			this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
			this.statusStrip.Items.AddRange(new ToolStripItem[] {
				this.fpsLabel
			});
			this.statusStrip.Location = new Point(0, 332);
			this.statusStrip.Name = "statusStrip";
			this.statusStrip.Size = new Size(434, 22);
			this.statusStrip.TabIndex = 1;
			this.statusStrip.Text = "statusStrip1";
			this.fpsLabel.Name = "fpsLabel";
			this.fpsLabel.Size = new Size(419, 17);
			this.fpsLabel.Spring = true;
			this.fpsLabel.TextAlign = ContentAlignment.MiddleLeft;
			this.mainPanel.Controls.Add(this.videoSourcePlayer);
			this.mainPanel.Dock = DockStyle.Fill;
			this.mainPanel.Location = new Point(0, 24);
			this.mainPanel.Name = "mainPanel";
			this.mainPanel.Size = new Size(434, 308);
			this.mainPanel.TabIndex = 2;
			this.videoSourcePlayer.AutoSizeControl = true;
			this.videoSourcePlayer.BackColor = SystemColors.ControlDarkDark;
			this.videoSourcePlayer.ForeColor = Color.White;
			this.videoSourcePlayer.Location = new Point(56, 33);
			this.videoSourcePlayer.Name = "videoSourcePlayer";
			this.videoSourcePlayer.Size = new Size(322, 242);
			this.videoSourcePlayer.TabIndex = 0;
			this.videoSourcePlayer.VideoSource = null;
			this.videoSourcePlayer.NewFrame += new AForge.Controls.VideoSourcePlayer.NewFrameHandler(this.videoSourcePlayer_NewFrame);
			this.timer.Interval = 1000;
			this.timer.Tick += new System.EventHandler(this.timer_Tick);
			this.openFileDialog.Filter = "AVI files (*.avi)|*.avi|All files (*.*)|*.*";
			this.openFileDialog.Title = "Opem movie";
			this.capture1stDisplayToolStripMenuItem.Name = "capture1stDisplayToolStripMenuItem";
			this.capture1stDisplayToolStripMenuItem.Size = new Size(250, 22);
			this.capture1stDisplayToolStripMenuItem.Text = "Capture 1st display";
			this.capture1stDisplayToolStripMenuItem.Click += new System.EventHandler(this.capture1stDisplayToolStripMenuItem_Click);
			this.AutoScaleDimensions = new SizeF(6F, 13F);
			this.AutoScaleMode = AutoScaleMode.Font;
			this.ClientSize = new Size(434, 354);
			this.Controls.Add(this.mainPanel);
			this.Controls.Add(this.statusStrip);
			this.Controls.Add(this.mainMenuStrip);
			this.MainMenuStrip = this.mainMenuStrip;
			this.Name = "MainForm";
			this.Text = "Simple Player";
			this.FormClosing += new FormClosingEventHandler(this.MainForm_FormClosing);
			this.mainMenuStrip.ResumeLayout(false);
			this.mainMenuStrip.PerformLayout();
			this.statusStrip.ResumeLayout(false);
			this.statusStrip.PerformLayout();
			this.mainPanel.ResumeLayout(false);
			this.ResumeLayout(false);
			this.PerformLayout();
		}
		[STAThread]
		public static void Main(string[] args) {
			Application.Run(new MainForm());
		}
	}
}





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?