import React, {useEffect, useMemo, useState} from "react"; import {gamma} from "../../utils/util"; import {ParamTypes} from "../../constant/ParamTypes"; import {getTypeTable, QRPointType} from "../../utils/qrcodeHandler"; function listPoints(qrcode, params) { if (!qrcode) return [] const nCount = qrcode.getModuleCount(); const typeTable = getTypeTable(qrcode); const pointList = new Array(nCount); let id = 0; for (let x = 0; x < nCount; x++) { for (let y = 0; y < nCount; y++) { const posX = 3 * x, posY = 3 * y; if (typeTable[x][y] == QRPointType.ALIGN_CENTER || typeTable[x][y] == QRPointType.ALIGN_OTHER || typeTable[x][y] == QRPointType.TIMING) { if (qrcode.isDark(x, y)) { pointList.push() } else { pointList.push() } } else if (typeTable[x][y] == QRPointType.POS_CENTER) { if (qrcode.isDark(x, y)) { pointList.push() } } else if (typeTable[x][y] == QRPointType.POS_OTHER) { if (qrcode.isDark(x, y)) { pointList.push() } else { pointList.push() } } else { if (qrcode.isDark(x, y)) { pointList.push() } else { pointList.push() } } } } return pointList; } function getParamInfo() { return [ { type: ParamTypes.UPLOAD_BUTTON, key: '背景图片', default: 0, } ]; } export function getViewBox(qrcode) { if (!qrcode) return '0 0 0 0'; const nCount = qrcode.getModuleCount() * 3; return String(-nCount / 5) + ' ' + String(-nCount / 5) + ' ' + String(nCount + nCount / 5 * 2) + ' ' + String(nCount + nCount / 5 * 2); } function getGrayPointList(imgBase64, size, black, white) { console.log(1) let canvas = document.createElement('canvas'); let ctx = canvas.getContext('2d'); let img = document.createElement('img'); let gpl = []; canvas.style.imageRendering = 'pixelated'; size *= 3; img.src = imgBase64; return new Promise(resolve => { img.onload = () => { canvas.width = size; canvas.height = size; ctx.imageSmoothingEnabled = false; ctx.drawImage(img, 0, 0, size, size); for (let x = 0; x < canvas.width; x++) { for (let y = 0; y < canvas.height; y++) { let imageData = ctx.getImageData(x, y, 1, 1); let data = imageData.data; let gray = gamma(data[0], data[1], data[2]); if (Math.random() > gray / 255) gpl.push(); } } resolve(gpl); } }) } const RendererResImage = ({qrcode, params, setParamInfo}) => { useEffect(() => { setParamInfo(getParamInfo()); }, [setParamInfo]); const [gpl, setGPL] = useState([]); useMemo(() => { getGrayPointList(params[0], qrcode.getModuleCount(), "#S-black", "#S-white").then(res => setGPL(res)); }, [setGPL, params[0], qrcode]) return ( {gpl.concat(listPoints(qrcode, params))} ) } export default RendererResImage