Waltari
Waltari

Reputation: 1259

Load sprite from a base64 string which came from a websocket

I am trying to turn a base64 String into an Sprite in Unity 3D, but my sprite in scene remains blank.

public var cardPicture : Image;

function ReceiveData(jsonReply : JSONObject) {
    var pictureBytes : byte[] = System.Convert.FromBase64String(jsonReply.GetString("picture"));
    var cardPictureTexture = new Texture2D( 720, 720);
    Debug.Log(cardPictureTexture.LoadImage(pictureBytes));
    var sprite : Sprite = new Sprite ();
    sprite = Sprite.Create (cardPictureTexture, new Rect (0,0,720,720), new Vector2 (0.5f, 0.5f));
    cardPicture.overrideSprite = sprite;
}

This prints out true, but I am not sure if it is loading the image appropriately from the bytes or if something else is going wrong. I am not sure what to check in order to determine what is going wrong either. Assigning some picture to the cardPicture in scene displays correctly.

I logged the jsonReply.picture and used an online base64 to image converter and it displayed the image correctly.

Upvotes: 0

Views: 2363

Answers (3)

Saad Anees
Saad Anees

Reputation: 1430

I don't know if this is solved but i want to share my solution.

void Start()
{
    StartCoroutine(GetQR());
}

IEnumerator GetQR()
{
    using (UnityWebRequest www = UnityWebRequest.Get(GetQR_URL))
    {
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            // Show results as text
            Debug.Log(www.downloadHandler.text);
            
            QRData qr = JsonUtility.FromJson<QRData>(www.downloadHandler.text);

            string result = Regex.Replace(qr.img, @"^data:image\/[a-zA-Z]+;base64,", string.Empty);

            CovertBase64ToImage(result);
        }
    }
}

void CovertBase64ToImage(string img)
{
    byte[] bytes = Convert.FromBase64String(img);
    Texture2D myTexture = new Texture2D(512,212);
    myTexture.LoadImage(bytes);
   
    Sprite sprite = Sprite.Create(myTexture, new Rect(0, 0, myTexture.width, myTexture.height), new Vector2(0.5f, 0.5f));
    QRimage.transform.parent.gameObject.SetActive(true);
    QRimage.sprite = sprite;
}

It is working perfectly on unity version 2019.4

Upvotes: 0

Ebleme
Ebleme

Reputation: 307

 byte[] pictureBytes = System.Convert.FromBase64String(jsonReply.GetString("picture"));

 Texture2D tex = new Texture2D(2, 2);
 tex.LoadImage( imageBytes );

 Sprite sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);

 cardPicture.overrideSprite = sprite;

Upvotes: 1

Dinal24
Dinal24

Reputation: 3192

I assume you are trying to fetch an image from a remote url and trying to parse bytes into a texture. In unity WWW has facilitated this and does not require user involvement in conversion.

I believe your response may have header details which might cause issues in converting to a texture. You may use a code like below,

public string Url = @"http://dummyimage.com/300/09f/fff.png";

    void Start () {
        // Starting a coroutine to avoid blocking
        StartCoroutine ("LoadImage");
    }

    IEnumerator LoadImage()
    {
        WWW www = new WWW(Url);
        yield return www;

        Debug.Log ("Loaded");
        Texture texture = www.texture;
        this.gameObject.GetComponent<Renderer>().material.SetTexture( 0,texture );
    }

Upvotes: 0

Related Questions