nashaofu / xcap

XCap is a cross-platform screen capture library written in Rust. It supports Linux (X11, Wayland), MacOS, and Windows. XCap supports screenshot and video recording (to be implemented).

Home Page:https://docs.rs/xcap

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Image JS Buffer (Neon)

Azperin opened this issue · comments

commented

How to return vec<u8> buffer as JsBuffer ? For later use with opencv in nodejs.
And feature request to add method .js_buffer() if possible

fn take_screenshot(mut cx: FunctionContext) -> JsResult<JsBuffer> {
  let start = Instant::now();
  let screen = Screen::from_point(100, 100).unwrap();
  let image = screen.capture_area(300, 300, 300, 300).unwrap();
  let buffer = image.buffer();
  
  
  Ok(buffer)
}

console error

commented

lib.rs

fn take_screenshot(mut cx: FunctionContext) -> JsResult<JsArray> {
  // let start = Instant::now();
  let screen = Screen::from_point(0, 0).unwrap();
  let image = screen.capture_area(0, 0, 900, 900).unwrap();
  let buffer = image.buffer();
  
  let js_buffer = JsArray::new(&mut cx, buffer.len() as u32);
  for (i, &item) in buffer.iter().enumerate() {
    let v = cx.number(item as f64);
    js_buffer.set(&mut cx, i as u32, v)?;
  };
  Ok(js_buffer)
}

NodeJS, atleast I can proccess it now =)

const shot = require('./index.node');
const fs = require('fs');
const screenshotBuffer = Buffer.from(  shot.take_screenshot() );
fs.writeFileSync('./test.bmp', screenshotBuffer);
commented

Can this help you?https://github.com/nashaofu/node-screenshots

Looks like exactly what I need. Today gonna test which one is faster for my purpose.

commented

Damn, https://github.com/nashaofu/node-screenshots is twice faster =)
thank you