- Home /
Self answered
How to take a photo from a webcam?
Hi I'm having some trouble taking a photo and saving it from a webcam, ideally I'm trying to get this working on iOS.
Answer by 199christoff · Feb 02, 2013 at 09:44 AM
Just want to share this with people as they may find it useful, after some additional research and trial and error I managed to solve my query.
Feel free to use the code.
using UnityEngine;
using System.Collections;
public class WebcamTest : MonoBehaviour {
public string deviceName;
WebCamTexture wct;
// Use this for initialization
void Start () {
WebCamDevice[] devices = WebCamTexture.devices;
deviceName = devices[0].name;
wct = new WebCamTexture(deviceName, 400, 300, 12);
renderer.material.mainTexture = wct;
wct.Play();
}
// For photo varibles
public Texture2D heightmap;
public Vector3 size = new Vector3(100, 10, 100);
void OnGUI() {
if (GUI.Button(new Rect(10, 70, 50, 30), "Click"))
TakeSnapshot();
}
// For saving to the _savepath
private string _SavePath = "C:/WebcamSnaps/"; //Change the path here!
int _CaptureCounter = 0;
void TakeSnapshot()
{
Texture2D snap = new Texture2D(wct.width, wct.height);
snap.SetPixels(wct.GetPixels());
snap.Apply();
System.IO.File.WriteAllBytes(_SavePath + _CaptureCounter.ToString() + ".png", snap.EncodeToPNG());
++_CaptureCounter;
}
}
Hi Oily. (the above code doesn't really work) Realistically you have to get the "NatCam" plug in, it's the only real solution for using the camera in Unity. The single biggest thing missing from Unity is they have not yet done camera for android/ios. (the supplied placeholder system from Unity is a total joke.) It's just simply not possible to write your own native plugin for camera handling - it takes about 18 man-months of work. 100% of projects that need actual native camera just use natcam - as soon as it was released it was one of the handful of plugins you just have to have to work. until Unity actually make camera software, that's how it is :O
NatCam is very expensive. I've actually figure it out for Android and iOS (This afternoon).
Just checked it on 3 different platforms and it's working fine :-)
Just a $$anonymous$$or rotational issue that's fixable as the WebCamTexture lets you know how far you should rotate to be facing the correct way.
Answer by DeveshPandey · Dec 26, 2015 at 05:04 AM
If you want to take photo from device native camera then you can use this very good plugin
Follow this Question
Related Questions
Does PersistentDataPath get overwritten on mobile? 1 Answer
WebCamTexture and VSync (iPhone 4) 3 Answers
Save game in iOS background 0 Answers
Dinamic name for data save 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers