I have created a function. That function can read the uploaded image and get the imageData from the canves.
But I couldn’t get base64 image from that imagedata
Here is what I did
function handleFileSelect(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
const base64Data = e.target.result.split('base64,')[1];
const img = new Image();
img.src = "data:image/png;base64," + base64Data;
img.onload = function() {
const canvas = document.getElementById('imageCanvas');
const ctx = canvas.getContext('2d');
let imageData;
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
};
};
reader.readAsDataURL(file);
}
}
2
Answers
The getImageData() method of the canvas context provides the pixel data of the image, but not in base64 format. You need to use the toDataURL() method:
Does this help you?