ParcelTrack API
Binary Signature Decode Samples
Tracking events may include an encoded signature. The following code samples below can be used to decode the signature image.
C# Sample
namespace SigDisplay
{
public partial class SigDisplay : Form
{
public SigDisplay()
{
InitializeComponent();
}
private void btnConvert_Click(object sender, EventArgs e)
{
// convert the original XML data into binary
byte[] bin = System.Convert.FromBase64String(txtEncoded.Text);
lblSize.Text = bin.Length.ToString() + " bytes";
// construct a string version of the binary data
string s = "";
bool b = false;
// create a graphics image of the binary data
Bitmap img = new Bitmap(pb.Width, pb.Height);
Graphics g = Graphics.FromImage(img);
g.Clear(Color.White);
Pen p = new Pen(Color.Black, 1);
// run the binary data, capturing the pairs and writing to image and string
Point p1 = new Point();
Point p0 = new Point();
bool st = false;
for (int i = 0; i < bin.Length; i++)
{
if (bin[i] == 255) // its the pen-down trigger
{
if (i > 0)
s += "\r\n"; // start as a new line
b = false; // and mark as a new pen
st = false;
}
else
{
if (b) // save the X co-ord
p1.X = bin[i];
else
{
p1.Y = bin[i]; // save the Y co-ord
if (st) // draw a line if we have a start and an end
g.DrawLine(p, p0, p1);
p0 = p1; // remember this point as the start of the next point
st = true; // have got a start co-ordinate for the line now
}
}
s += bin[i].ToString() + (b ? "," : " ");
b = !b; // getting the 'other' byte
}
// and update the form
txtDecoded.Text = s;
pb.Image = img;
}
}
}
Base64 Sample
function draw_signature() {
const data = atob(b64);
const bin = Uint8Array.from(data, (b) => b.charCodeAt(0));
// construct a string version of the binary data
let s = "";
let b = false;
// create a canvas image of the binary data
const canvas = document.getElementById("pb");
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = "black";
ctx.lineWidth = 1;
// run the binary data, capturing the pairs and writing to image and string
let p1 = { x: 0, y: 0 };
let p0 = { x: 0, y: 0 };
let st = false;
for (let i = 0; i < bin.length; i++) {
if (bin[i] === 255) {
// its the pen-down trigger
if (i > 0) s += "\r\n"; // start as a new line
b = false; // and mark as a new pen
st = false;
} else {
if (b)
// save the X co-ord
p1.x = bin[i];
else {
p1.y = bin[i]; // save the Y co-ord
if (st) {
// draw a line if we have a start and an end
console.log("Drawing line from ", p0, " to ", p1);
ctx.beginPath();
ctx.moveTo(p0.x, p0.y);
ctx.lineTo(p1.x, p1.y);
ctx.stroke();
}
p0 = { ...p1 }; // remember this point as the start of the next point
st = true; // have got a start co-ordinate for the line now
}
}
b = !b; // getting the 'other' byte
}
}