0

I try to use this code as a basis and want to modify the output, after scanning a qr code before putting it out in the textbox.

let's say I have a qr containing "12345", I want to scan it and just get "123" of the whole content as output in the textbox. But I can't manage where to put a variable in the js part, taking the putput, modify it and after that giving it back to the input field.

I'm a rookie in js, so this might be a pretty dumb question.

like at the last line of the js part:

var test = reader.readAsDataURL(node.files[0]);
var output = test.substr(0,3);
return output;

Here's the full code on w3schools.com again

Tom
  • 1
  • 2
  • Are you asking how to put the value of `output` inside an `input`? Because in that case, see here https://stackoverflow.com/questions/7609130/set-the-value-of-an-input-field – Federico klez Culloca Apr 04 '19 at 14:17

2 Answers2

0

You are modifying the wrong variable!

function openQRCamera(node) {
  var reader = new FileReader();
  reader.onload = function() {
    node.value = "";
    qrcode.callback = function(res) {
      if(res instanceof Error) {
        alert("No QR code found. Please make sure the QR code is within the camera's frame and try again.");
      } else {
//THIS LINE ARE UPDATING THE INPUT
//YOU CAN CHANGE THE "res" var
res = "RESULT: "+res
        node.parentNode.previousElementSibling.value = res;
      }
    };
    qrcode.decode(reader.result);
  };
  reader.readAsDataURL(node.files[0]);
}
FabioStein
  • 750
  • 7
  • 23
0

Looks like a solution to me but I can't figure out to get it running.

I added your function to the js part:

function openQRCamera(node) {
  var reader = new FileReader();
  reader.onload = function() {
    node.value = "";
    qrcode.callback = function(res) {
      if(res instanceof Error) {
        alert("No QR code found. Please make sure the QR code is within the camera's frame and try again.");
      } else {
        node.parentNode.previousElementSibling.value = res;
      }
    };
    qrcode.decode(reader.result);
  };
  reader.readAsDataURL(node.files[0]);
  alert (getTruncatedQRCode(reader, node, "3"));
}

function getTruncatedQRCode(reader, node, index) {
    if (!(reader && node && Array.isArray(node.files))) return;
    const code = reader.readAsDataURL(node.files[0]);
    if (code) return code.substr(0, index);
}

But the alert shows just undefined :(

Tom
  • 1
  • 2