cozmo / jsQR

A pure javascript QR code reading library. This library takes in raw images and will locate, extract and parse any QR code found within.
https://cozmo.github.io/jsQR/
Apache License 2.0
3.69k stars 607 forks source link

How to use your library with a server-side read file content? #210

Closed realtebo closed 2 years ago

realtebo commented 3 years ago

I am reading a file content using php

<?php
            $file_name = './image6513693026486043647.jpg';
            $image_file_content = base64_encode(file_get_contents( $file_name ));

            $image_size = getimagesize( $file_name );
?>

I tried to pass data to js-side using this

    const jpegData = Buffer.from("<?php echo $image_file_content ?>", 'base64');
    const rawImageData = jpeg.decode(jpegData);
    const clamped_array = Uint8ClampedArray.from(rawImageData);
    const qrContent= window.jsQR(
                clamped_array,
                <?php echo $image_size[0]; ?>,
                <?php echo $image_size[1]; ?>
            );

But the problem is that jpeg.decode give me an error about malformed data

I logged jpegData

image

I ask you kindly what transformation to to my file data [a valid jpg] to be scanned client-side from your library.

cozmo commented 2 years ago

But the problem is that jpeg.decode give me an error about malformed data

This seems like the kind of thing you'd need to take up with the author of the jpeg decode library?

jsQR just needs an array of RGBA pixel values, so using jpeg.decode or such seems right to me, but I do not know why it would throw an error without knowing that code.

realtebo commented 2 years ago

I found how to make it work

$image_file_content = base64_encode(file_get_contents( $filepath ));
$image_size = getimagesize( $filepath );

then I force js output passind data from php

const jpegData = Buffer.from("<?php echo $image_file_content ?>", 'base64');

const rawImageData = jpeg.decode(jpegData);

const clamped_array = Uint8ClampedArray.from(rawImageData.data);

const qr_code_text = window.jsQR(
                    clamped_array,
                    <?php echo $image_size[0]; ?>,
                    <?php echo $image_size[1]; ?>
);

After then I can use qr_code_text su decode an european green pass

const greenpassBody = qr_code_text.trim().substr(4);
const decodedData = base45.decode(greenpassBody);
const deflatedData = pako.inflate(decodedData);
const decodedGreenPassaData  = cbor.decodeAllSync(deflatedData);
const decodedGreenPassDataValue = decodedGreenPassaData[0].value;
[headers1, headers2, cbor_data, signature] = decodedGreenPassDataValue;
const greenpassData = cbor.decodeAllSync(cbor_data);

... and ... yes.. this requires a lot of different packages. I used browserify to make it work in a browser