Commit 26bda0f1 authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

Example of files to access camera

parent bad6f0c3
// JA : 20/09/2020 Script unity pour l'envoie d'une caméra à travers un websocket
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NativeWebSocket;
//using System.Diagnostics;
using Debug = UnityEngine.Debug;
using System.Text;
using System;
using System.Diagnostics;
public class ImageWriter : MonoBehaviour
{
public string url = "ws://127.0.0.1:9999";
WebSocket websocket;
bool needUpdate;
Camera camera;
Texture2D texture;
private string img;
// Start is called before the first frame update
void Start()
{
camera = GetComponent<Camera>();
texture = new Texture2D((int)camera.targetTexture.width, (int)camera.targetTexture.width);
//img = GetRawTextureData(camera);
StartCoroutine("Connect");
StartCoroutine("UpdateCameraData");
}
async void Connect()
{
websocket = new WebSocket(url);
websocket.OnOpen += () =>
{
Debug.Log("Connection open sender image!");
};
websocket.OnError += (e) =>
{
Debug.Log("Error! " + e);
};
websocket.OnClose += (e) =>
{
Debug.Log("Connection closed!");
};
websocket.OnMessage += (bytes) =>
{
var message = System.Text.Encoding.UTF8.GetString(bytes);
needUpdate = true;
Debug.Log("mess:" + message);
//commands = JsonUtility.FromJson<SerializableCommands>(message);
// reconvert(img);
websocket.SendText(img);
};
await websocket.Connect();
}
IEnumerator UpdateCameraData()
{
while (true)
{
yield return new WaitForEndOfFrame();
img = GetRawTextureData(camera);
}
}
public static void SaveTextureAsPNG(Texture2D _texture, string _fullPath)
{
byte[] _bytes = _texture.EncodeToPNG();
Debug.Log("Debug size:" + _bytes.Length);
System.IO.File.WriteAllBytes(_fullPath, _bytes);
Debug.Log(_bytes.Length / 1024 + "Kb was saved as: " + _fullPath);
}
private void reconvert(string encoded)
{
byte[] decodedBytes = Convert.FromBase64String(encoded);
/* foreach(var val in decodedBytes)
{
Debug.Log(val);
}*/
Texture2D tex= new Texture2D((int)camera.targetTexture.width, (int)camera.targetTexture.width);
tex.LoadImage(decodedBytes);
tex.Apply();
SaveTextureAsPNG(tex, "C://Users/Julien/Desktop/test_robotic_arm/rewrite_img.png");
}
private string GetRawTextureData(Camera camera)
{
RenderTexture.active = camera.targetTexture;
texture.ReadPixels(new Rect(0, 0, (int)camera.targetTexture.width, (int)camera.targetTexture.width), 0, 0);
texture.Apply();
//SaveTextureAsPNG(texture, "C://Users/Julien/Desktop/test_robotic_arm/toto.png");
return Convert.ToBase64String(texture.EncodeToJPG());
}
// Update is called once per frame
void Update()
{
if (needUpdate)
{
//img = GetRawTextureData(camera);
//reachy.HandleCommand(commands);
needUpdate = false;
}
}
}
ImageWriter.cs
--------------
Example of file to access image in unity. Can be useful to simulate cameras
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment