幽冥大陆(十八)手机摄像头注册到电脑源码——东方仙盟炼气期

代码

复制代码
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace cyberwin.trade.livetips
{
    public partial class Form1 : Form
    {


        string textToScroll_1 = "足浴店收入支出工具,进销存,超市,服装店,建材城";
        private int scrollIndex_1 = 0;
        public Form1()
        {
            InitializeComponent();
        }
        private VideoCapture capture;
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            capture = new VideoCapture(1);
            Application.Idle += new EventHandler(Application_Idle);
        }
        private void Application_Idle(object sender, EventArgs e)
        {
            if (capture != null)
            {
                Mat frame = capture.QueryFrame();// capture.QueryFrame();
                if (frame != null)
                {
                    //pictureBox1.Image = frame.ToBitmap();

                    float widthRatio = (float)pictureBox1.Width / frame.Width;
                    float heightRatio = (float)pictureBox1.Height / frame.Height;
                    float scaleRatio = Math.Min(widthRatio, heightRatio);
                    int newWidth = (int)(frame.Width * scaleRatio);
                    int newHeight = (int)(frame.Height * scaleRatio);

                    // Mat resizedFrame = frame.Resize(newWidth, newHeight, Emgu.CV.CvEnum.Inter.Cubic);
                    // 根据目标大小缩放
                    Mat resizedBySize = MatResizer.ResizeByTargetSize(frame, newWidth, newHeight);

                    pictureBox1.Image = resizedBySize.ToBitmap();
                }
            }
        }

        /*
        private void Application_Idle_动作(object sender, EventArgs e)

在现代应用开发中,计算机视觉技术的应用越来越广泛,从简单的视频监控到复杂的图像识别,都离不开高效的图像处理库。本文将介绍一个使用 Emgu.CV 库实现视频捕获与显示的 Windows Forms 应用程序,该程序还包含了文本滚动功能,适用于各类商业场景的信息展示。

应用程序结构

该应用程序的核心是一个名为Form1的 Windows 窗体,位于cyberwin.trade.livetips命名空间下。程序主要依赖 Emgu.CV 库进行视频处理,这是一个基于 OpenCV 的.NET 封装库,提供了丰富的图像处理功能。

程序开头引用了必要的命名空间,包括 Emgu.CV 相关的核心命名空间和 Windows Forms 应用程序开发所需的命名空间:

csharp

复制代码
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using System;
using System.Windows.Forms;
// 其他必要命名空间

核心功能实现

文本滚动功能

应用程序定义了一个文本滚动功能,用于展示商业信息:

csharp

复制代码
string textToScroll_1 = "足浴店收入支出工具,进销存,超市,服装店,建材城";
private int scrollIndex_1 = 0;

这里定义了需要滚动显示的文本内容,涵盖了多种商业场景的应用提示,scrollIndex_1变量用于跟踪滚动位置,实现文本的动态展示效果。

视频捕获与显示

视频处理是该应用的核心功能,主要通过以下组件实现:

  1. 视频捕获对象

    csharp

    复制代码
    private VideoCapture capture;

    VideoCaptureEmgu.CV 库中用于捕获视频流的核心类,可以从摄像头或视频文件中获取视频帧。

  2. 启动视频捕获

    csharp

    复制代码
    private void button1_Click(object sender, EventArgs e)
    {
        capture = new VideoCapture(1);
        Application.Idle += new EventHandler(Application_Idle);
    }

    当用户点击按钮时,程序初始化视频捕获对象(参数 1 表示使用第二个摄像头设备),并注册Application_Idle事件处理程序,用于在应用程序空闲时处理视频帧。

  3. 视频帧处理与显示

    csharp

    复制代码
    private void Application_Idle(object sender, EventArgs e)
    {
        if (capture != null)
        {
            Mat frame = capture.QueryFrame();
            if (frame != null)
            {
                // 计算缩放比例,保持图像比例
                float widthRatio = (float)pictureBox1.Width / frame.Width;
                float heightRatio = (float)pictureBox1.Height / frame.Height;
                float scaleRatio = Math.Min(widthRatio, heightRatio);
                int newWidth = (int)(frame.Width * scaleRatio);
                int newHeight = (int)(frame.Height * scaleRatio);
    
                // 按目标尺寸缩放图像
                Mat resizedBySize = MatResizer.ResizeByTargetSize(frame, newWidth, newHeight);
    
                // 在PictureBox中显示图像
                pictureBox1.Image = resizedBySize.ToBitmap();
            }
        }
    }

    这段代码实现了视频帧的获取、缩放和显示功能。为了确保视频在 PictureBox 中正确显示,程序计算了合适的缩放比例,保持图像的原始宽高比,避免图像变形。MatEmgu.CV 中用于存储图像数据的矩阵类,通过ToBitmap()方法可以将其转换为 Windows Forms 支持的 Bitmap 格式。

应用场景与扩展

从程序中的文本信息可以看出,该应用可能用于商业场所的信息展示与监控系统。例如,在超市、服装店、建材城等零售场所,可以通过该程序展示商品信息,同时利用摄像头进行客流分析或安全监控。

程序中还预留了Application_Idle_动作方法的注释框架,暗示可以扩展更多基于视频帧的动作识别功能,如手势识别、顾客行为分析等,进一步提升商业应用价值。

总结

本应用程序结合了 Emgu.CV 的视频处理能力和 Windows Forms 的界面展示功能,实现了一个简洁而实用的视频监控与信息展示系统。通过合理的图像缩放算法,确保了视频在界面中的良好显示效果,同时文本滚动功能增强了信息传递的效率。该程序可以根据实际需求进行扩展,添加更多计算机视觉相关的功能,适用于多种商业场景。

阿雪技术观

在科技发展浪潮中,我们不妨积极投身技术共享。不满足于做受益者,更要主动担当贡献者。无论是分享代码、撰写技术博客,还是参与开源项目维护改进,每一个微小举动都可能蕴含推动技术进步的巨大能量。东方仙盟是汇聚力量的天地,我们携手在此探索硅基生命,为科技进步添砖加瓦。

Hey folks, in this wild tech - driven world, why not dive headfirst into the whole tech - sharing scene? Don't just be the one reaping all the benefits; step up and be a contributor too. Whether you're tossing out your code snippets, hammering out some tech blogs, or getting your hands dirty with maintaining and sprucing up open - source projects, every little thing you do might just end up being a massive force that pushes tech forward. And guess what? The Eastern FairyAlliance is this awesome place where we all come together. We're gonna team up and explore the whole silicon - based life thing, and in the process, we'll be fueling the growth of technology

相关推荐
未来之窗软件服务10 小时前
幽冥大陆(六十五) PHP6.x SSL 文字解密—东方仙盟古法结界
网络·数据库·ssl·加解密·仙盟创梦ide·东方仙盟
未来之窗软件服务11 小时前
幽冥大陆(六十四) PHP7.0 SSL 文字解密—东方仙盟筑基期
php·ssl·加解密·仙盟创梦ide·东方仙盟
未来之窗软件服务1 天前
幽冥大陆(六十三) roff 语言:核心用途与使用指南—东方仙盟筑基期
策略模式·东方仙盟·roff
未来之窗软件服务1 天前
服务器运维(二十)服务器防护双雄:Fail2ban 与 CrowdSec 入门指南——东方仙盟炼气期
运维·服务器·东方仙盟
未来之窗软件服务2 天前
幽冥大陆(六十二) 多数据库交叉链接系统Go语言—东方仙盟筑基期
数据库·人工智能·oracle·golang·数据库集群·仙盟创梦ide·东方仙盟
未来之窗软件服务3 天前
幽冥大陆(五十八)php1024位密码生成—东方仙盟筑基期
开发语言·算法·仙盟创梦ide·东方仙盟
未来之窗软件服务3 天前
幽冥大陆(六十) SmolVLM 本地部署 轻量 AI 方案—东方仙盟筑基期
人工智能·本地部署·轻量模型·东方仙盟·东方仙盟自动化
未来之窗软件服务3 天前
自建开发工具IDE(七)数据库集群智能升级东方仙盟数据库同化,五行八卦排序+游戏修仙,精准补齐差异还能圆武侠梦—东方仙盟筑基期
数据库·游戏·oracle·仙盟创梦ide·东方仙盟·东方仙盟架构·东方仙盟商业开发
未来之窗软件服务4 天前
幽冥大陆(五十七)ASR whisper-cli命令行使用 C语言—东方仙盟筑基期
c语言·开发语言·whisper·仙盟创梦ide·东方仙盟·东方仙盟自动化·东方仙盟商业开发
未来之窗软件服务5 天前
幽冥大陆(五十五)ASR SetThreadInformation C语言识别到自动化软件
运维·自动化·asr·东方仙盟·操作系统级别错误