Is possible to pass an input image from an html form to MATLAB using JavaScript?

39 Views Asked by At

I'm building a graphic interface using MATLAB with HTML, CSS, and JavaScript. I'm working on a small form where users can upload an image, and this image needs to be sent to MATLAB to be displayed using the imshow() function.

This is what I'm doing:

fig = uifigure("Position",[100 100 900 500]);
h = uihtml(fig,"Position",[0 0 900 500]);
h.HTMLSource = 'immagineInput.html';
h.HTMLEventReceivedFcn = @displayNumber;
function displayNumber(src,event)
name = event.HTMLEventName;
if strcmp(name,'ButtonClicked')
    number = event.HTMLEventData;
    b64 = matlab.net.base64decode(number);
    imshow(b64);
end
end

But the problem is that it displays an empty window, without the image. The JavaScript code is the following:

function setup(htmlComponent) {
    var input = document.getElementById("immagine");
    var submit = document.getElementById("submit");
    submit.addEventListener("click", function () {
        var file = input.files[0];
        var reader = new FileReader();
        reader.onloadend = function () {
            htmlComponent.sendEventToMATLAB("ButtonClicked", reader.result);
        };
        reader.readAsDataURL(file);
    });
}
0

There are 0 best solutions below