OpenCV中文网站

 找回密码
 立即注册
搜索
热搜: 安装 配置
查看: 3249|回复: 0

學生想要用在UNITY裡面利用WEBCAM錄製影像存AVI

[复制链接]
发表于 2015-9-23 13:54:03 | 显示全部楼层 |阅读模式
各位老師好
學生想要達成在 UNITY 裡面利用 WEBCAM 錄製影像轉存 AVI 的效果
我找到一個教學,將其寫法轉為UNITY的寫法,如下 ( 這樣的寫法不知道是不是有很大的錯誤 )

using UnityEngine;
using System.Collections;
using System.IO;
using System;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using System.Threading;
using Emgu.CV.CvEnum;

public class EmhuCV_text : MonoBehaviour
{
    private Capture _capture;
    private bool _captureInProgress = true;
    public GameObject pic_view;
    Texture webcam_texture;
    string _string_DateTime;
    string _string_AVIName;
        // Use this for initialization
        void Start () {
        _string_AVIName = "text";         
        _capture = new Capture();
        Image<Bgr, byte> frame = _capture.QueryFrame();
        webcam_texture = pic_view.GetComponent<Renderer>().material.mainTexture;

        if (_capture != null)
        {
            if (_captureInProgress)
            {
                _captureInProgress = false;
                print("開始");
                //Application.Idle -= Application_Idle;
            }
            else
            {
                _captureInProgress = true;
                print("結束");
            }
        }
        }
       
        // Update is called once per frame
        void Update () {


        }

    void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 100, 50), "關閉"))
        {
            _captureInProgress = false;
            print("關閉");
            _capture.Dispose();
        }

        if (GUI.Button(new Rect(100, 0, 100, 50), "錄製"))
        {
            _capture = new Capture();
            Image<Bgr, byte> temp = _capture.QueryFrame();
            _string_DateTime = DateTime.Now.ToString("yyyyMMddhhmmss");
            VideoWriter video = new VideoWriter(_string_AVIName, CvInvoke.CV_FOURCC('X', 'V', 'I', 'D'), 20, 800, 600, true);

            while(temp !=null)
            {
                CvInvoke.cvShowImage("camera", temp.Ptr);
                temp = _capture.QueryFrame();
                video.WriteFrame<Bgr, byte>(temp);
                int c = CvInvoke.cvWaitKey(20);

                if (c == 27) break;

            }

            video.Dispose();
            CvInvoke.cvDestroyWindow("camera");
        }
    }
}


============================ 運行之後,點擊 "錄製" 按鈕後,錯誤訊息如下 ==========================

InvalidOperationException: Unable to write frame to the video writer
Emgu.CV.VideoWriter.WriteFrame[Bgr,Byte] (Emgu.CV.Image`2 frame)
EmhuCV_text.OnGUI () (at Assets/script/EmhuCV_text.cs:68)


錄製按下之後跳出 VIEW 之後,停在第一個 frame,WEBCAM就沒有再更新到第二個 frame 了
各位老師可以幫幫學生解決這個錯誤嗎?
還是說有其他更簡單的 WEBCAM 影像轉存影片或是 UNITY 裡讓 WECAM 錄影轉存影片的範例可以指導學生嗎?

以上,感謝各位老師!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|OpenCV中文网站

GMT+8, 2024-3-29 14:33 , Processed in 0.010413 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表