Start a new topic

Get target image from camera in Unity

Get target image from camera in Unity

1 person has this problem

Hi.


I apologize for the late reply.


Here is a snippet of code that captures the frame and saves it on disk

 

using UnityEngine;
using System.Collections;
using Wikitude;
using System;
using System.IO;

public class ScreenshotCapture : MonoBehaviour {

	private WikitudeCamera _wikitudeCamera;

	private RenderTexture _targetRenderTexture;
	private Texture2D _cameraFeed;

	void Start () {
		_wikitudeCamera = FindObjectOfType<WikitudeCamera>();
		if (_wikitudeCamera == null) {
			Debug.LogError("No WikitudeCamera found!");
			Destroy(this);
		} else {
			CreateTextures();
		}
	}
	
	private void CreateTextures() {
		_targetRenderTexture = new RenderTexture (Screen.width, Screen.height, 24);
		_cameraFeed = new Texture2D (Screen.width, Screen.height);
	}

	void Update () {
		if (Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) {
			string path = Path.Combine(Application.persistentDataPath, "wikitudeFeedImg_" + System.DateTime.Now.ToString("hh_mm_ss") + ".png");
			Debug.Log("path: " + path);
			StartCoroutine(CaptureScreenshot(_wikitudeCamera, path));
		}
	}

	private IEnumerator CaptureScreenshot(WikitudeCamera wikitudeCamera_, string path_) 		{
		yield return new WaitForEndOfFrame ();
		
		if (_targetRenderTexture.width != Screen.width || _targetRenderTexture.height != Screen.height) {
			CreateTextures();
		}

		Graphics.Blit(wikitudeCamera_.CameraTexture, _targetRenderTexture);
		RenderTexture.active = _targetRenderTexture;

		_cameraFeed.ReadPixels(new Rect(0, 0, _cameraFeed.width, _cameraFeed.height),0,0);
		_cameraFeed.Apply();

		byte[] bytes;
		bytes = _cameraFeed.EncodeToPNG();
		File.WriteAllBytes(path_, bytes);
	}
}

 


Best regards,

Alexandru.

The link just takes me back to the forum home?

My mistake. 

I updated the previous post.


Best regards,

Alexandru

Hi! is there a way to render the camera texture to a RawImage without Wikitude also rendering the camera feed in the background?

 

I can render to a custom image just fine with rawImage.texture = wikitudeCamera.CameraTexture; but only as long as the WikitudeCamera component has Enable Camera Rendering set to true in the Editor, which makes it render the texture to the background as well. If I set it to false I get no background but also no texture (white image) to render to the RawImage.

 

Is this even possible (outside of covering the background with something or doing ugly hacks)?

 

Thanks!

Hi, 


There currently isn't a proper way to do this.


However, the background is rendered by a hidden object named "BackgroundCamera", so you could simply destroy it and rendering will also stop. This object is created during the Start method of the WikitudeCamera, so make sure to wait for that before searching for the BackgroundCamera. Also make sure to enable clearFlags on your main camera, as they are set to DepthOnly by default.


Here is a short snippet of how this can be done:


IEnumerator Start() {

    yield return null;

    Destroy(GameObject.Find("BackgroundCamera"));

    Camera.main.clearFlags = CameraClearFlags.Color;

}


Best regards,

Alexandru

In case anybody is trying to do this, the earlier method doesn't work -- at least on iOS, wikitudecamera.cameraTexture doesn't get updated unless the BackgroundCamera's camera is present and active (as soon as you disable the camera, cameraTexture no longer updates). So I had to just send that camera backwards in depth so I could draw over it. This works differently in the editor, which made this a huge pain to debug. 


And I was only able to do that because of the comment above that happens to mention the hidden gameobject. Not sure why that's hidden -- only makes working with wikitude harder. 

Login or Signup to post a comment