[QR scan]

This commit is contained in:
CPunisher 2020-05-19 12:36:23 +08:00
parent 52b2b75f95
commit e26b286079
16 changed files with 114 additions and 52 deletions

5
package-lock.json generated
View File

@ -8091,6 +8091,11 @@
"verror": "1.10.0" "verror": "1.10.0"
} }
}, },
"jsqr": {
"version": "1.3.1",
"resolved": "https://registry.npm.taobao.org/jsqr/download/jsqr-1.3.1.tgz",
"integrity": "sha1-UVp2bliwDIAULzotxLh1EQDO7c8="
},
"jsx-ast-utils": { "jsx-ast-utils": {
"version": "2.2.3", "version": "2.2.3",
"resolved": "https://registry.npm.taobao.org/jsx-ast-utils/download/jsx-ast-utils-2.2.3.tgz", "resolved": "https://registry.npm.taobao.org/jsx-ast-utils/download/jsx-ast-utils-2.2.3.tgz",

View File

@ -6,6 +6,7 @@
"@testing-library/jest-dom": "^4.2.4", "@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2", "@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2", "@testing-library/user-event": "^7.1.2",
"jsqr": "^1.3.1",
"prop-types": "^15.7.2", "prop-types": "^15.7.2",
"react": "^16.13.1", "react": "^16.13.1",
"react-color": "^2.18.1", "react-color": "^2.18.1",

View File

@ -1,8 +1,8 @@
import React from 'react'; import React from 'react';
import './App.css'; import './App.css';
import '../Qrcode.css'; import '../Qrcode.css';
import Footer from "../footer/Footer"; import PartFooter from "./PartFooter";
import Header from "../header/Header"; import PartHeader from "./PartHeader";
import PartMore from "./PartMore"; import PartMore from "./PartMore";
import PartParams from "./PartParams"; import PartParams from "./PartParams";
import PartDownloadViewer from "../../containers/app/PartDownloadViewer"; import PartDownloadViewer from "../../containers/app/PartDownloadViewer";
@ -14,12 +14,12 @@ function App() {
<header className="App-header"> <header className="App-header">
<div className="Layout"> <div className="Layout">
<div className="Qr-outer"> <div className="Qr-outer">
<Header/> <PartHeader/>
<PartStylesViewer/> <PartStylesViewer/>
<PartParams/> <PartParams/>
<PartDownloadViewer/> <PartDownloadViewer/>
<PartMore/> <PartMore/>
<Footer/> <PartFooter/>
</div> </div>
</div> </div>
</header> </header>

View File

@ -3,7 +3,7 @@ import '../Qrcode.css';
const currentYear = new Date().getFullYear(); const currentYear = new Date().getFullYear();
const Footer = () => ( const PartFooter = () => (
<div className="Qr-titled"> <div className="Qr-titled">
<div className="Qr-Centered Qr-footer note-font"> <div className="Qr-Centered Qr-footer note-font">
<div> <div>
@ -33,4 +33,4 @@ const Footer = () => (
</div> </div>
) )
export default Footer export default PartFooter

View File

@ -9,7 +9,7 @@ const logoStyle = {
backgroundPosition: 'left' backgroundPosition: 'left'
}; };
const Header = () => ( const PartHeader = () => (
<div className="Qr-Centered"> <div className="Qr-Centered">
<div style={logoStyle}> <div style={logoStyle}>
<h1 className="Qr-title">&ensp;</h1> <h1 className="Qr-title">&ensp;</h1>
@ -19,4 +19,4 @@ const Header = () => (
</div> </div>
) )
export default Header export default PartHeader

View File

@ -1,7 +1,5 @@
import React, { useEffect } from "react"; import React, { useEffect } from "react";
import {defaultViewBox, rand} from "../../utils/util"; import {defaultViewBox, rand} from "../../utils/util";
import {ParamTypes} from "../../constant/ParamTypes";
import {getTypeTable, QRPointType} from "../../utils/qrcodeHandler";
function listPoints(qrcode, params) { function listPoints(qrcode, params) {
if (!qrcode) return [] if (!qrcode) return []

View File

@ -167,7 +167,7 @@ const RendererResImage = ({qrcode, params, setParamInfo}) => {
const [gpl, setGPL] = useState([]); const [gpl, setGPL] = useState([]);
useMemo(() => { useMemo(() => {
getGrayPointList(params, qrcode.getModuleCount(), "#S-black", "#S-white").then(res => setGPL(res)); getGrayPointList(params, qrcode.getModuleCount(), "#S-black", "#S-white").then(res => setGPL(res));
}, [setGPL, params[0], params[1], params[2], qrcode]) }, [setGPL, params, 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"

View File

@ -3,5 +3,5 @@ export const actionTypes = {
CHANGE_STYLE: 'CHANGE_STYLE', CHANGE_STYLE: 'CHANGE_STYLE',
CHANGE_CORRECT_LEVEL: 'CHANGE_CORRECT_LEVEL', CHANGE_CORRECT_LEVEL: 'CHANGE_CORRECT_LEVEL',
CREATE_PARAM: 'CREATE_PARAM', CREATE_PARAM: 'CREATE_PARAM',
CHANGE_PARAM: 'CHANGE_PARAM' CHANGE_PARAM: 'CHANGE_PARAM',
} }

View File

@ -1,19 +1,55 @@
import {connect} from 'react-redux'; import {connect} from 'react-redux';
import {genQRInfo} from "../../actions"; import {genQRInfo} from "../../actions";
import React from "react"; import React, {useRef} from "react";
import {isPicture} from "../../utils/util";
import {decodeData} from "../../utils/qrcodeHandler";
const InputText = ({dispatch}) => ( const InputText = ({dispatch}) => {
<input const textRef = useRef();
className="Qr-input big-input"
placeholder="Input your URL here" return (
onBlur={e => dispatch(genQRInfo(e.target.value))} <React.Fragment>
onKeyPress={(e) => { <input
if (e.key === 'Enter') { className="Qr-input big-input"
dispatch(genQRInfo(e.target.value)); placeholder="Input your URL here"
e.target.blur(); ref={textRef}
} onBlur={e => dispatch(genQRInfo(e.target.value))}
}} onKeyPress={(e) => {
/> if (e.key === 'Enter') {
) dispatch(genQRInfo(e.target.value));
e.target.blur();
}
}}
/>
<label
htmlFor="image_scanner"
className="dl-btn"
style={{textAlign: "center"}}
>
扫描图片
</label>
<input
type="file"
id="image_scanner"
hidden={true}
accept=".jpg, .jpeg, .png"
onClick={(e) => e.target.value = null}
onChange={(e) => {
if (e.target.files.length > 0) {
const file = e.target.files[0];
if (isPicture(file)) {
decodeData(file).then((res) => {
if (res) {
textRef.current.value = res.data;
console.log(res.data)
dispatch(genQRInfo(res.data))
}
}).catch(console.err);
}
}
}}
/>
</React.Fragment>);
}
export default connect()(InputText); export default connect()(InputText);

View File

@ -1,6 +1,5 @@
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import PartDownload from "../../components/app/PartDownload"; import PartDownload from "../../components/app/PartDownload";
import React from "react";
import {saveImg, saveSvg} from "../../utils/downloader"; import {saveImg, saveSvg} from "../../utils/downloader";
import {increaseDownloadData, recordDownloadDetail} from "../../api/db"; import {increaseDownloadData, recordDownloadDetail} from "../../api/db";
import {getParamDetailedValue, outerHtml} from "../../utils/util"; import {getParamDetailedValue, outerHtml} from "../../utils/util";

View File

@ -1,17 +1,7 @@
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import {changeParam} from "../../actions"; import {changeParam} from "../../actions";
import ParamUpload from "../../components/param/ParamUpload"; import ParamUpload from "../../components/param/ParamUpload";
import {toBase64} from "../../utils/util"; import {isPicture, toBase64} from "../../utils/util";
const fileTypes =[
'image/jpeg',
'image/pjpeg',
'image/png'
]
function validFileType(file) {
return fileTypes.includes(file.type);
}
const mapStateToProps = (state, ownProps) => ({ const mapStateToProps = (state, ownProps) => ({
rendererIndex: ownProps.rendererIndex, rendererIndex: ownProps.rendererIndex,
@ -24,7 +14,7 @@ const mapDispatchToProps = (dispatch, ownProps) => ({
onChange: (e) => { onChange: (e) => {
if (e.target.files.length > 0) { if (e.target.files.length > 0) {
const file = e.target.files[0]; const file = e.target.files[0];
if (validFileType(file)) { if (isPicture(file)) {
toBase64(file, 500, 500).then(res => { toBase64(file, 500, 500).then(res => {
dispatch(changeParam(ownProps.rendererIndex, ownProps.paramIndex, res)) dispatch(changeParam(ownProps.rendererIndex, ownProps.paramIndex, res))
}) })

View File

@ -1,8 +1,7 @@
import {connect, useDispatch} from 'react-redux'; import {connect} from 'react-redux';
import {changeStyle, createParam} from "../../actions"; import {changeStyle} from "../../actions";
import StyleList from "../../components/style/StyleList"; import StyleList from "../../components/style/StyleList";
import RendererViewer from "./RendererViewer"; import RendererViewer from "./RendererViewer";
import RendererBlank from "../../components/renderer/RendererBlank";
import RendererBase from "../../components/renderer/RendererBase"; import RendererBase from "../../components/renderer/RendererBase";
import RendererDSJ from "../../components/renderer/RendererDSJ"; import RendererDSJ from "../../components/renderer/RendererDSJ";
import RendererRound from "../../components/renderer/RendererRound"; import RendererRound from "../../components/renderer/RendererRound";

View File

@ -1,4 +1,4 @@
import {getQrcodeData} from "../utils/qrcodeHandler"; import {encodeData} from "../utils/qrcodeHandler";
import {actionTypes} from "../constant/ActionTypes"; import {actionTypes} from "../constant/ActionTypes";
import {QRBTF_URL} from "../constant/References"; import {QRBTF_URL} from "../constant/References";
import RendererBase from "../components/renderer/RendererBase"; import RendererBase from "../components/renderer/RendererBase";
@ -11,7 +11,7 @@ const initialState = {
correctLevel: 0, correctLevel: 0,
textUrl: QRBTF_URL, textUrl: QRBTF_URL,
history: [], history: [],
qrcode: getQrcodeData({text: QRBTF_URL, correctLevel: 0}), qrcode: encodeData({text: QRBTF_URL, correctLevel: 0}),
paramInfo: new Array(16).fill(new Array(16)), paramInfo: new Array(16).fill(new Array(16)),
paramValue: new Array(16).fill(new Array(16)) paramValue: new Array(16).fill(new Array(16))
} }
@ -23,7 +23,7 @@ export default function appReducer(state = initialState, action) {
if (!text || text.length == 0) text = QRBTF_URL; if (!text || text.length == 0) text = QRBTF_URL;
return Object.assign({}, state, { return Object.assign({}, state, {
textUrl: text, textUrl: text,
qrcode: getQrcodeData({text: text, correctLevel: state.correctLevel}) qrcode: encodeData({text: text, correctLevel: state.correctLevel})
}); });
} }
case actionTypes.CHANGE_STYLE: { case actionTypes.CHANGE_STYLE: {
@ -37,7 +37,7 @@ export default function appReducer(state = initialState, action) {
case actionTypes.CHANGE_CORRECT_LEVEL: { case actionTypes.CHANGE_CORRECT_LEVEL: {
return Object.assign({}, state, { return Object.assign({}, state, {
correctLevel: parseInt(action.correctLevel), correctLevel: parseInt(action.correctLevel),
qrcode: getQrcodeData({text: state.textUrl, correctLevel: parseInt(action.correctLevel)}) qrcode: encodeData({text: state.textUrl, correctLevel: parseInt(action.correctLevel)})
}) })
} }
case actionTypes.CREATE_PARAM: { case actionTypes.CREATE_PARAM: {

View File

@ -13,6 +13,7 @@
// http://www.denso-wave.com/qrcode/faqpatent-e.html // http://www.denso-wave.com/qrcode/faqpatent-e.html
// //
//--------------------------------------------------------------------- //---------------------------------------------------------------------
/* eslint-disable */
function QR8bitByte(data) { function QR8bitByte(data) {
this.mode = QRMode.MODE_8BIT_BYTE; this.mode = QRMode.MODE_8BIT_BYTE;
this.data = data; this.data = data;
@ -1270,4 +1271,4 @@ QRBitBuffer.prototype = {
} }
}; };
export default QRCode exports.qrcode = QRCode;

View File

@ -1,4 +1,5 @@
import QRCode from "./qrcode"; import { qrcode as QRCodeEncoder } from "./qrcodeEncoder";
import jsQR from "jsqr";
function extend(target, options) { function extend(target, options) {
for (let name in options) { for (let name in options) {
@ -18,7 +19,7 @@ export var QRPointType = {
VERSION: 7 VERSION: 7
} }
export function getQrcodeData(options) { export function encodeData(options) {
if (!options.text || options.text.length <= 0) return null if (!options.text || options.text.length <= 0) return null
options = extend({ options = extend({
@ -31,7 +32,7 @@ export function getQrcodeData(options) {
foreground : "#000000" foreground : "#000000"
}, options); }, options);
let qrcode = new QRCode(options.typeNumber, options.correctLevel) let qrcode = new QRCodeEncoder(options.typeNumber, options.correctLevel)
qrcode.addData(options.text) qrcode.addData(options.text)
qrcode.make() qrcode.make()
@ -90,3 +91,26 @@ export function getTypeTable(qrcode) {
} }
return typeTable; return typeTable;
} }
export function decodeData(file) {
let canvas = document.createElement('canvas');
let ctx = canvas.getContext('2d');
let img = document.createElement('img');
const maxSize = 400;
img.setAttribute('src', URL.createObjectURL(file));
return new Promise((resolve) => {
img.onload = () => {
let rate = Math.min(img.width, img.height) / maxSize;
canvas.width = img.width / rate;
canvas.height = img.height / rate;
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
let result = jsQR(
ctx.getImageData(0, 0, canvas.width, canvas.height).data, canvas.width, canvas.height);
resolve(result);
};
})
}

View File

@ -1,8 +1,17 @@
import {ParamTypes} from "../constant/ParamTypes"; import {ParamTypes} from "../constant/ParamTypes";
import React from "react";
let seed = 0; let seed = 0;
const fileTypes =[
'image/jpeg',
'image/pjpeg',
'image/png'
]
export function isPicture(file) {
return fileTypes.includes(file.type);
}
export function srand(sd) { export function srand(sd) {
seed = sd; seed = sd;
} }