123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- import {
- getInputDir,
- getOutputDir,
- makeDirSync,
- getImgDecodeTool,
- getTmpDir,
- getExtraDir
- } from "./env";
- import { randomCode, formatDate } from "./utils";
- const fs = window.nodeRequire("fs");
- const path = window.nodeRequire("path");
- const childProcess = window.nodeRequire("child_process");
- const gm = window.nodeRequire("gm").subClass({
- imageMagick: true,
- appPath: getExtraDir("imagemagick/")
- });
- /**
- * 旋转图片,并保存为正式文件
- * @param {*} scaningImageList 图片路径
- * @param {String} paperInfo 保持文件名称
- */
- export function saveOutputImage(scaningImageList, paperInfo) {
- console.log(scaningImageList);
- let outputOriginPaths = [];
- for (let i = 0; i < scaningImageList.length; i++) {
- const imagePath = scaningImageList[i];
- const originImageFile = saveOriginImage(imagePath, paperInfo);
- outputOriginPaths.push(originImageFile);
- fs.unlinkSync(imagePath);
- }
- return outputOriginPaths;
- }
- function saveOriginImage(imagePath, paperInfo) {
- console.log(imagePath);
- const outputDir = path.join(getOutputDir("origin"), paperInfo.taskId + "");
- if (!fs.existsSync(outputDir)) makeDirSync(outputDir);
- const outputOriginPath = path.join(outputDir, path.basename(imagePath));
- fs.copyFileSync(imagePath, outputOriginPath);
- return outputOriginPath;
- }
- /**
- * 获取最早添加的文件
- * @param {String} dir 图片目录
- */
- export function getEarliestFile(dir) {
- const ddir = dir || getInputDir();
- const files = fs
- .readdirSync(ddir)
- .filter(fileName => fileName.toLowerCase().match(/\.(jpg|png|jpeg)/))
- .map(fileName => {
- return {
- name: fileName,
- time: fs.statSync(path.join(ddir, fileName)).birthtimeMs
- };
- })
- .sort((a, b) => a.time - b.time);
- if (!files.length) return { url: "", name: "" };
- return {
- url: path.join(ddir, files[0].name),
- name: files[0].name
- };
- }
- export function getPreUploadFilesAutoSerial(dir) {
- const ddir = dir || getInputDir();
- const files = fs
- .readdirSync(ddir)
- .filter(fileName => fileName.toLowerCase().match(/\.(jpg|png|jpeg)/));
- let imageList = [];
- const len = Math.ceil(files.length / 2);
- for (let i = 0; i < len; i++) {
- const frontFile = path.join(dir, files[2 * i]);
- const versoFile = path.join(dir, files[2 * i + 1]);
- imageList.push({
- frontFile,
- versoFile
- });
- }
- return imageList;
- }
- export function getPreUploadFileCount(dir) {
- const ddir = dir || getInputDir();
- const files = fs
- .readdirSync(ddir)
- .filter(fileName => fileName.toLowerCase().match(/\.(jpg|png|jpeg)/));
- return Math.ceil(files.length / 2);
- }
- export function clearDir(dir) {
- fs.readdirSync(dir).forEach(file => {
- fs.unlinkSync(path.join(dir, file));
- });
- }
- export function getPreUploadFiles(dir) {
- const ddir = dir || getInputDir();
- const files = fs
- .readdirSync(ddir)
- .filter(fileName => fileName.toLowerCase().match(/\.(json)/));
- let imageList = [];
- if (!files.length) return { succeed: false, errorMsg: "当前无扫描文件!" };
- const fileCont = fs.readFileSync(path.join(ddir, files[0]));
- const fileInfo = JSON.parse(fileCont);
- if (!fileInfo.succeed) {
- return { succeed: false, errorMsg: fileInfo.errorMsg };
- }
- imageList = fileInfo.images.map(item => {
- return {
- frontFile: item.front,
- versoFile: item.back
- };
- });
- if (!imageList.length)
- return { succeed: false, errorMsg: "当前无扫描文件!" };
- return { succeed: true, data: imageList };
- }
- export function renamePreUploadJsonFile(dir) {
- const ddir = dir || getInputDir();
- const files = fs
- .readdirSync(ddir)
- .filter(fileName => fileName.toLowerCase().match(/\.(json)/));
- if (!files.length) return;
- files.forEach(file => {
- fs.renameSync(
- path.join(ddir, file),
- path.join(ddir, path.basename(file, ".json") + `_${randomCode(8)}.txt`)
- );
- });
- }
- /**
- 扫描仪返回的数据
- {
- "images": [
- {
- "duplex": true,
- "front": "D:/workspace/project/scan-library/test/00000001_F.jpg",
- "back":"D:/workspace/project/scan-library/test/00000001_B.jpg"
- },
- {
- "duplex": true,
- "front": "D:/workspace/project/scan-library/test/00000001_B.jpg",
- "back":"D:/workspace/project/scan-library/test/00000001_B.jpg"
- }
- ],
- "succeed": true,
- "errorMsg":"请将试卷放置到扫描仪上,再进行扫描!"
- }
- */
- /**
- * 解析图片code信息
- * @param {String} imgPath 图片路径
- * @param {Object} codeArea code所在文件区域信息
- */
- export function decodeImageCode(imgPath, codeArea) {
- const tmpFile = path.join(
- getTmpDir(),
- `${formatDate("YYYYMMDDHHmmss")}_${randomCode(8)}.jpg`
- );
- const imgObj = gm(imgPath);
- // 裁剪条形码区域
- imgObj.crop(codeArea.width, codeArea.height, codeArea.x, codeArea.y);
- // 旋转
- const rotate = parseInt(codeArea.rotate);
- if (rotate) imgObj.rotate("#FFFFFF", rotate);
- return new Promise((resolve, reject) => {
- // 写入临时文件
- imgObj.write(tmpFile, function(err) {
- if (err) {
- reject("条形码临时文件获取失败");
- return;
- }
- // 获取条形码解析工具
- const exec = getImgDecodeTool();
- // 解析条形码
- let code;
- try {
- const DecodeResult = childProcess.execSync(`${exec} ${tmpFile}`);
- const codes = DecodeResult.toString()
- .replace(/\r/, "")
- .replace(/\n/, "")
- .split(":");
- // console.dir(codes);
- if (codes.length <= 2) {
- code = codes[codes.length - 1].replace(/\s+/g, "");
- }
- } catch (e) {
- console.log(e);
- reject("解析条形码错误");
- return;
- }
- // 暂时不删除临时文件
- fs.unlink(tmpFile, () => {});
- resolve(code);
- });
- });
- }
|