本範例共需兩個 Plane,一個是動態畫面預覽,另一個是拍照完畢顯示。
將本程式碼套用至其中一個 Plane,而另一個Plane 套用至本程式碼的 public GameObject g; 之中
運行後即可有效果。
本程式碼支持 PC 的 WebCam 網路攝影機、Android 與 iOS 的攝影機。
using UnityEngine;
using System.Collections;
public class MyCamera : MonoBehaviour {
public GameObject g;
WebCamTexture c;
void Start () {
c = new WebCamTexture (WebCamTexture.devices[0].name);
renderer.material.mainTexture = c;
c.Play ();
}
void OnGUI(){
if(GUI.Button(new Rect(0, 0, 100, 50), "拍照")){
Texture2D t = new Texture2D(c.width, c.height);
t.SetPixels(c.GetPixels());
t.Apply();
g.renderer.material.mainTexture = t;
// System.IO.File.WriteAllBytes("image.png", t.EncodeToPNG()); // 如果你要保存至硬碟or記憶卡的話
}
}
}

你好 我想請問這個拍照的程式 只要將這個程式丟入NGUI 然後匯出到android平板 點選按鈕就能拍照嗎? 我們這樣嘗試過 但點擊按鈕沒有任何反應 能請問還缺少了什麼步驟或是有什麼步驟做錯了嗎?
這個範例不是用 NGUI,你可以在外面建立一個 Cube 或 Plane, 把它拖進 public GameObject g; 中 而這個程式碼本身存放的地方也是 Cube 或 Plane 這樣就可以囉
System.IO.File.WriteAllBytes("image.png", t.EncodeToPNG()); 老師請問一下上方程式碼儲存的路徑是指定在哪裡呢?
Unity 專案中喔 ! 例如:C:\Users\Loli\Documents\New Unity Project\ 當然你也可以指定路徑,例如以下我放在 D:/ 磁碟機 System.IO.File.WriteAllBytes("D:/image.png", t.EncodeToPNG());
我輸入此程式 System.IO.File.WriteAllBytes("C:/image.png", t.EncodeToPNG()); 出現在此錯誤訊息,目前只有辦法將圖片存在本身專案的資料夾內, 請問老師我哪裡設定錯誤了嗎? 他說不可訪問。 Access to the path "C:\image.png" is denied. System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/FileStream.cs:320) System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int) System.IO.File.Create (System.String path, Int32 bufferSize) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/File.cs:135) System.IO.File.Create (System.String path) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/File.cs:130) System.IO.File.WriteAllBytes (System.String path, System.Byte[] bytes) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/File.cs:594) MyCamera.OnGUI () (at Assets/scene/MyCamera.cs:22) 以上,感謝老師!
不建議放在 C 磁碟機 底下(所以我才故意寫 D:/ ),因為 C 的權限很高,一般程式是無法存取的
原來如此!!! 感謝老師!
請問老師WebCamTexture.devices[0].name 的devices[0],這裡指的是第幾個CAMER的意思嗎? 如果我有另一個攝影機是不是就可以宣告成WebCamTexture.devices[1].name ( 如果是的話,後續接上的Camera的順序將會變成其陣列的順序對嗎? ) 以上,感謝老師!
沒錯,陣列裡面是輸入要用第幾台攝影機
我想要判斷 webcam 是否有啟動抓到,我該怎麼做呢?
if(WebCamTexture.devices.Length == 0){ print ("找不到實體攝影機"); } 詳細可以參考這邊:http://lolikitty.pixnet.net/blog/post/182776173
老師我想要在 UNITY 的遊戲開啟 WEBCAM 邊錄影儲存影像。 網路上查了一下是轉成 AVI 或是 WMV 都好大.... 要將WEBCAM的影像錄下來,請問有辦法儲存成小檔案的影音格式嗎? 或是有什麼解決辦法嗎? 以上,感謝老師
很大沒關係啊...之後用 FFMPEG 轉成小檔案 ? 但是我不確定你要做什麼...如果只是要變成小檔案...然後傳到網路上...可以用上面那種做法
那麼不考慮格式大小問題,直接轉出可以轉出 AVI 的話呢? 我找到下面這個網址的教學,實際操作遇到好多問題↓ http://a7419.pixnet.net/blog/post/88913093-c%23-%E4%BD%BF%E7%94%A8webcam%E9%8C%84%E8%A3%BD%E5%BD%B1%E7%89%87(.avi) 他很多函式都是針對 WindowsForm 開發的,像是 using System.Windows.Forms.dll 的使用 UNITY就不支援了,請問老師有沒有內建的解決方法可以轉存影片呢? 以上感謝老師~
內建是沒有的,都是要外部擴充 or 外掛 可以去找看看有沒有 AVI or MP4 編碼器 像是 FFMPEG、LibAV 都可以做到
老師我照著上面的那個教學式著轉程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 = false; public GameObject pic_view; Texture webcam_texture; string _string_DateTime; string _string_AVIName; // Use this for initialization void Start () { _capture = new Capture(); Image frame = _capture.QueryFrame();
webcam_texture = pic_view.GetComponent().material.mainTexture;
}
// Update is called once per frame
void Update () {
if (_capture != null)
{
if (_captureInProgress)
{
_captureInProgress = false;
print("開始");
//Application.Idle -= Application_Idle;
}
else
{
_captureInProgress = true;
print("結束");
}
}
}
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 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();
int c = CvInvoke.cvWaitKey(20);
video.WriteFrame(temp);
if (c == 27) break;
}
video.Dispose();
CvInvoke.cvDestroyWindow("camera");
}
}
}
===========================上方為我修改程式碼
_capture = new Capture(0); //預設使用第一台的webcam
↑ 上列這個指定攝影機的部分錯誤,該怎麼解決呢?
DllNotFoundException: opencv_core231
Emgu.CV.CvInvoke..cctor ()
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for Emgu.CV.CvInvoke
Emgu.CV.Capture..ctor (Int32 camIndex)
EmhuCV_text.Start () (at Assets/script/EmhuCV_text.cs:22)
他說找不到 Dll 檔 不是每個 Dll 都可以被 Unity 使用 另外這個還有接 OpenCV ,不是單純的 C# 而已
老師我有去另外載入 OpenCV 了。 現在的新問題是無法寫入 frame... 當我按下 "錄製" 按鈕就出現錄製視窗,並出現以下錯誤 ( 視窗顯示的是WEBCAM的畫面,靜止 ) 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:71) 錯誤的那段程式碼↓ while(temp !=null) { CvInvoke.cvShowImage("camera", temp.Ptr); temp = _capture.QueryFrame(); int c = CvInvoke.cvWaitKey(20); video.WriteFrame(temp); <------------ 錯誤點
if (c == 27) break;
}
P.S 按下錄製按鈕跳出的視窗,應該是因為發生錯誤所以停止,進而顯示為第一個 frame 的畫面靜止不動。( 學生這樣猜測的)
是只有 Unity 出錯嗎? 要不要試試看用微軟的 Visual C# 來開發 然後再用插件的方式掛進 Unity 中 我常常遇到 Visual C# 的程式碼在 Unity 中不能順利執行,需要另外寫然後再掛進去
好的,我試試看 感謝老師
請問老師,我另外用VS寫好了錄影與截圖的功能之後,能夠用UNITY去控制此程式的按鈕嗎? 像是開始錄影 或 截圖、結束錄影。 另外,我也想在UNITY的UI中顯示 WEBCAM 的畫面,這樣會不會跟我用VS寫好的程是衝突呢? (學生認為 WEBCAM 的 FRAME 被VS的程是搶走,所以 UNITY 會抓不到WBECAM 的資料 ) 這部分請問老師會怎麼解決呢? 以上,感謝老師!
請問老師 我丟程式碼進去一個Plan裡面 然後將 cube丟進 GameObject g 之後按下拍照卻顯示以下2個錯誤,我看不懂是甚麼意思 IndexOutOfRangeException: Array index is out of range. NullReferenceException: Object reference not set to an instance of an object 請老師幫我解答,感謝老師
可能是下面這條程式碼出問題,devices[0] 抓取的是第一台CAMERA,如果你沒有該變此數值,那就是沒有抓到任何攝影機。 ( devices["webcam 插入的順序編號"] ) c = new WebCamTexture (WebCamTexture.devices[0].name);
老師您好 我想用一個螢幕截圖然後存到手機相簿的程式 我參考了這篇文章 http://game.ceeger.com/forum/read.php?tid=17668&fid=27&page=1 他存檔的部分 File.WriteAllBytes(Application.persistentDataPath + "/sdcard/DCIM/ARphoto/photo-"+i+".png",imagebytes); 都無法存檔 有甚麼解決的方式嗎? 請老師幫我解答 感謝老師
老師您好 我有一個問題 我現在可以成功截圖 不過手機要重新開機才看的到照片
你好, 老師, 我想問 如何將另一個Plane 套用至本程式碼的 public GameObject g? 謝謝