|
各位老師好
學生想要達成在 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 錄影轉存影片的範例可以指導學生嗎?
以上,感謝各位老師!
|
|