[Style res image]

This commit is contained in:
CPunisher 2020-05-14 21:42:15 +08:00
parent 7ddd4cf191
commit 58ebae4125
3 changed files with 44 additions and 38 deletions

View File

@ -1,5 +1,5 @@
import React, {useEffect, useState} from "react"; import React, {useEffect, useMemo, useState} from "react";
import {getGrayPointList, rand} from "../../utils/util"; import {gamma} from "../../utils/util";
import {ParamTypes} from "../../constant/ParamTypes"; import {ParamTypes} from "../../constant/ParamTypes";
import {getTypeTable, QRPointType} from "../../utils/qrcodeHandler"; import {getTypeTable, QRPointType} from "../../utils/qrcodeHandler";
@ -60,15 +60,46 @@ export function getViewBox(qrcode) {
return String(-nCount / 5) + ' ' + String(-nCount / 5) + ' ' + String(nCount + nCount / 5 * 2) + ' ' + String(nCount + nCount / 5 * 2); 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(<use key={"g_" + x + "_" + y} x={x} y={y} xlinkHref={black} />);
}
}
resolve(gpl);
}
})
}
const RendererResImage = ({qrcode, params, setParamInfo}) => { const RendererResImage = ({qrcode, params, setParamInfo}) => {
useEffect(() => { useEffect(() => {
setParamInfo(getParamInfo()); setParamInfo(getParamInfo());
}, [setParamInfo]); }, [setParamInfo]);
const [gpl, setGPL] = useState([]); const [gpl, setGPL] = useState([]);
useEffect(() => { useMemo(() => {
getGrayPointList(params[0], qrcode.getModuleCount(), "#S-black", "#S-white").then(res => setGPL(res)); getGrayPointList(params[0], qrcode.getModuleCount(), "#S-black", "#S-white").then(res => setGPL(res));
}, [params[0], qrcode]) }, [setGPL, params[0], qrcode])
return ( return (
<svg className="Qr-item-svg" width="100%" height="100%" viewBox={getViewBox(qrcode)} fill="white" <svg className="Qr-item-svg" width="100%" height="100%" viewBox={getViewBox(qrcode)} fill="white"
@ -79,7 +110,6 @@ const RendererResImage = ({qrcode, params, setParamInfo}) => {
<rect id="S-black" fill="black" width={1} height={1}/> <rect id="S-black" fill="black" width={1} height={1}/>
<rect id="S-white" fill="white" width={1} height={1}/> <rect id="S-white" fill="white" width={1} height={1}/>
</defs> </defs>
{gpl.concat(listPoints(qrcode, params))} {gpl.concat(listPoints(qrcode, params))}
</svg> </svg>
) )

View File

@ -4,7 +4,7 @@ import React from "react";
import {saveImg, saveSvg} from "../../utils/downloader"; import {saveImg, saveSvg} from "../../utils/downloader";
import ReactDOMServer from "react-dom/server"; import ReactDOMServer from "react-dom/server";
import {increaseDownloadData, recordDownloadDetail} from "../../api/db"; import {increaseDownloadData, recordDownloadDetail} from "../../api/db";
import {getParamDetailedValue} from "../../utils/util"; import {getParamDetailedValue, outerHtml} from "../../utils/util";
function saveEl(state, type) { function saveEl(state, type) {
const el = React.createElement(state.rendererType, { const el = React.createElement(state.rendererType, {
@ -31,10 +31,12 @@ function saveEl(state, type) {
const mapStateToProps = (state) => ({ const mapStateToProps = (state) => ({
value: state.value, value: state.value,
onSvgDownload: () => { onSvgDownload: () => {
saveSvg(state.value, ReactDOMServer.renderToString(saveEl(state, 'svg'))) // saveSvg(state.value, ReactDOMServer.renderToString(saveEl(state, 'svg')))
saveSvg(state.value, outerHtml(state.selectedIndex))
}, },
onJpgDownload: () => { onJpgDownload: () => {
return saveImg(state.value, ReactDOMServer.renderToString(saveEl(state, 'jpg')), 1500, 1500) // return saveImg(state.value, ReactDOMServer.renderToString(saveEl(state, 'jpg')), 1500, 1500)
return saveImg(state.value, outerHtml(state.selectedIndex), 1500, 1500)
} }
}) })

View File

@ -43,38 +43,12 @@ export function getParamDetailedValue(item, paramValue) {
return paramValue; return paramValue;
} }
export function gamma(r, g, b) { export function outerHtml(selectedIndex) {
return Math.pow((Math.pow(r, 2.2) + Math.pow(1.5 * g, 2.2) + Math.pow(0.6 * b, 2.2)) / (1 + Math.pow(1.5, 2.2) + Math.pow(0.6, 2.2)), 1/2.2) return document.getElementsByClassName('Qr-item-svg')[selectedIndex].outerHTML;
} }
export function getGrayPointList(imgBase64, size, black, white) { export function gamma(r, g, b) {
let canvas = document.createElement('canvas'); return Math.pow((Math.pow(r, 2.2) + Math.pow(1.5 * g, 2.2) + Math.pow(0.6 * b, 2.2)) / (1 + Math.pow(1.5, 2.2) + Math.pow(0.6, 2.2)), 1/2.2)
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(<use key={"g_" + x + "_" + y} x={x} y={y} xlinkHref={white} />);
else gpl.push(<use key={"g_" + x + "_" + y} x={x} y={y} xlinkHref={black} />);
}
}
resolve(gpl);
}
})
} }
export function toBase64(file, width, height) { export function toBase64(file, width, height) {