arduino-audio-tools
JpegOpenCV.h
1 #pragma once
2 
3 #include "Video/Video.h"
4 #include <opencv2/core/core.hpp>
5 #include <opencv2/highgui/highgui.hpp>
6 
7 namespace audio_tools {
8 
15 class JpegOpenCV : public VideoOutput {
16 public:
17  JpegOpenCV() = default;
18 
19  // Allocate memory and create window
20  void beginFrame(size_t jpegSize) override {
21  if (start == 0l)
22  start = millis();
23  LOGI("jpegSize: %d", (int)jpegSize);
24  // prevent memory fragmentation, change size only if more memory is needed
25  if (img_vector.size() < jpegSize) {
26  img_vector.resize(jpegSize);
27  }
28  this->pos = 0;
29  this->open = jpegSize;
30  this->size = jpegSize;
31 
32  if (create_window) {
33  create_window = false;
34  // create image window named "My Image"
35  cv::namedWindow(window);
36  }
37  }
38 
41  uint32_t endFrame() override {
42  display();
43  return millis() - start;
44  }
45 
46  // Add some more data to the image vector
47  size_t write(const uint8_t *data, size_t len) override {
48  memcpy(&img_vector[pos], data, len);
49  pos += len;
50  open -= len;
51  return len;
52  }
53 
54 protected:
55  bool create_window = true;
56  std::vector<uint8_t> img_vector;
57  const char *window = "Movie";
58  size_t pos = 0;
59  size_t size = 0;
60  int open = 0;
61  uint64_t start = 0;
62 
63  void display() {
64  assert(open == 0);
65 
66  cv::Mat data(1, size, CV_8UC1, (void *)&img_vector[0]);
67  // cv::InputArray input_array(img_vector);
68  cv::Mat mat = cv::imdecode(data, 0);
69  cv::imshow(window, mat);
70  // cv::pollKey();
71  cv::waitKey(1);
72  }
73 };
74 
75 } // namespace audio_tools
Display image with opencv to be used on the desktop.
Definition: JpegOpenCV.h:15
uint32_t endFrame() override
Definition: JpegOpenCV.h:41
Abstract class for video playback. This class is used to assemble a complete video frame in memory.
Definition: Video.h:21
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823
uint32_t millis()
Returns the milliseconds since the start.
Definition: Time.h:12