Home > Back-end >  I want to convert image data(Texture2D) to Base64 string and store in JSON file in Unity3D(C#)
I want to convert image data(Texture2D) to Base64 string and store in JSON file in Unity3D(C#)

Time:09-30

I want to convert image(Texture2D) data to Base64 string and store in JSON format. as of now I am getting image texture, it's a kind of screenshot from live rendering camera. And I want the same image data into JSON format. Please suggest me with any example.

This is how I am converting texture to Base64 then JSON.

Application.ExternalCall("screenshot", System.Convert.ToBase64String(texture.EncodeToPNG()));
string TextureArray = System.Convert.ToBase64String(texture.EncodeToPNG());
string serializedAbilites = JsonUtility.ToJson(TextureArray);
File.WriteAllText(Application.dataPath   "/File.json", TextureArray);
  • Unity Version: 2020.3.11f1,
  • language: C#

CodePudding user response:

I know this kind of answer is kind of annoying but doing this is a bad idea, if you really need to save texture data you should save it as binary and not as a string, you will user more bytes, over complicate your code and in general it's not a good practice.

HOWEVER, I will still answer the question, but please avoid this. If you want to save this string as a json, you will have to specify that its serializable as c# sees strings as not suitable for serialization (this is false however: Is String not serializable by default in .Net Core?), add this behind your string and your code should work (in case you are wondering this is called an attribute, it's sort of like a tag):

[Serializable]

NOW, for the correct way of doing this we will take an example code from the unity docs (an extremely helpful tool by the way): Texture2D.EncodeToPNG

// Encode texture into PNG
        byte[] bytes = tex.EncodeToPNG();
        Object.Destroy(tex);

        // For testing purposes, also write to a file in the project folder
        // File.WriteAllBytes(Application.dataPath   "/../SavedScreen.png", bytes);

This piece of code takes a texture 2d class, converts it to bytes (just like you are already doing) and then writes it as binary using File.WriteAllBytes(). You should also destroy the texture object (Object.Destroy(Your texture)) to prevent the game from storing unnecessary bytes which might cause lag spikes from the garbage collector (I think).

But all of this is useless for you're case, if you want to screenshot the main camera you can just use unity's built in function, which also insures cross platform compatibility:

ScreenCapture.CaptureScreenshot("your path");

If you need an higher-res OR lower-res screenshot, add an multiplier argument after the path, like this:

ScreenCapture.CaptureScreenshot("your path", SizeMultiplier);

So, to wrap this up, do something like this and you should be good to go:

ScreenCapture.CaptureScreenshot(Application.dataPath   "/File.json");

CodePudding user response:

I don't understand most of your code

like e.g. what is this line doing?

Application.ExternalCall("screenshot", System.Convert.ToBase64String(texture.EncodeToPNG()));

Is this supposed to be a callback of some sort? Seems like you encode some texture to a string and then never use that string for anything.

And then you use texture.EncodeToPNG again right after that which is pretty expensive.

Then you already get a string. So what is in your eyes supposed to be the outcome of

string serializedAbilites = JsonUtility.ToJson(TextureArray);

And finally you get the serializedAbilities but then anyway don't use it at all but rather write to the file

File.WriteAllText(Application.dataPath   "/File.json", TextureArray);

I guess what you rather would do to get a JSON would be simply wrapping this into a valid JSON object like e.g.

{
    "content" : "..........Your encoded string here ........."
}

So you could very easily achieve what you want to do by using

var json = "{\"content\":\""   TextureArray   "\"}";
File.WriteAllText(Path.Combine(Application.dataPath, "File.json"), json);

Or alternatively have an actual wrapper class for your JSON layout like

[Serializable]
public class ImageJson
{
    public string content;
}

and then use

var imageJson = new ImageJson
{
    content = TextureArray 
};

var json = JsonUtility.ToJson(imageJson);
File.WriteAllText(Path.Combine(Application.dataPath, "File.json"), json);
  • Related