ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You could decode the uint8[] array raw image and encode it as jpeg as follows:

  import jpeg from 'jpeg-js'

  function rgb8ImageToBase64Jpeg (msg) {
    var raw = atob(msg.data)
    var array = new Uint8Array(new ArrayBuffer(raw.length))
    for (let i = 0; i < raw.length; i++) {
      array[i] = raw.charCodeAt(i)
    }

    var frameData = Buffer.alloc(msg.width * msg.height * 4)
    for (let i = 0; i < msg.width * msg.height; i++) {
      frameData[4 * i + 0] = array[3 * i + 0]
      frameData[4 * i + 1] = array[3 * i + 1]
      frameData[4 * i + 2] = array[3 * i + 2]
      frameData[4 * i + 3] = 0
    }
    var rawImageData = {
      data: frameData,
      width: msg.width,
      height: msg.height
    }
    return jpeg.encode(rawImageData, 50).data.toString('base64')
  }

I do agree it is better to send a compressed image since this reduces the bandwidth. Or use the mentioned mjpeg server.

You could decode the uint8[] array raw image and encode it as jpeg as follows:

follows:
      import jpeg from 'jpeg-js'

'jpeg-js'

  function rgb8ImageToBase64Jpeg (msg) {
    var raw = atob(msg.data)
    var array = new Uint8Array(new ArrayBuffer(raw.length))
    for (let i = 0; i < raw.length; i++) {
      array[i] = raw.charCodeAt(i)
    }

    var frameData = Buffer.alloc(msg.width * msg.height * 4)
    for (let i = 0; i < msg.width * msg.height; i++) {
      frameData[4 * i + 0] = array[3 * i + 0]
      frameData[4 * i + 1] = array[3 * i + 1]
      frameData[4 * i + 2] = array[3 * i + 2]
      frameData[4 * i + 3] = 0
    }
    var rawImageData = {
      data: frameData,
      width: msg.width,
      height: msg.height
    }
    return jpeg.encode(rawImageData, 50).data.toString('base64')
  }

I do agree it is better to send a compressed image since this reduces the bandwidth. Or use the mentioned mjpeg server.

You could decode the uint8[] array raw image and encode it as jpeg as follows: follows:

  import jpeg from 'jpeg-js'

'jpeg-js'

  function rgb8ImageToBase64Jpeg (msg) {
    var raw = atob(msg.data)
    var array = new Uint8Array(new ArrayBuffer(raw.length))
    for (let i = 0; i < raw.length; i++) {
      array[i] = raw.charCodeAt(i)
    }

    var frameData = Buffer.alloc(msg.width * msg.height * 4)
    for (let i = 0; i < msg.width * msg.height; i++) {
      frameData[4 * i + 0] = array[3 * i + 0]
      frameData[4 * i + 1] = array[3 * i + 1]
      frameData[4 * i + 2] = array[3 * i + 2]
      frameData[4 * i + 3] = 0
    }
    var rawImageData = {
      data: frameData,
      width: msg.width,
      height: msg.height
    }
    return jpeg.encode(rawImageData, 50).data.toString('base64')
  }

I do agree it is better to send a compressed image since this reduces the bandwidth. Or use the mentioned mjpeg server.

You could decode the uint8[] array raw image and encode it as jpeg as follows:

  import jpeg from 'jpeg-js'

  function rgb8ImageToBase64Jpeg (msg) {
    var raw = atob(msg.data)
    var array = new Uint8Array(new ArrayBuffer(raw.length))
    for (let i = 0; i < raw.length; i++) {
      array[i] = raw.charCodeAt(i)
    }

    var frameData = Buffer.alloc(msg.width * msg.height * 4)
    for (let i = 0; i < msg.width * msg.height; i++) {
      frameData[4 * i + 0] = array[3 * i + 0]
      frameData[4 * i + 1] = array[3 * i + 1]
      frameData[4 * i + 2] = array[3 * i + 2]
      frameData[4 * i + 3] = 0
    }
    var rawImageData = {
      data: frameData,
      width: msg.width,
      height: msg.height
    }
    return jpeg.encode(rawImageData, 50).data.toString('base64')
  }

I do agree it is better to send a compressed image since this reduces the bandwidth. Or use the mentioned mjpeg server.server that is now named http://wiki.ros.org/web_video_server