import { maxNum, minNum } from "@/utils/tool"; import { abc } from "@/constants/enumerate"; // recognize ---start > // 扫描数据相关 // 参考:./data.json export type RecogAreaType = "absent" | "breach" | "paperType" | "question"; export interface RecogOptionSize { x: number; y: number; w: number; h: number; filled: boolean; } export interface RecognizeArea { index: number; type: RecogAreaType; fillPosition: Array<{ x: number; y: number }>; fillSize: { w: number; h: number; }; fillArea: { x: number; y: number; w: number; h: number }; options: string[]; optionSizes: RecogOptionSize[]; result: string[]; multiple: boolean; // 当前区域的切图 sliceImg: string; } export interface RecogDataField { field: string; index: number; content: string; wrong_flag: number; fill_result: RecogDataFillResult[]; type: string; rect?: [number, number, number, number]; } export interface RecogDataFillResult { main_number: number; sub_number: number; // 所有试题的统一序号 index?: number; single: number; fill_option: number[]; suspect_flag: number; fill_position: string[]; fill_size: [number, number]; } export interface RecogDataType { algorithm: number; page_index: number; error_flag: number; blank_flag: number; angle: number; examNumber: RecogDataField; absent: RecogDataField; breach: RecogDataField; pageNumber: RecogDataField; paperType: RecogDataField; question: RecogDataField[]; block_struct: number[]; } // recognize ---end > // extends export interface RecogEditData extends RecognizeArea { areaSrc: string; [k: string]: any; } export interface ImageRecogData extends RecognizeArea { [k: string]: any; } export interface RecogBlock extends RecognizeArea { areaImg: string; fillAreaStyle: Record; fillOptionStyles: Array>; [k: string]: any; } // functions export function parseRecogData(data: string | null) { if (!data) return null; const precogData = window.atob(data); const recogData: RecogDataType | null = precogData ? JSON.parse(precogData) : null; return recogData; } export function parseDetailSize( data: RecogDataFillResult, type: RecogAreaType, index: number, fillResult?: number[] ): RecognizeArea { const result: RecognizeArea = { index, type, fillPosition: [], fillSize: { w: 0, h: 0 }, fillArea: { x: 0, y: 0, w: 0, h: 0 }, options: [], optionSizes: [], result: [], multiple: false, sliceImg: "", }; if (!data) return result; const fillResultList = fillResult && fillResult.length ? fillResult : data.fill_option; result.fillSize = { w: data.fill_size[0], h: data.fill_size[1], }; const offsetX = data.fill_size[0] / 2; const offsetY = data.fill_size[1] / 2; result.fillPosition = data.fill_position.map((item) => { const size = item.split(","); const x = size[0] ? Number(size[0]) : 0; const y = size[1] ? Number(size[1]) : 0; return { x: x - offsetX, y: y - offsetY, }; }); const xs = result.fillPosition.map((item) => item.x); const maxX = maxNum(xs); const minX = minNum(xs); result.fillArea = { x: minX, y: result.fillPosition[0].y, w: maxX - minX + result.fillSize.w, h: result.fillSize.h, }; result.optionSizes = result.fillPosition.map((item, index) => { let filled = false; if (type !== "paperType") { filled = fillResultList[index] === 1; } return { x: item.x - result.fillArea.x, y: item.y - result.fillArea.y, filled, ...result.fillSize, }; }); if (type === "question") { const options = abc.substring(0, data.fill_position.length).split(""); // 空用“#”表示 result.options = ["#", ...options]; result.result = options.filter((r, ind) => fillResultList[ind] === 1); result.multiple = true; } return result; }