一、Display 的功能
- 在日常应用开发中,需要开发者获取显示器的相关信息以此来进行后续相关业务的开发,比如投屏等。基于这种需求,EdgerOS 提供了 Display 模块,方便开发者可以在应用开发中便捷的获取显示器的相关信息。
- 需要注意的是这个模块只有在 EdgerOS 1.8.0 及以上版本才会提供,并且需要给予显示权限才能使用。
① 引入模块:
const Display = require('display');
② 实例化:
- new Display(channel)
-
- channel {Integer} 显示设备信息通道标识数字
-
- Returns: {Object} Display 对象
const display = new Display(0);
③ 显示列表
- Display.list()
-
- Returns: {Array} 合法的显示设备信息通道标识数字集合
console.log(Display.list());
④ 设备信息
- display.info():Returns: {Object} 显示设备信息
- 获取显示设备的相关信息,可以包含以下内容;
-
- -high {Integer} 垂直像素宽度
- -color {Integer} 色彩深度位
- -linked {Boolean} 显示器是否已连接
- -busy {Boolean} 是否占用
const info = display.info();
if (info.linked) {
console.log(info.width, 'X', info.high);
}
二、示例
- 假设我们有投屏类似的需求,就可以借助 Display 模块获取显示设备的相关信息,本示例就是实现将广告图片投到显示设备上。在此示例中,首先使用了 Display 模块获取到了显示设备的分辨率信息, 并依赖上文提及的 ImageCodec 模块将图片进行重新编码,将广告图片的分辨率大小和显示设备的分辨率修改到一致,最后使用了 MediaDecoder 模块的 previewFormat 方法,将修改后的图片投到显示设备上。
- 示例代码如下:
const MediaDecoder = require('mediadecoder');
const Display = require('display');
const imagecodec = require('imagecodec');
const iosched = require('iosched');
const display = new Display(0);
const info = display.info();
const wid = info.width;
const hig = info.high;
display.close()
const imageFile = imagecodec.decode('./test.bmp');
let newImage = imagecodec.resize(imageFile , {
width: wid,
height: hig,
components: imageFile.components
});
imagecodec.encode(newImage, './temp.bmp', {
quality: 80 });
let image = new MediaDecoder().open('file://./temp.bmp');
if (image == undefined) {
console.error('Can not open image file!');
return;}
image.destVideoFormat({
width: 240, height: 240, fps: 1, pixelFormat: MediaDecoder.PIX_FMT_RGB24, noDrop: false, disable: false});
image.destAudioFormat({
disable: true});
image.previewFormat({
enable: true, fb: 0, fps: 25});
let quited = false;
image.on('video', (video) => {
console.log('get video frame');
});
image.on('eof', () => {
quited = true;
});
image.start();
while (!quited) {
iosched.poll();
}
image.close();
- 在不同的场景下我们可以利用 Display 模块进行不同的业务开发,投屏仅是其中一部分的用途,只要需求有关于获取显示设备的信息,都可以考虑使用 Display 模块。