123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import { getInputDir, getOutputDir, makeDirSync } from "./env";
- const fs = require("fs");
- const path = require("path");
- /**
- * 旋转图片,并保存为正式文件
- * @param {*} scaningImageList 图片路径
- * @param {String} paperInfo 保持文件名称
- */
- export function saveOutputImage(scaningImageList, paperInfo) {
- 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) {
- 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 = files[i];
- const versoFile = files[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 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(dir, 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 };
- }
- /**
- 扫描仪返回的数据
- {
- "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":"请将试卷放置到扫描仪上,再进行扫描!"
- }
- */
|