opt.优化截图和点击接口

This commit is contained in:
MistEO
2022-01-18 10:43:41 +08:00
parent ba06216ddd
commit fb6ccca542
3 changed files with 23 additions and 16 deletions

View File

@@ -472,7 +472,7 @@ std::vector<uchar> asst::Assistant::get_image() const
bool asst::Assistant::ctrler_click(int x, int y, bool block)
{
Ctrler.click_without_scale(Point(x, y), block);
Ctrler.click(Point(x, y), block);
return true;
}

View File

@@ -695,6 +695,20 @@ bool asst::Controller::screencap(const std::string & cmd, DecodeFunc decode_func
}
return false;
}
}
cv::Mat asst::Controller::get_resized_image() const
{
const static cv::Size dsize(m_scale_size.first, m_scale_size.second);
std::shared_lock<std::shared_mutex> image_lock(m_image_mutex);
if (m_cache_image.empty()) {
Log.error("image is empty");
return cv::Mat(dsize, CV_8UC3);
}
cv::Mat resized_mat;
cv::resize(m_cache_image, resized_mat, dsize);
return resized_mat;
}
int asst::Controller::click(const Point & p, bool block)
@@ -818,25 +832,15 @@ cv::Mat asst::Controller::get_image(bool raw)
return m_cache_image;
}
else {
const static cv::Size dsize(m_scale_size.first, m_scale_size.second);
if (m_cache_image.empty()) {
Log.error("image is empty");
return cv::Mat(dsize, CV_8UC3);
}
cv::Mat resized_mat;
cv::resize(m_cache_image, resized_mat, dsize);
return resized_mat;
return get_resized_image();
}
}
std::vector<uchar> asst::Controller::get_image_encode()
{
std::shared_lock<std::shared_mutex> image_lock(m_image_mutex);
cv::Mat img = get_resized_image();
std::vector<uchar> buf;
cv::imencode(".png", m_cache_image, buf);
image_lock.unlock();
cv::imencode(".png", img, buf);
return buf;
}

View File

@@ -69,9 +69,12 @@ namespace asst
void pipe_working_proc();
std::optional<std::vector<unsigned char>> call_command(const std::string& cmd);
int push_cmd(const std::string& cmd);
bool screencap();
using DecodeFunc = std::function<bool(const std::vector<uchar>&)>;
bool screencap();
bool screencap(const std::string& cmd, DecodeFunc decode_func);
cv::Mat get_resized_image() const;
Point rand_point_in_rect(const Rect& rect);
void random_delay() const;
@@ -90,7 +93,7 @@ namespace asst
std::atomic<unsigned> m_completed_id = 0;
unsigned m_push_id = 0; // push_id的自增总是伴随着queue的push肯定是要上锁的所以没必要原子
std::shared_mutex m_image_mutex;
mutable std::shared_mutex m_image_mutex;
cv::Mat m_cache_image;
constexpr static int PipeBuffSize = 4 * 1024 * 1024; // 管道缓冲区大小