简体   繁体   中英

Decoding base64 image string to an image in Unity

I'm using the following code encode an image from python end,

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5558")

frame = cv2.imread("input.jpg")
encoded, buffer = cv2.imencode('.jpg', frame)
encoded_string = base64.b64encode(buffer)
socket.send_string("output", zmq.SNDMORE)
socket.send_pyobj(encoded_string)

Eventually, I'm using the following code to decrypt it in Unity end.

void Start()
{
    AsyncIO.ForceDotNet.Force();
    NetMQConfig.ManualTerminationTakeOver();
    NetMQConfig.ContextCreate(false);
    string topic = "output";
    subSocket = new SubscriberSocket();
    
    var timeout = new System.TimeSpan(0, 0, 1); //1sec
    string subport;
    
    subSocket.Options.ReceiveHighWatermark = 1000;
    subSocket.Connect("tcp://localhost:5558");
    subSocket.Subscribe(topic);
    bool is_connected =  subSocket.TryReceiveFrameString(timeout, out subport);
    
    Debug.Log(is_connected);

    myTexture = new Texture2D(640, 480, TextureFormat.ARGB32, false);
    
    
}

// Update is called once per frame
void Update()
{
        
    string base64string = subSocket.ReceiveFrameString();
    Debug.Log(base64string.Length);
   
    
    if (base64string.Length > 100)
    {
        byte[] imgBytes = Convert. FromBase64String(base64string);

        
        myTexture.LoadRawTextureData(imgBytes);
        myTexture.Apply();

        rawImg.texture = myTexture;
    }
}

Unfortunately it throws the following error,

FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

what am I missing?

It would be more helpful if you could try sending a very small image through the channel (encode, transfer, receive) and log-print what was received on the other end.

My suspicions :

  • Somehow you're not converting to base-64
  • You end up sending too many bytes, or unnecessary bytes (null-termination character?)
  • Receiving the image content in a manner that adds CR or LF at the end (such as 'HTTP Response' body)

More information is needed to provide a better answer...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM