UNPKG

scanner-to-buffer

Version:
86 lines (85 loc) 4.25 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.list = exports.scan = void 0; const utils_1 = require("./utils"); const powershell = (_a) => { var { script } = _a, o = __rest(_a, ["script"]); return (0, utils_1.spawn)(Object.assign(Object.assign({}, o), { args: ["powershell.exe", "-NoProfile", "-Command", `& {${script}}`] })); }; const formats = (0, utils_1.satisfies)({ bmp: "AB", png: "AF", gif: "B0", jpeg: "AE", tiff: "B1", }); const fid = (x) => `{B96B3C${x}-0728-11D3-9D7B-0000F81EF32E}`; const scan = (_a) => __awaiter(void 0, void 0, void 0, function* () { var _b, _c; var { timeout, logger } = _a, o = __rest(_a, ["timeout", "logger"]); const formatID = formats[(_b = o.format) !== null && _b !== void 0 ? _b : "png"]; if (formatID === undefined) throw utils_1.Errors.windowsNoFormat(o.format); return powershell({ script: (0, utils_1.trimN)(6) ` $ErrorActionPreference = "Stop" $deviceManager = new-object -ComObject WIA.DeviceManager ${o.device ? `$device = $deviceManager.DeviceInfos | where DeviceID -eq "${o.device}"` : "$device = $deviceManager.DeviceInfos.Item(1)"} $deviceConnected = $device.Connect() foreach ($item in $deviceConnected.Items) { foreach($prop in $item.Properties){ if(($prop.PropertyID -eq 6147) -or ($prop.PropertyID -eq 6148)){ $prop.Value = "${(_c = o.dpi) !== null && _c !== void 0 ? _c : 150}" } } } $imageProcess = new-object -ComObject WIA.ImageProcess foreach ($item in $deviceConnected.Items) { $image = $item.Transfer() } $imageProcess.Filters.Add($imageProcess.FilterInfos.Item("Convert").FilterID) $imageProcess.Filters.Item(1).Properties.Item("FormatID").Value = "${fid(formatID)}" $imageProcess.Filters.Item(1).Properties.Item("Quality").Value = 5 $image = $imageProcess.Apply($image) $bytes = $image.FileData.BinaryData [System.Console]::OpenStandardOutput().Write($bytes, 0, $bytes.Length)`, timeout, logger, onError: ifHas => { var _a, _b; return (_b = (_a = ifHas(utils_1.Errors.invalidDPI, "At line:8 char:76", "The parameter is incorrect.")) !== null && _a !== void 0 ? _a : ifHas(utils_1.Errors.connect, "At line:5", "An unspecified error occurred during an attempted communication with the WIA device.")) !== null && _b !== void 0 ? _b : ifHas(utils_1.Errors.busy, "At line:5", "The WIA device is busy."); }, }); }); exports.scan = scan; const list = (o) => __awaiter(void 0, void 0, void 0, function* () { const rawDevices = yield powershell(Object.assign(Object.assign({}, o), { script: (0, utils_1.trimN)(6) ` $ErrorActionPreference = "Stop" $deviceManager = new-object -ComObject WIA.DeviceManager $deviceManager.DeviceInfos | ForEach-Object -Process {$_.DeviceId}` })); return rawDevices .toString() .split("\r\n") .filter(x => x); }); exports.list = list;