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.63k stars 600 forks source link

crop qrcode error #244

Open chengdonglin opened 10 months ago

chengdonglin commented 10 months ago

Hi, from the decode result, I want to crop the qroce from the image, next is my code:

Jimp.read(buffer)
  .then(function (blockimg) {
    var width = blockimg.bitmap.width,
      height = blockimg.bitmap.height,
      imgData = blockimg.bitmap.data;
    var code = jsqr(imgData, width, height);
    if (code) {
      console.log(code); 
      Jimp.read(buffer).then((image) => {
        const cropMeta = {
          x: code.location.topLeftCorner.x - 20,
          y: code.location.topLeftCorner.y - 20,
          width:
            code.location.topRightCorner.x - code.location.topLeftCorner.x + 10,
          height:
            code.location.bottomLeftCorner.y -
            code.location.topLeftCorner.y +
            10,
        };
        image.crop(cropMeta.x, cropMeta.y, cropMeta.width, cropMeta.height);
        image.writeAsync("./output4.jpg");
      }); 
    } else {
      console.log("decode error");
    }
  })
  .catch(function (err2) {
    if (err2) {
      console.log(err2);
      cb(null, null);
    }
  });

but I find there is not crop all the qrcode part, image like below: image

also if I change 10 to 50, it seem ok, But I have no idea that is a support method? or have any other method to find out x, y, width, height?