Hi people,
I have a problem with load an image from webcam. First I have a webcam that capture image like camera phone. Then I want to display that photo into plane at another scene. But when I try to display an image, my app always crash. The image save on asset folder (android/data/mycomm/files/image.png). Please help me...
Here's my code
First Scene
using UnityEngine;
using System.Collections;
using System.IO;
public class Satu : MonoBehaviour
{
WebCamTexture webCamTexture;
string deviceName="";
bool isPhoto;
float TimerPhoto;
string fileName = "";
string path = "";
public Texture2D heightmap;
public Vector3 size = new Vector3(100, 10, 100);
void Start()
{
isPhoto = false;
TimerPhoto = 0;
WebCamDevice[] devices = WebCamTexture.devices;
deviceName = devices[0].name;
webCamTexture = new WebCamTexture(deviceName, 400, 300, 12);
renderer.material.mainTexture = webCamTexture;
webCamTexture.Play();
}
// Update is called once per frame
void Update()
{
if (isPhoto)
{
TimerPhoto += Time.deltaTime;
if (TimerPhoto > 5)
{
Application.LoadLevel("2");
TimerPhoto = 0;
isPhoto = false;
}
}
}
void OnGUI()
{
if (GUI.Button(new Rect(0, 0, 100, 50), "Capture"))
{
fileName = "image.png";
TakePhoto();
Debug.Log(path);
isPhoto = true;
}
}
void TakePhoto()
{
Texture2D snap = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.RGB24, false);
snap.SetPixels(webCamTexture.GetPixels());
snap.Apply();
System.IO.File.WriteAllBytes(Application.persistentDataPath + "/image.png", snap.EncodeToPNG());
}
}
And the second scene for loadimage
using UnityEngine;
using System.Collections;
using System.IO;
public class Dua : MonoBehaviour {
void Start()
{
StartCoroutine(Show());
}
// Update is called once per frame
void Update () {
}
IEnumerator Show()
{
yield return new WaitForEndOfFrame();
string FileName = Application.persistentDataPath + "/image.png";
byte[] bytes = File.ReadAllBytes(FileName);
Texture2D tex = new Texture2D(320, 420);
tex.LoadImage(bytes);
renderer.material.mainTexture = tex;
yield return null;
}
}
thank you very much
↧