|
| 1 | +// |
| 2 | +// Copyright (C) 2024 nihui |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +#include <opencv2/core.hpp> |
| 18 | +#include <opencv2/highgui.hpp> |
| 19 | + |
| 20 | +#include <stdio.h> |
| 21 | +#include <string.h> |
| 22 | + |
| 23 | +#if defined __linux__ && !__ANDROID__ |
| 24 | +#include "writer_http.h" |
| 25 | +#endif |
| 26 | + |
| 27 | +namespace cv { |
| 28 | + |
| 29 | +class VideoWriterImpl |
| 30 | +{ |
| 31 | +public: |
| 32 | + VideoWriterImpl(); |
| 33 | + |
| 34 | +public: |
| 35 | + bool is_opened; |
| 36 | + int width; |
| 37 | + int height; |
| 38 | + float fps; |
| 39 | + |
| 40 | +#if defined __linux__ && !__ANDROID__ |
| 41 | + writer_http wt_http; |
| 42 | +#endif |
| 43 | +}; |
| 44 | + |
| 45 | +VideoWriterImpl::VideoWriterImpl() |
| 46 | +{ |
| 47 | + is_opened = false; |
| 48 | + width = 0; |
| 49 | + height = 0; |
| 50 | + fps = 0; |
| 51 | +} |
| 52 | + |
| 53 | +VideoWriter::VideoWriter() : d(new VideoWriterImpl) |
| 54 | +{ |
| 55 | +} |
| 56 | + |
| 57 | +VideoWriter::~VideoWriter() |
| 58 | +{ |
| 59 | + release(); |
| 60 | + |
| 61 | + delete d; |
| 62 | +} |
| 63 | + |
| 64 | +bool VideoWriter::open(const String& name, int port) |
| 65 | +{ |
| 66 | + if (d->is_opened) |
| 67 | + { |
| 68 | + release(); |
| 69 | + } |
| 70 | + |
| 71 | + if (name == "httpjpg") |
| 72 | + { |
| 73 | +#if defined __linux__ && !__ANDROID__ |
| 74 | + if (writer_http::supported()) |
| 75 | + { |
| 76 | + int ret = d->wt_http.open(port); |
| 77 | + if (ret == 0) |
| 78 | + { |
| 79 | + d->is_opened = true; |
| 80 | + } |
| 81 | + } |
| 82 | + else |
| 83 | +#endif |
| 84 | + { |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return d->is_opened; |
| 89 | +} |
| 90 | + |
| 91 | +bool VideoWriter::isOpened() const |
| 92 | +{ |
| 93 | + return d->is_opened; |
| 94 | +} |
| 95 | + |
| 96 | +void VideoWriter::release() |
| 97 | +{ |
| 98 | + if (!d->is_opened) |
| 99 | + return; |
| 100 | + |
| 101 | +#if defined __linux__ && !__ANDROID__ |
| 102 | + if (writer_http::supported()) |
| 103 | + { |
| 104 | + d->wt_http.close(); |
| 105 | + } |
| 106 | + else |
| 107 | +#endif |
| 108 | + { |
| 109 | + } |
| 110 | + |
| 111 | + d->is_opened = false; |
| 112 | + d->width = 0; |
| 113 | + d->height = 0; |
| 114 | + d->fps = 0; |
| 115 | +} |
| 116 | + |
| 117 | +VideoWriter& VideoWriter::operator<<(const Mat& image) |
| 118 | +{ |
| 119 | + write(image); |
| 120 | + |
| 121 | + return *this; |
| 122 | +} |
| 123 | + |
| 124 | +void VideoWriter::write(const Mat& image) |
| 125 | +{ |
| 126 | + if (!d->is_opened) |
| 127 | + return; |
| 128 | + |
| 129 | +#if defined __linux__ && !__ANDROID__ |
| 130 | + if (writer_http::supported()) |
| 131 | + { |
| 132 | + // encode jpg |
| 133 | + std::vector<uchar> buf; |
| 134 | + cv::imencode(".JPG", image, buf); |
| 135 | + |
| 136 | + d->wt_http.write_jpgbuf((const unsigned char*)buf.data(), buf.size()); |
| 137 | + } |
| 138 | + else |
| 139 | +#endif |
| 140 | + { |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +} // namespace cv |
0 commit comments