# desktopCapturer
`desktopCapturer` 模塊可用來獲取可用資源,這個資源可通過 `getUserMedia` 捕獲得到.
```javascript
// 在渲染進程中.
var desktopCapturer = require('electron').desktopCapturer;
desktopCapturer.getSources({types: ['window', 'screen']}, function(error, sources) {
if (error) throw error;
for (var i = 0; i < sources.length; ++i) {
if (sources[i].name == "Electron") {
navigator.webkitGetUserMedia({
audio: false,
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: sources[i].id,
minWidth: 1280,
maxWidth: 1280,
minHeight: 720,
maxHeight: 720
}
}
}, gotStream, getUserMediaError);
return;
}
}
});
function gotStream(stream) {
document.querySelector('video').src = URL.createObjectURL(stream);
}
function getUserMediaError(e) {
console.log('getUserMediaError');
}
```
當調用 `navigator.webkitGetUserMedia` 時創建一個約束對象,如果使用 `desktopCapturer` 的資源,必須設置 `chromeMediaSource` 為 `"desktop"` ,并且 `audio` 為 `false`.
如果你想捕獲整個桌面的 audio 和 video,你可以設置 `chromeMediaSource` 為 `"screen"` ,和 `audio` 為 `true`.
當使用這個方法的時候,不可以指定一個 `chromeMediaSourceId`.
## 方法
`desktopCapturer` 模塊有如下方法:
### `desktopCapturer.getSources(options, callback)`
* `options` Object
* `types` Array - 一個 String 數組,列出了可以捕獲的桌面資源類型, 可用類型為 `screen` 和 `window`.
* `thumbnailSize` Object (可選) - 建議縮略可被縮放的 size, 默認為 `{width: 150, height: 150}`.
* `callback` Function
發起一個請求,獲取所有桌面資源,當請求完成的時候使用 `callback(error, sources)` 調用 `callback` .
`sources` 是一個 `Source` 對象數組, 每個 `Source` 表示了一個捕獲的屏幕或單獨窗口,并且有如下屬性 :
* `id` String - 在 `navigator.webkitGetUserMedia` 中使用的捕獲窗口或屏幕的 id . 格式為 `window:XX` 禍
`screen:XX`,`XX` 是一個隨機數.
* `name` String - 捕獲窗口或屏幕的描述名 . 如果資源為屏幕,名字為 `Entire Screen` 或 `Screen <index>`; 如果資源為窗口, 名字為窗口的標題.
* `thumbnail` [NativeImage](NativeImage.md) - 縮略圖.
**注意:** 不能保證 `source.thumbnail` 的 size 和 `options` 中的 `thumnbailSize` 一直一致. 它也取決于屏幕或窗口的縮放比例.
- 介紹
- 常見問題
- Electron 常見問題
- 向導
- 支持平臺
- 分發應用
- 提交應用到 Mac App Store
- 打包應用
- 使用 Node 原生模塊
- 主進程調試
- 使用 Selenium 和 WebDriver
- 使用開發人員工具擴展
- 使用 Pepper Flash 插件
- 使用 Widevine CDM 插件
- 教程
- 快速入門
- 桌面環境集成
- 在線/離線事件探測
- API文檔
- 簡介
- 進程對象
- 支持的 Chrome 命令行開關
- 環境變量
- 自定義的 DOM 元素
- File 對象
- &lt;webview&gt; 標簽
- window.open 函數
- 在主進程內可用的模塊
- app
- autoUpdater
- BrowserWindow
- contentTracing
- dialog
- globalShortcut
- ipcMain
- Menu
- MenuItem
- powerMonitor
- powerSaveBlocker
- protocol
- session
- webContents
- Tray
- 在渲染進程(網頁)內可用的模塊
- desktopCapturer
- ipcRenderer
- remote
- webFrame
- 在兩種進程中都可用的模塊
- clipboard
- crashReporter
- nativeImage
- screen
- shell
- 開發
- 代碼規范
- 源碼目錄結構
- 與 NW.js(原 node-webkit)在技術上的差異
- 構建系統概覽
- 構建步驟(OS X)
- 構建步驟(Windows)
- 構建步驟(Linux)
- 在調試中使用 Symbol Server