With one camera attached, Darkgrade Slate's new Camera() opens it. With several, say which one: list them with Camera.discover(), then pass a filter that matches exactly one.
import { Camera } from '@darkgrade/slate'
const [first] = await Camera.discover()
if (!first) throw new Error('No camera on USB')
const { vendorId, serialNumber } = first
const camera = new Camera({
device: { usb: { filters: [{ vendorId, serialNumber }] } },
})
await camera.connect()List the cameras
Camera.discover() lists the PTP cameras on USB without opening a session on any of them. Each entry has vendorId and productId, and serialNumber, manufacturer and model when the device reports them: see DiscoveredCamera.
discover() covers USB only. A camera on the network is opened by address: see Wi-Fi with PTP/IP.
Open one camera
A DeviceDescriptor says which device a Camera opens. Pass it to the constructor as device, or to connect({ device }) to override it for one connection.
usb.filters[] field | Matches |
|---|---|
vendorId | One maker's cameras. The ids are in Vendor IDs. |
productId | One model. |
serialNumber | One body. Use it whenever two of the same model are attached. |
classCode, subclassCode, protocolCode | A USB interface class. Without a descriptor, Slate matches any still-image (PTP) interface. |
A device must match at least one filter and none of usb.exclusionFilters, which take the same fields.
A descriptor must match exactly one camera. If it matches several, connect() throws Multiple USB cameras match; select a unique serialNumber before connecting and opens none of them. A filter field left undefined matches anything, so check that discover() gave you a serialNumber before you rely on it.
Run multiple cameras
Each Camera holds its own connection and its own queue, so separate cameras run side by side:
import { Camera } from '@darkgrade/slate'
const found = await Camera.discover()
const cameras = found.map(({ vendorId, productId, serialNumber }) => {
const filters = [{ vendorId, productId, serialNumber }]
return new Camera({ device: { usb: { filters } } })
})
await Promise.all(cameras.map(camera => camera.connect()))
try {
const photos = await Promise.all(
cameras.map(camera => camera.takePhoto()),
)
console.log(photos.map(photo => photo.name))
} finally {
await Promise.allSettled(cameras.map(camera => camera.disconnect()))
}The photos are requested together, not triggered on the same instant: Slate does not synchronise shutters between cameras.
To start and stop video on several cameras as one take, use VideoTake.
Tell which camera is which
Once a camera is open, getInfo() reads what the body says about itself: manufacturer, model, firmware, and serialNumber when the body reports one.
To name a device before you open it, identifyCamera looks up its USB ids and returns a CameraIdentification:
import { Camera, identifyCamera } from '@darkgrade/slate'
for (const { vendorId, productId } of await Camera.discover()) {
const { status, candidates } = identifyCamera({ vendorId, productId })
console.log(status, candidates.map(candidate => candidate.models))
}status | Meaning |
|---|---|
'matched' | One camera line uses these ids. |
'ambiguous' | Several lines share these ids. All are in candidates. |
'unknown' | Slate has no record of these ids. |
'identity-mismatch' | You passed ptpModel and it names none of the candidates. |
Pass ptpModel (the model from getInfo()) to narrow an ambiguous match. Recognising a device says nothing about what it can do: getCapabilities() does.
One camera, several programs
Only one program can hold a camera's USB session. To let an AI app, a script and your own UI use the same camera, hold it in one process and share that: see Host cameras for other processes.
On macOS, the system's own camera service counts as one of those programs: it claims a camera as soon as it is plugged in. Prepare your computer shows how to stop it with killMacCameraDaemons().