简体   繁体   English

某些二维码无法从 react-qr-reader 中读取

[英]Certain QR codes are not read from react-qr-reader

I've tried to implement QR Validation.我试图实施 QR 验证。 So I generated QR codes for 40 people out of which 2 didn't get scanned.所以我为 40 个人生成了二维码,其中 2 个人没有被扫描。 When I tried to scan via Other apps it read the data from the QR Code.当我尝试通过其他应用程序进行扫描时,它会从二维码中读取数据。 I'm not able to figure out if the problem is with QR Code or the Scanner.我无法确定问题出在二维码还是扫描仪上。 When I generated the QR code from an online Website called QR-monkey, the scanner was able to read the data.当我从一个名为 QR-monkey 的在线网站生成二维码时,扫描仪能够读取数据。 So please help me figure out what and where exactly the problem is.所以请帮我弄清楚问题出在哪里。 I resized the QR resolution also to 2000px when I assumed its because of clarity issues.由于清晰度问题,当我假设它时,我也将 QR 分辨率调整为 2000px。

Scanner:扫描器:

const Scanner = (props) => {
  const [startScan, setStartScan] = useState(false);
  const [loadingScan, setLoadingScan] = useState(false);
  const [data, setData] = useState("");

  
  const handleScan = async(scanData) => {
    console.log(`loaded data data`, scanData);
    if (scanData && scanData !== "") {
      setLoadingScan(true);
      console.log(`loaded >>>`, scanData);
      const resData= JSON.parse(scanData)
      console.log(resData);
      setStartScan(false);
      setData(resData);
  setLoadingScan(false);
      
  };
}


  const View = (e) => {
    navigate('/View')
  }

  const handleError = (err) => {
    console.error(err);
    navigate('/Scan')
  };
  
  const handleVerify = async(e) =>{
    setisverifying(true)

    const response = await axios({
      method:'post',
      url: 'verifyurl',
      data: data
  }).then(res => {
    console.log(res);
    if (res.status == 201) {
     
      navigate('/Success', {
        state:{
          name:res.data.resName,
          taken:res.data.resTaken,
          preference:res.data.resPreference,
          team:res.data.resTeam,
        }
      })
    }
  }).catch(err => {
    console.log(err);
  })
  setisverifying(false)
  }
  
  return (
    <div className="login-box">
    <h2>
      Scanner
    </h2>
<>
    <button className='scanButton'
      onClick={() => {
        setStartScan(!startScan);
        setData('')
      }}
    >
      {startScan? "Stop Scan": "Start Scan"}
    </button>
    {startScan && (
      <>
        <QrReader
          delay={100}
          onError={handleError}
          onScan={handleScan}
          // chooseDeviceId={()=>selected}
          style={{ width: "100%", height: "100%" }}
        />
      </>
    )}
    {loadingScan ? 
    <CircularProgress />
    : ''}

    {data == ""? '':''
        }
    {data !== "" && <table className='tTable'>
        <tbody className='Ttbody' >
          <tr className='Ttr'><p>Name</p>
          <td className='Ttd' >{data.state.name}</td>
          </tr>
          
          <tr className='Ttr'><p>Team</p>
          <td className='Ttd'>{data.team}</td>
          </tr>
          
        </tbody>
      </table>}
    {isverifying?
    <CircularProgress /> : '' }
    {data !=="" && <button className='scanButton' type="" onClick={handleVerify} >Verify</button>}
    
    <button className='scanButton' onClick={View}>View</button>
  </div>
  );
};

export default Scanner;

Generator.js:生成器.js:

function QrCodeGenerator() {
    const [state, setState] = useState({
        name:'',
        phone:'',
        email:''
    });
    const [team, setTeam] = useState('');
  const teams = [
    {value:'Greeters', label:'Greeters'},
    {value:'Cleaning', label:'Cleaning'},
  ]
  

    const GenerateQRCode = (e) => {
    e.preventDefault()

    QRCode.toDataURL(value, {
            margin: 2,
            color: {
                dark: '#000000',
                light: '#ffffff'
            },
      width: 2000,
      height:2000
        }, (err, value) => {
            if (err) return console.error(err)    
            console.log(value)
            setQr(value)
      console.log(data)
        })
    }


    function handleChange(event) {
        const res = event.target.value;
        setState({
            ...state,
            [event.target.name]: res
        })
    }

    const handleSelect = (e) => {
      setTeam(e.value)
      console.log(data);
    }
    const data = {
      state, team
    }

    
    const value = JSON.stringify(data)
    const handleSubmit = async(e) =>{
      e.preventDefault()
      if (team=='') {
        alert("Team is Required")
      }
      else{
      setLoading(true)
      const response = await axios({
        method:'post',
        url: "submitURL",
        data: data
      })
      .then(res => {
        console.log(res);
        if(res.status==201){
          setSuccess(true)
          setLoading(false)
        }
        // Create the email payload with the QR code image as an attachment
        
      })
      .catch(err => {
        console.log(err);
        if (err.response.status==406) {
          setErr(true)
          seterrMsg("The volunteer already exists!")
        }
        else{
          alert('There is an internal Server error. Kindly report to the IT team')
        }
      })
      setState({
        name:'',
        phone:'',
        email:''
      })
      setTeam('')
      setLoading(false)

    }
  }

}
const handleSuccess = (e) =>{
  setSuccess(false)
}
      // download QR code
    
  return (
    <div className='login-box'>
        <form onSubmit={GenerateQRCode}>
          <input name='name' id='name' placeholder='Full Name' pattern="^(\w+)\s(\w+)$" value={state.name} onChange={handleChange} required />
            <input name='phone' id='phone' placeholder='Phone' type='tel' pattern="(6|7|8|9)\d{9}$" value={state.phone} onChange={handleChange} required='true'/>
            <input name='email' id='mail' placeholder='Email' type='email'  value={state.email} onChange={handleChange} required />
              <br/>
            <Select
              className='SelectTeam'
              closeMenuOnSelect={true}
              components={animatedComponents}
              isMulti={false}
              options={teams}
              name='team'
              onChange={handleSelect}
            />
                <button type='submit' >Generate</button>
        </form>  
        {qr && <>
                <img src={qr} alt='' className='qrimg' id='can'/>
        <a href={qr} download={`${state.name}.png`} ><button value="Download" >Download</button></a>
            </>}
      {loading ?  <CircularProgress /> :
      <button onClick={handleSubmit}>Submit</button>}
      <br/>       
    </div>

  )
}

export default QrCodeGenerator

Thank you for helping out.谢谢你帮忙。 I've tried to reduce the code to the most significant parts, but still ended up retaining them assuming it might give some input to help me out.我试图将代码减少到最重要的部分,但最终还是保留了它们,假设它可能会提供一些输入来帮助我。 Final question is, what kind of scanners do apps that were actually able to read the QR codes use?最后一个问题是,真正能够读取二维码的应用程序使用什么样的扫描仪? and Why the one generated online was readable by my scanner and not the one I generated?为什么我的扫描仪可以读取在线生成的,而不是我生成的?

It seems that the react-qr-reader has bugs that were not resolved.似乎react-qr-reader有未解决的错误。 So I just changed the module to modern-react-qr-reader which is a new and better package that will receive many updates.所以我只是将模块更改为modern-react-qr-reader ,这是一个新的更好的 package,它将收到许多更新。 That fixed my problem.那解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM