qrbtf/src/components/renderer/RendererResImage.js

178 lines
76 KiB
JavaScript
Raw Normal View History

2020-05-14 13:42:15 +00:00
import React, {useEffect, useMemo, useState} from "react";
import {gamma} from "../../utils/util";
2020-05-14 12:07:09 +00:00
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);
2020-05-14 14:07:02 +00:00
let alignType = params[1];
let timingType = params[2];
2020-05-15 03:37:31 +00:00
let otherColor = params[3];
let posColor = params[4];
2020-05-14 12:07:09 +00:00
let id = 0;
for (let x = 0; x < nCount; x++) {
for (let y = 0; y < nCount; y++) {
2020-05-14 12:31:21 +00:00
const posX = 3 * x, posY = 3 * y;
2020-05-14 14:07:02 +00:00
if (typeTable[x][y] == QRPointType.ALIGN_CENTER || typeTable[x][y] == QRPointType.ALIGN_OTHER) {
2020-05-14 12:07:09 +00:00
if (qrcode.isDark(x, y)) {
2020-05-14 14:07:02 +00:00
if (alignType === 2) {
2020-05-15 03:37:31 +00:00
pointList.push(<use key={id++} xlinkHref="#B-black" x={posX - 0.03} y={posY - 0.03}/>)
2020-05-14 14:07:02 +00:00
} else {
2020-05-15 03:37:31 +00:00
pointList.push(<use key={id++} xlinkHref="#S-black" x={posX + 1 - 0.01} y={posY + 1 - 0.01}/>)
2020-05-14 14:07:02 +00:00
}
2020-05-14 12:07:09 +00:00
} else {
2020-05-14 14:07:02 +00:00
if (alignType === 0) {
pointList.push(<use key={id++} xlinkHref="#S-white" x={posX + 1} y={posY + 1}/>)
} else {
2020-05-15 03:37:31 +00:00
pointList.push(<use key={id++} xlinkHref="#B-white" x={posX - 0.03} y={posY - 0.03}/>)
2020-05-14 14:07:02 +00:00
}
}
} else if (typeTable[x][y] == QRPointType.TIMING) {
if (qrcode.isDark(x, y)) {
if (timingType === 2) {
2020-05-15 03:37:31 +00:00
pointList.push(<use key={id++} xlinkHref="#B-black" x={posX - 0.03} y={posY - 0.03}/>)
2020-05-14 14:07:02 +00:00
} else {
pointList.push(<use key={id++} xlinkHref="#S-black" x={posX + 1} y={posY + 1}/>)
}
} else {
if (timingType === 0) {
pointList.push(<use key={id++} xlinkHref="#S-white" x={posX + 1} y={posY + 1}/>)
} else {
2020-05-15 03:37:31 +00:00
pointList.push(<use key={id++} xlinkHref="#B-white" x={posX - 0.03} y={posY - 0.03}/>)
2020-05-14 14:07:02 +00:00
}
2020-05-14 12:07:09 +00:00
}
} else if (typeTable[x][y] == QRPointType.POS_CENTER) {
if (qrcode.isDark(x, y)) {
2020-05-15 03:37:31 +00:00
pointList.push(<use key={id++} fill={posColor} xlinkHref="#B" x={posX - 0.03} y={posY - 0.03}/>)
2020-05-14 12:07:09 +00:00
}
} else if (typeTable[x][y] == QRPointType.POS_OTHER) {
if (qrcode.isDark(x, y)) {
2020-05-15 03:37:31 +00:00
pointList.push(<use key={id++} fill={posColor} xlinkHref="#B" x={posX - 0.03} y={posY - 0.03}/>)
2020-05-14 12:07:09 +00:00
} else {
2020-05-15 03:37:31 +00:00
pointList.push(<use key={id++} xlinkHref="#B-white" x={posX - 0.03} y={posY - 0.03}/>)
2020-05-14 12:07:09 +00:00
}
} else {
if (qrcode.isDark(x, y)) {
2020-05-14 12:31:21 +00:00
pointList.push(<use key={id++} xlinkHref="#S-black" x={posX + 1} y={posY + 1}/>)
2020-05-14 12:07:09 +00:00
}
}
}
}
return pointList;
}
function getParamInfo() {
return [
{
type: ParamTypes.UPLOAD_BUTTON,
key: '背景图片',
2020-05-14 14:07:02 +00:00
default: data,
},
{
type: ParamTypes.SELECTOR,
key: '小定位点样式',
2020-05-14 12:07:09 +00:00
default: 0,
2020-05-14 14:07:02 +00:00
choices: [
"无",
"白",
"黑白",
]
},
{
type: ParamTypes.SELECTOR,
key: '时钟样式',
default: 0,
choices: [
"无",
"白",
"黑白",
]
},
2020-05-15 03:37:31 +00:00
{
type: ParamTypes.COLOR_EDITOR,
key: '信息点颜色',
default: '#000000'
},
{
type: ParamTypes.COLOR_EDITOR,
key: '定位点颜色',
default: '#000000'
},
2020-05-14 12:07:09 +00:00
];
}
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);
}
2020-05-14 13:42:15 +00:00
function getGrayPointList(imgBase64, size, black, white) {
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]);
2020-05-15 03:37:31 +00:00
if (Math.random() > gray / 255 && ( x % 3 !== 1 || y % 3 !== 1 ) ) gpl.push(<use key={"g_" + x + "_" + y} x={x} y={y} xlinkHref={black} />);
2020-05-14 13:42:15 +00:00
}
}
resolve(gpl);
}
})
}
2020-05-14 12:07:09 +00:00
const RendererResImage = ({qrcode, params, setParamInfo}) => {
2020-05-15 03:37:31 +00:00
let otherColor = params[3];
let posColor = params[4];
2020-05-14 12:07:09 +00:00
useEffect(() => {
setParamInfo(getParamInfo());
}, [setParamInfo]);
const [gpl, setGPL] = useState([]);
2020-05-14 13:42:15 +00:00
useMemo(() => {
2020-05-14 12:31:21 +00:00
getGrayPointList(params[0], qrcode.getModuleCount(), "#S-black", "#S-white").then(res => setGPL(res));
2020-05-14 13:42:15 +00:00
}, [setGPL, params[0], qrcode])
2020-05-14 12:07:09 +00:00
return (
<svg className="Qr-item-svg" width="100%" height="100%" viewBox={getViewBox(qrcode)} fill="white"
xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink">
<defs>
2020-05-15 03:37:31 +00:00
<rect id="B-black" fill={otherColor} width={3.08} height={3.08}/>
<rect id="B-white" fill="white" width={3.08} height={3.08}/>
<rect id="S-black" fill={otherColor} width={1.02} height={1.02}/>
<rect id="S-white" fill="white" width={1.02} height={1.02}/>
<rect id="B" width={3.08} height={3.08}/>
<rect id="S" width={1.02} height={1.02}/>
2020-05-14 12:07:09 +00:00
</defs>
{gpl.concat(listPoints(qrcode, params))}
</svg>
)
}
2020-05-14 14:07:02 +00:00
let data = "data:image/jpg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QEWRXhpZgAATU0AKgAAAAgABwEOAAIAAAAYAAAAYgESAAMAAAABAAEAAAEaAAUAAAABAAAAegEbAAUAAAABAAAAggEoAAMAAAABAAIAAAExAAIAAAAJAAAAiodpAAQAAAABAAAAlAAAAABQcm9jZXNzZWQgd2l0aCBNYXhDdXJ2ZQAAAABIAAAAAQAAAEgAAAABTWF4Q3VydmUAAAAIkAAABwAAAAQwMjIxkAQAAgAAABQAAAD6kQEABwAAAAQBAgMAoAAABwAAAAQwMTAwoAEAAwAAAAEAAQAAoAIABAAAAAEAAAISoAMABAAAAAEAAAISpAYAAwAAAAEAAAAAAAAAADIwMjA6MDU6MTQgMjA6NDY6MTMA/+0AfFBob3Rvc2hvcCAzLjAAOEJJTQQEAAAAAABDHAFaAAMbJUccAgAAAgACHAI+AAgyMDIwMDUxNBwCPwAGMjA0NjEzHAJ4ABdQcm9jZXNzZWQgd2l0aCBNYXhDdXJ2ZQA4QklNBCUAAAAAABCQrEhpTykhBw18M4lq86qT/8AAEQgCEgISAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJS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xEudQ1jxH4le5k1TWJIFiV2vWB/cxqwVUSNFCqAABg
2020-05-14 12:07:09 +00:00
export default RendererResImage