smasherprog / screen_capture_lite

cross platform screen/window capturing library
MIT License
616 stars 156 forks source link

anyone translated the image to opencv's cv::Mat ? #124

Closed EMCP closed 2 years ago

EMCP commented 2 years ago

This is still in progress as well, but looking how to translate the SL::Screen_Capture::Image to cv::Mat .. purpose is so a client on the other side of the socket can read the bytes and display the frame

void transmitsocketdata(tcp::socket & socket, const SL::Screen_Capture::Image & img){

  cv::Mat frame = NULL;  // <---this line needs to be filled in

  std::vector<std::uint8_t> buff;
  std::vector<int> param(2);
  cv::imencode(".jpg", frame, buff, param);

  // now we send the header message
  std::string image_dimensions = "6016Wx3384H";
  std::string image_buff_bytes = std::to_string(buff.size());
  std::string message_header = image_dimensions + "," +  image_buff_bytes;
  std::cout << "sending message header of " << std::to_string(message_header.length()) << " bytes...." << std::endl;
  message_header.append(63 - message_header.length(), ' ');
  message_header = message_header + '\0';

  std::cout << "sending message header of " << std::to_string(message_header.length()) << " bytes...." << std::endl;
  socket.write_some(boost::asio::buffer(message_header), ignored_error);
  std::cout << "sending image message of " << image_buff_bytes << " bytes...." << std::endl;
  socket.write_some(boost::asio::buffer(buff), ignored_error);
}

Im sort of guessing it will be a sort of unisigned char array, using the dimensions sort of option.. but if anyone has already done this would appreciate to understand how to and share this example once finished

EMCP commented 2 years ago

I guess I am honing in on this example code now.. where we sort of extract the data for PNG creation.. ideally I'd like to make it a JPG within a buffer but.. a bit concerned I need to first


void ExtractAndConvertToIntVect(const SL::Screen_Capture::Image &img, std::vector<std::uint8_t> *dst, size_t dst_size)
{
    assert(dst_size >= static_cast<size_t>(SL::Screen_Capture::Width(img) * SL::Screen_Capture::Height(img) * sizeof(SL::Screen_Capture::ImageBGRA)));
    auto imgsrc = StartSrc(img);

    std::cout << "image was contiguous, dst_size = " << std::to_string(dst_size) << std::endl;
    for (auto h = 0; h < Height(img); h++) {
      auto startimgsrc = imgsrc;
      for (auto w = 0; w < Width(img); w++) {
          dst->push_back(imgsrc->R);
          dst->push_back(imgsrc->G);
          dst->push_back(imgsrc->B);
          imgsrc++;
      }
      imgsrc = SL::Screen_Capture::GotoNextRow(img, startimgsrc);
    }
    std::cout << "copied... " << std::to_string(dst_size) << std::endl;  
}