In the previous version, there was a demand on the street. Use the URL to generate a QR code, and then combine it with another picture to create a picture for you.
The implementation idea is like thisAfter generating the image, I found that the image is very blurry. The solution is to expand the canvas twice and exaggerate other parameters by twice.
jr-qrcode You can use npm install --save jr-qrcode to install this package
The function is to convert text to data:base64 for use by img's src
The code is as follows
import React, { Component } from 'react'const qrcode = require('jr-qrcode')const loadImg = (src) => { const paths = Array.isArray(src) ? src : [src]; const promise = [ ]; paths.forEach((path) => { promise.push(new Promise((resolve, reject) => { let img = new Image(); img.crossOrigin = Anonymous; img.src = path; img.onload = () => { resolve(img); }; img.onerror = (err) => { console.log('Image loading failed ') } })) }); return Promise.all(promise);}const getImageData = (url, src) => { const imgsrc = qrcode.getQrBase64(url) let canvas = document.createElement('canvas') const width = document.documentElement.clientWidth const height = document.documentElement.clientHeight canvas.width = width*2 canvas.height = height*2 let ctx = canvas.getContext(2d) loadImg([imgsrc, src]).then(([img1, img2]) => { ctx.drawImage(img2, 0, 0, width*2, height*2) ctx.drawImage(img1, width-80, height*2-220, 200, 160) ctx.fillStyle = 'rgba (0,0,0,0.3)'; ctx.fillRect(width-80, height*2-60, 200, 40); ctx.save() ctx.font=28px Arial ctx.fillStyle = '#fff'; ctx.fillText('Long press to identify QR code', width-80, height*2-30, 200, 160) let imageURL = canvas.toDataURL(image/png) document.getElementById('mix_img').setAttribute('src',imageURL) })}export default class QRcode extends Component { render() { const { url , picSrc} = this.props getImageData(url,picSrc) return ( < div> <img </div> ) }}The above is the entire content of this article. I hope it will be helpful to everyone’s study. I also hope everyone will support VeVb Wulin Network.