1.10.31. fejezet, Windows COM objektumok elérése

Kapcsolódó hivatkozások

npm i node-gyp
npm i node-activex

npm i node-gyp
npm install winax

Kép beolvasása WIA ActiveX objektumon keresztül

var winax = require("winax");
 
const WIA_IMG_FORMAT_PNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}";
const WIA_SCAN_COLOR_MODE = "6146";
const WIA_HORIZONTAL_SCAN_RESOLUTION_DPI = "6147";
const WIA_VERTICAL_SCAN_RESOLUTION_DPI = "6148";
const WIA_HORIZONTAL_SCAN_START_PIXEL = "6149";
const WIA_VERTICAL_SCAN_START_PIXEL = "6150";
const WIA_HORIZONTAL_SCAN_SIZE_PIXELS = "6151";
const WIA_VERTICAL_SCAN_SIZE_PIXELS = "6152";
const WIA_SCAN_BRIGHTNESS_PERCENTS = "6154";
const WIA_SCAN_CONTRAST_PERCENTS = "6155";
 
let deviceManager = winax.Object("WIA.DeviceManager");
 
const resolution = 150;
const width_pixel = resolution * 8.5;
const height_pixel = resolution * 11;
const color_mode = 1;
 
if (deviceManager.DeviceInfos.Count > 0) {
	const device_info = deviceManager.DeviceInfos(1);
	for (let idx = 1; idx <= device_info.Properties.Count; idx++) {
		const property = device_info.Properties(idx);
		console.log(property.Name + " : " + String(property.Value));
	}
	const device = device_info.Connect();
	const item = device.Items[1];
	setWiaProperty(item, WIA_SCAN_COLOR_MODE, color_mode);
	setWiaProperty(item, WIA_HORIZONTAL_SCAN_RESOLUTION_DPI, resolution);
	setWiaProperty(item, WIA_VERTICAL_SCAN_RESOLUTION_DPI, resolution);
	setWiaProperty(item, WIA_HORIZONTAL_SCAN_START_PIXEL, 0);
	setWiaProperty(item, WIA_VERTICAL_SCAN_START_PIXEL, 0);
	setWiaProperty(item, WIA_HORIZONTAL_SCAN_SIZE_PIXELS, width_pixel);
	setWiaProperty(item, WIA_VERTICAL_SCAN_SIZE_PIXELS, height_pixel);
	setWiaProperty(item, WIA_SCAN_BRIGHTNESS_PERCENTS, 0);
	setWiaProperty(item, WIA_SCAN_CONTRAST_PERCENTS, 0);
 
	let image = item.Transfer(WIA_IMG_FORMAT_PNG);
	image.SaveFile("test.png");
}
 
function setWiaProperty(device: any, property: string, value: any) {
	let wiaProperty = device.Properties(property);
	if (wiaProperty != null) {
		wiaProperty.Value = value;
	}
}