Display byte array in html without conversion to Base64 string - javascript

I know that it is possible to display byte arrays as images in html after conversion to Base64 string as explained: Here
Is it possible in html to display the raw byte array as Image without conversion to string?

the window.URL.createObjectURL Method creates a URL for the byte array image directly without conversion
var link = window.URL.createObjectURL(data);
var image = document.getElementById("liveBox");
image.src = link;

Related

Converting binary data to base64 in Google appscript

I'm getting an input data in appscript in the form of binary data (docx file data) which looks something like below
I need this data to be converted into Base64 string. I tried using Utilities class to encode it into base64, however, it returns a few character string which is invalid. Is there any way to convert this form of data in appscript?
The current script is as below
function run() {
var inputData = Eventbus.get('encodedData');//this is received as binary data
var convertedData = Utilities.base64Encode(inputData);//need to encode to base64 but doesn't work
Eventbus.set('decodedData',convertedData);
}
Thanks
Saurabh

How to create and parse Tag, Length, Value (TLV) in JavaScript or java and encode it in Base64

The QR code fields shall be encoded in Tag-Length-Value (TLV) format with the tag values specified in the “Tag” column of the adjacent table in the image below.
The TLV encoding shall be as follows:
Tag: the tag value as mentioned above is stored in one byte
Length: the length of the byte array resulted from the UTF8 encoding of the field value. The length shall be stored in one byte.
Value: the byte array resulting from the UTF8 encoding of the field value.
This image describe my problem:
You can generate the required hashes by using this library here.
String qrBarcodeHash = QRBarcodeEncoder.encode(
new Seller("شركة تجريبية"),
new TaxNumber("312345678901233"),
new InvoiceDate("2021-11-18T18:40:34+03:00"),
new InvoiceTotalAmount("2.30"),
new InvoiceTaxAmount("0.30")
);

How to convert an image file into its base64 representation without the leading prefix: "data:image/jpeg;base64" in js&html5

I have below codes in my frontend:
let reader = new FileReader();
reader.onload = function(e) {
_this.imageBase64 = e.target.result;
alert(_this.imageBase64);
...
}
the alert function shows that the imageBase64 is a base64 string, which is starting with data:image/jpeg;base64.
The problem is that, is there any elegant way that I can get the base64 string of the image, without this prefix? (I don't want to use substring like function).
Since the server end codes will read this string with the assumption that it only contains the base64 representation of the image.
Maybe base64Str.split(',').pop() is my best choice, as string.slice(start, stop) and string.substring(start, stop) require the exact index.
Seems that we are getting a data url(which has the leading meta data prefix) by using the approach I mentioned. The advantage is that that url can be used directly in some src field. This is the reason why we have that prefix in the front end world.

How to convert Encoded url into JSON format?

Here I want to convert my encoded url into JSON format. The encoded url is:
http://localhost:63342/AngularJs/services/e_cell.html#!/%7B%22book%22:%22ABC%22%7D
As much as I understand from your URL you are trying to post this %7B%22book%22:%22ABC%22%7D data in query string.
So first you need to decode your URL encoded data into an string which can be parsed. For that you can take help of decodeURIComponent() javascript API.
decodeURIComponent() - this function decodes an encoded URI component back to the plain text i.e. like in your encoded text it will convert %7B into opening brace {. So once we apply this API you get -
//output : Object { book: "ABC" }
This is a valid JSON string now you can simply parse. So what all you need to do is -
var formData = "%7B%22book%22:%22ABC%22%7D";
var decodedData = decodeURIComponent(formData);
var jsonObject = JSON.parse(decodedData);
console.log(jsonObject );
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string
The decodeURIComponent function will convert URL encoded characters back to plain text.
var myJSON = decodeURIComponent("%7B%22book%22:%22ABC%22%7D");
var myObject = JSON.parse(myJSON);

Convert Base64 to image file with javascript

Does anyone know of any simple javascript that I can use to turn a base64 string into an image (jpg format preferably), so that I can save the image to a file? This is for a signature pad application. I can get the signature into a base64 format, but need to save the signature as an image file to use for embedding into a Crystal Report.
I have tried this method in a JSP page to print the image from a base64 string, I guess this should hold good for javascript too. Do not have sample data for now to verify, but here's how I would try it.
var oImg=document.createElement("img");
var baseString = null; //the base64 string you have
var imsc = 'data:image/jpg;base64, '+baseString;
oImg.setAttribute('src', imsc);
document.body.appendChild(oImg);

Categories

Resources