feat.新增了截图和点击的外部接口(C层接口)

This commit is contained in:
MistEO
2022-01-17 15:48:00 +08:00
parent 4f314c6298
commit 18a044f584
6 changed files with 72 additions and 7 deletions

View File

@@ -2,6 +2,8 @@
#include "AsstPort.h"
typedef unsigned long long size_t;
namespace asst
{
class Assistant;
@@ -38,6 +40,9 @@ extern "C" {
bool ASSTAPI AsstSetPenguinId(asst::Assistant* p_asst, const char* id);
// bool ASSTAPI AsstSetParam(asst::Assistant* p_asst, const char* type, const char* param, const char* value);
size_t ASSTAPI AsstGetImage(asst::Assistant* p_asst, void* buff, size_t buff_size);
bool ASSTAPI AsstCtrlerClick(asst::Assistant* p_asst, int x, int y, bool block);
ASSTAPI_PORT const char* ASST_CALL AsstGetVersion();
#ifdef __cplusplus
}

View File

@@ -463,6 +463,17 @@ void asst::Assistant::set_penguin_id(const std::string& id)
else {
opt.penguin_report.extra_param = "-H \"authorization: PenguinID " + id + "\"";
}
}
std::vector<uchar> asst::Assistant::get_image() const
{
return Ctrler.get_image_encode();
}
bool asst::Assistant::ctrler_click(int x, int y, bool block)
{
Ctrler.click_without_scale(Point(x, y), block);
return true;
}
bool asst::Assistant::start(bool block)

View File

@@ -13,6 +13,8 @@
#include "AsstMsg.h"
#include "AsstInfrastDef.h"
typedef unsigned char uchar;
namespace cv
{
class Mat;
@@ -76,6 +78,9 @@ namespace asst
// 设置企鹅数据汇报个人ID
void set_penguin_id(const std::string& id);
std::vector<uchar> get_image() const;
bool ctrler_click(int x, int y, bool block = true);
[[deprecated]] bool set_param(const std::string& type, const std::string& param, const std::string& value);
static constexpr int ProcessTaskRetryTimesDefault = AbstractTask::RetryTimesDefault;

View File

@@ -248,6 +248,28 @@ bool AsstSetPenguinId(asst::Assistant* p_asst, const char* id)
p_asst->set_penguin_id(id);
return true;
}
size_t ASSTAPI AsstGetImage(asst::Assistant* p_asst, void* buff, size_t buff_size)
{
if (p_asst == nullptr || buff == nullptr) {
return 0;
}
auto img_data = p_asst->get_image();
size_t data_size = img_data.size();
if (buff_size < data_size) {
return 0;
}
memcpy(buff, img_data.data(), data_size);
return data_size;
}
bool ASSTAPI AsstCtrlerClick(asst::Assistant* p_asst, int x, int y, bool block)
{
if (p_asst == nullptr) {
return false;
}
return p_asst->ctrler_click(x, y, block);
}
//bool AsstSetParam(asst::Assistant* p_asst, const char* type, const char* param, const char* value)

View File

@@ -601,19 +601,28 @@ bool asst::Controller::screencap()
if (is_all_zero) {
return false;
}
m_cache_image = cv::Mat(
cv::Mat temp = cv::Mat(
adb.display_height,
adb.display_width,
CV_8UC4,
unzip_data.data() + header_size);
cv::cvtColor(m_cache_image, m_cache_image, cv::COLOR_RGB2BGR);
return !m_cache_image.empty();
if (temp.empty()) {
return false;
}
cv::cvtColor(temp, temp, cv::COLOR_RGB2BGR);
std::unique_lock<std::shared_mutex> image_lock(m_image_mutex);
m_cache_image = temp;
return true;
};
DecodeFunc decode_encode = [&](const std::vector<uchar>& data) -> bool {
m_cache_image = cv::imdecode(data, cv::IMREAD_COLOR);
return !m_cache_image.empty();
cv::Mat temp = cv::imdecode(data, cv::IMREAD_COLOR);
if (temp.empty()) {
return false;
}
std::unique_lock<std::shared_mutex> image_lock(m_image_mutex);
m_cache_image = temp;
return true;
};
switch (adb.screencap_method) {
@@ -818,4 +827,16 @@ cv::Mat asst::Controller::get_image(bool raw)
cv::resize(m_cache_image, resized_mat, dsize);
return resized_mat;
}
}
std::vector<uchar> asst::Controller::get_image_encode()
{
std::shared_lock<std::shared_mutex> image_lock(m_image_mutex);
std::vector<uchar> buf;
cv::imencode(".png", m_cache_image, buf);
image_lock.unlock();
return buf;
}

View File

@@ -38,6 +38,7 @@ namespace asst
bool try_capture(const EmulatorInfo& info, bool without_handle = false);
cv::Mat get_image(bool raw = false);
std::vector<uchar> get_image_encode();
// 点击和滑动都是异步执行返回该任务的id
int click(const Point& p, bool block = true);
@@ -89,7 +90,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;
std::shared_mutex m_image_mutex;
cv::Mat m_cache_image;
constexpr static int PipeBuffSize = 4 * 1024 * 1024; // 管道缓冲区大小