diff --git a/src/components/app/PartDownload.js b/src/components/app/PartDownload.js index f3432ad..43aa76f 100644 --- a/src/components/app/PartDownload.js +++ b/src/components/app/PartDownload.js @@ -1,7 +1,7 @@ import React, {useState} from 'react'; import './App.css'; import PropTypes from 'prop-types'; -import {isWeiXin} from "../../utils/util"; +import {isWeiXin} from "../../utils/navigatorUtils"; const WxMessage = () => { if (isWeiXin()) { diff --git a/src/components/app/PartStyles.js b/src/components/app/PartStyles.js index 0afd1ed..6d963e0 100644 --- a/src/components/app/PartStyles.js +++ b/src/components/app/PartStyles.js @@ -1,7 +1,7 @@ import React, {useEffect, useState} from 'react'; import './App.css'; import StyleListViewer from "../../containers/style/StyleListViewer"; -import {isPC} from "../../utils/util"; +import {isPC} from "../../utils/navigatorUtils"; const PartStyles = ({ setParamInfo }) => { const [loaded, setLoaded] = useState(false); diff --git a/src/components/renderer/RendererBase.js b/src/components/renderer/RendererBase.js index 9b6e49e..e78812a 100644 --- a/src/components/renderer/RendererBase.js +++ b/src/components/renderer/RendererBase.js @@ -1,4 +1,4 @@ -import React, { useEffect } from "react"; +import React from "react"; import {ParamTypes} from "../../constant/ParamTypes"; import {getTypeTable, QRPointType} from "../../utils/qrcodeHandler"; import {createRenderer} from "../style/Renderer"; diff --git a/src/components/renderer/RendererRandRect.js b/src/components/renderer/RendererRandRect.js index 8326f43..c7b89b9 100644 --- a/src/components/renderer/RendererRandRect.js +++ b/src/components/renderer/RendererRandRect.js @@ -1,5 +1,5 @@ -import React, { useEffect } from "react"; -import {defaultViewBox, rand} from "../../utils/util"; +import React from "react"; +import {rand} from "../../utils/util"; import {createRenderer} from "../style/Renderer"; function listPoints(qrcode, params) { diff --git a/src/components/renderer/RendererResImage.js b/src/components/renderer/RendererResImage.js index 70e25ad..054dde1 100644 --- a/src/components/renderer/RendererResImage.js +++ b/src/components/renderer/RendererResImage.js @@ -1,5 +1,5 @@ import React, {useEffect, useMemo, useState} from "react"; -import {gamma} from "../../utils/util"; +import {gamma} from "../../utils/imageUtils"; import {ParamTypes} from "../../constant/ParamTypes"; import {getTypeTable, QRPointType} from "../../utils/qrcodeHandler"; import {defaultResImage} from "../../constant/References"; diff --git a/src/containers/app/InputText.js b/src/containers/app/InputText.js index e4d6faf..aa6c1aa 100644 --- a/src/containers/app/InputText.js +++ b/src/containers/app/InputText.js @@ -1,7 +1,7 @@ import {connect} from 'react-redux'; import {genQRInfo} from "../../actions"; import React, {useRef} from "react"; -import {isPicture} from "../../utils/util"; +import {isPicture} from "../../utils/imageUtils"; import {decodeData} from "../../utils/qrcodeHandler"; const InputText = ({dispatch}) => { diff --git a/src/containers/param/ParamUploadViewer.js b/src/containers/param/ParamUploadViewer.js index 6b8ed63..2e6e84d 100644 --- a/src/containers/param/ParamUploadViewer.js +++ b/src/containers/param/ParamUploadViewer.js @@ -1,7 +1,7 @@ import { connect } from 'react-redux'; import {changeParam} from "../../actions"; import ParamUpload from "../../components/param/ParamUpload"; -import {isPicture, toBase64} from "../../utils/util"; +import {isPicture, toBase64} from "../../utils/imageUtils"; const mapStateToProps = (state, ownProps) => ({ rendererIndex: ownProps.rendererIndex, diff --git a/src/utils/downloader.js b/src/utils/downloader.js index 7cfe63b..67a5fec 100644 --- a/src/utils/downloader.js +++ b/src/utils/downloader.js @@ -1,10 +1,6 @@ const svgHead = "\n " + "\n" -export function isChrome() { - return navigator.userAgent.toLowerCase().indexOf('chrome') > -1; -} - export function saveSvg(value, content) { let htmlContent = [svgHead + content] let bl = new Blob(htmlContent, {type: "image/svg+xml"}) diff --git a/src/utils/imageUtils.js b/src/utils/imageUtils.js new file mode 100644 index 0000000..8398346 --- /dev/null +++ b/src/utils/imageUtils.js @@ -0,0 +1,33 @@ +const fileTypes =[ + 'image/jpeg', + 'image/pjpeg', + 'image/png' +] + +export function isPicture(file) { + return fileTypes.includes(file.type); +} + +export function toBase64(file, width, height) { + let canvas = document.createElement('canvas'); + let ctx = canvas.getContext('2d'); + let img = document.createElement('img'); + + canvas.setAttribute('width', width); + canvas.setAttribute('height', height); + img.setAttribute('src', URL.createObjectURL(file)); + + return new Promise(resolve => { + img.onload = () => { + ctx.fillStyle = 'white' + ctx.fillRect(0, 0, width, height) + ctx.drawImage(img, 0, 0, width, height); + + resolve(canvas.toDataURL(file.type, 0.9)); + }; + }) +} + +export function gamma(r, g, b) { + 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) +} diff --git a/src/utils/navigatorUtils.js b/src/utils/navigatorUtils.js new file mode 100644 index 0000000..31cd97a --- /dev/null +++ b/src/utils/navigatorUtils.js @@ -0,0 +1,27 @@ +export function isWeiXin(){ + const ua = window.navigator.userAgent.toLowerCase(); + if(ua.match(/MicroMessenger/i) == 'micromessenger'){ + return true; + }else{ + return false; + } +} + +export function isPC() { + const userAgentInfo = navigator.userAgent; + const Agents = ["Android", "iPhone", + "SymbianOS", "Windows Phone", + "iPad", "iPod"]; + let flag = true; + for (let v = 0; v < Agents.length; v++) { + if (userAgentInfo.indexOf(Agents[v]) > 0) { + flag = false; + break; + } + } + return flag; +} + +export function isChrome() { + return navigator.userAgent.toLowerCase().indexOf('chrome') > -1; +} diff --git a/src/utils/util.js b/src/utils/util.js index ddd7c50..7d3a6cb 100644 --- a/src/utils/util.js +++ b/src/utils/util.js @@ -1,17 +1,6 @@ import {ParamTypes} from "../constant/ParamTypes"; let seed = 0; -const fileTypes =[ - 'image/jpeg', - 'image/pjpeg', - 'image/png' -] - -export function isPicture(file) { - return fileTypes.includes(file.type); -} - - export function srand(sd) { seed = sd; } @@ -21,47 +10,12 @@ export function rand(min, max) { return min + (seed / 233280.0) * (max - min); } -export function randRGB(minR, maxR, minG, maxG, minB, maxB) { - return 'rgb(' + parseInt(minR, maxR) + ',' + parseInt(minG, maxG) + ',' + parseInt(minB, maxB) + ')'; -} - -export function defaultViewBox(qrcode) { - if (!qrcode) return '0 0 0 0'; - - const nCount = qrcode.getModuleCount(); - return String(-nCount / 5) + ' ' + String(-nCount / 5) + ' ' + String(nCount + nCount / 5 * 2) + ' ' + String(nCount + nCount / 5 * 2); -} - export function fillEmptyWith(arr, value) { for (let i = 0; i < arr.length; i++) if (!arr[i]) arr[i] = value; return arr; } -export function isWeiXin(){ - const ua = window.navigator.userAgent.toLowerCase(); - if(ua.match(/MicroMessenger/i) == 'micromessenger'){ - return true; - }else{ - return false; - } -} - -export function isPC() { - const userAgentInfo = navigator.userAgent; - const Agents = ["Android", "iPhone", - "SymbianOS", "Windows Phone", - "iPad", "iPod"]; - let flag = true; - for (let v = 0; v < Agents.length; v++) { - if (userAgentInfo.indexOf(Agents[v]) > 0) { - flag = false; - break; - } - } - return flag; -} - export function getParamDetailedValue(item, paramValue) { if (item.type == ParamTypes.SELECTOR) return item.choices[paramValue]; return paramValue; @@ -71,10 +25,6 @@ export function outerHtml(selectedIndex) { return document.getElementsByClassName('Qr-item-svg')[selectedIndex].outerHTML; } -export function gamma(r, g, b) { - 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) -} - export function getExactValue(value, defaultValue) { if (typeof value != "string") return value; if (value.length <= 0) value = defaultValue; @@ -89,22 +39,3 @@ export function extend(target, options) { return target } -export function toBase64(file, width, height) { - let canvas = document.createElement('canvas'); - let ctx = canvas.getContext('2d'); - let img = document.createElement('img'); - - canvas.setAttribute('width', width); - canvas.setAttribute('height', height); - img.setAttribute('src', URL.createObjectURL(file)); - - return new Promise(resolve => { - img.onload = () => { - ctx.fillStyle = 'white' - ctx.fillRect(0, 0, width, height) - ctx.drawImage(img, 0, 0, width, height); - - resolve(canvas.toDataURL(file.type, 0.9)); - }; - }) -}