print base64 image

Issues related to label design (working with databases, data processing, RFID encoding etc.) and printing (from NiceLabel Express, NiceLabel Pro, NiceForm and NicePrint)

Moderators: Georges, milos, NiceLabel Support Team

Post Reply
cedric.grun
Newcomer
Posts: 2
Joined: Thu May 07, 2020 5:31 pm

print base64 image

Post by cedric.grun » Wed Jul 08, 2020 1:46 pm

Hi,

We have an application that sends an xml file containing a base64 encoded image to an http server on automation.
When we send the image content in the nicelabel label, we go through a variable.

We decode the variable with this python code:

Code: Select all

import base64
Result.Value = base64.b64decode (content.Value)
The image object always returns a data processing error with the content of the element cannot be defined, because its value is not valid.

Could you tell us what is not done right?

User avatar
Saso
NiceLabel
NiceLabel
Posts: 2984
Joined: Mon Sep 04, 2006 8:09 am
Contact:

Re: print base64 image

Post by Saso » Wed Jul 08, 2020 2:05 pm

Well, you have two problems.
  1. When you convert base64-encoded data back into original data, you will end up with the binary content (you mentioned you send over an image). By default, the variable that you define in Automation (your "Result" variable) doesn't support binary connect, though you can define variable type as binary. However, that doesn't help much in your case, because of the problem #2 (read on).
  2. At this time, you cannot feed binary content directly into the Image object via the variable. We will add the feature to support base64-encoded data directly with the Image object in one of the next releases.
To solve a problem today, you have to store the binary content to disk and then use that .PNG file with the Image object.

You can use the code below to convert base64-data into binary and save it to disk.

Code: Select all

import System

byteData = System.Convert.FromBase64String(Result.Value)
System.IO.File.WriteAllBytes(ConfigurationFilePath.Value + '\\' + 'image.png', byteData)
This uses Python notation but .NET Framework. You do not need desktop Python installed on the PC.
FYI, ConfigurationFilePath in an internal variable containing folder name where Automation configuration is saved.

If you will use trigger concurrency (multiple requests coming in simultanously), you will have to update the code and use variable name for the image (e.g. auto-generated unique file name), not static "image.png", so the concurrent requests will not overwrite each other's data.
Saso Fleiser
Senior Technical Product Specialist

cedric.grun
Newcomer
Posts: 2
Joined: Thu May 07, 2020 5:31 pm

Re: print base64 image

Post by cedric.grun » Wed Jul 08, 2020 4:12 pm

thank you for the answer

we go through a database rather than a file to solve the concurrency problem

Post Reply