Introduction:
I am reading image data of fingerprints from smart card and as you know this data save as raw image in smart card. I am developing a client side program which only use java script to read image from scanner of a card reader and show that in the client page.
Now my question:
How can I convert hex string of my raw data to a hex string which accomplished with appropriate header of bitmap image? Note that I have width
and height
of my image.
Tried methods:
I have been developed this program in java by get buffered image from raw data. Also, I could convert a hex string of a bit map image to base64 by Hex2Base64 and then I could show base64 string in an image tag by base64AsImage. However these functions work well if and only if the hex contains header, while our data is raw.
My code (that only works for Hex String which contains header):
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script>
if (!window.atob) {
var tableStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var table = tableStr.split("");
window.atob = function (base64) {
if (/(=[^=]+|={3,})$/.test(base64)) throw new Error("String contains an invalid character");
base64 = base64.replace(/=/g, "");
var n = base64.length & 3;
if (n === 1) throw new Error("String contains an invalid character");
for (var i = 0, j = 0, len = base64.length / 4, bin = []; i < len; ++i) {
var a = tableStr.indexOf(base64[j++] || "A"), b = tableStr.indexOf(base64[j++] || "A");
var c = tableStr.indexOf(base64[j++] || "A"), d = tableStr.indexOf(base64[j++] || "A");
if ((a | b | c | d) < 0) throw new Error("String contains an invalid character");
bin[bin.length] = ((a << 2) | (b >> 4)) & 255;
bin[bin.length] = ((b << 4) | (c >> 2)) & 255;
bin[bin.length] = ((c << 6) | d) & 255;
};
return String.fromCharCode.apply(null, bin).substr(0, bin.length + n - 4);
};
window.btoa = function (bin) {
for (var i = 0, j = 0, len = bin.length / 3, base64 = []; i < len; ++i) {
var a = bin.charCodeAt(j++), b = bin.charCodeAt(j++), c = bin.charCodeAt(j++);
if ((a | b | c) > 255) throw new Error("String contains an invalid character");
base64[base64.length] = table[a >> 2] + table[((a << 4) & 63) | (b >> 4)] +
(isNaN(b) ? "=" : table[((b << 2) & 63) | (c >> 6)]) +
(isNaN(b + c) ? "=" : table[c & 63]);
}
return base64.join("");
};
}
function hexToBase64(str) {
return btoa(String.fromCharCode.apply(null,
str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" "))
);
}
function base64ToHex(str) {
for (var i = 0, bin = atob(str.replace(/[ \r\n]+$/, "")), hex = []; i < bin.length; ++i) {
var tmp = bin.charCodeAt(i).toString(16);
if (tmp.length === 1) tmp = "0" + tmp;
hex[hex.length] = tmp;
}
return hex.join(" ");
}
function doConvert() {
var myHex = document.getElementById('myText').value;
var myBase64 = hexToBase64(myHex);
document.getElementById('myImage').src = "data:image/bmp;base64," + myBase64;
}
</script>
</head>
<body>
<div>
<p>
Enter Raw Hex:
<br>
<textarea rows="4" cols="50" id="myText">Enter Raw Hex String here ...</textarea>
<br>
<button id="myButton" onclick="doConvert()"> Click me </button>
<br>
<img id="myImage" alt="img1"/>
</p>
</div>
</body>
</html>
a part of code which solve the problem in java:
private static BufferedImage byte2Buffered(byte[] rawData, int width, int height){
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
byte[] array = ((DataBufferByte)image.getRaster().getDataBuffer()).getData();
System.arraycopy(rawData, 0, array, 0, rawData.length);
return image;
}
Notice that, as there is not BufferedImage
type in JavaScript, we could not equivalent this approach in JavaScript.