pub struct MaaImageBuffer { /* private fields */ }Expand description
Image buffer for storing and manipulating image data.
Supports raw BGR data and encoded formats (PNG/JPEG). Compatible with OpenCV image types.
Implementations§
Source§impl MaaImageBuffer
impl MaaImageBuffer
Sourcepub fn new() -> MaaResult<Self>
pub fn new() -> MaaResult<Self>
Create a new buffer.
Examples found in repository?
37 fn analyze(
38 &self,
39 context: &Context,
40 _task_id: sys::MaaTaskId,
41 node_name: &str,
42 custom_recognition_name: &str,
43 custom_recognition_param: &str,
44 _image: &maa_framework::buffer::MaaImageBuffer,
45 _roi: &maa_framework::common::Rect,
46 ) -> Option<(maa_framework::common::Rect, String)> {
47 println!(
48 "[MyRecognition] analyze called: node={}, name={}, param={}",
49 node_name, custom_recognition_name, custom_recognition_param
50 );
51
52 // --- Context API Demo ---
53
54 // 1. Run sub-pipeline recognition with override
55 let pp_override = r#"{"MyCustomOCR": {"recognition": "OCR", "roi": [100, 100, 200, 300]}}"#;
56 if let Ok(img_buf) = buffer::MaaImageBuffer::new() {
57 let reco_id = context.run_recognition("MyCustomOCR", pp_override, &img_buf);
58 println!(" run_recognition result: {:?}", reco_id);
59 }
60
61 // 2. Take a new screenshot via tasker's controller
62 let tasker_ptr = context.tasker_handle();
63 if !tasker_ptr.is_null() {
64 // Note: In real usage, you would use the Tasker's controller reference
65 println!(" tasker handle available for controller access");
66 }
67
68 // 3. Async click - post now, will wait later
69 // (In real code, you'd get controller from context.tasker)
70 println!(" [Demo] Would post async click at (10, 20)");
71
72 // 4. Override pipeline for all subsequent operations in this context
73 let _ = context.override_pipeline(r#"{"MyCustomOCR": {"roi": [1, 1, 114, 514]}}"#);
74
75 // 5. Check if tasker is stopping - IMPORTANT for responsive cancellation
76 // Note: Context provides tasker_handle() which returns a raw pointer.
77 // In production code, you should wrap this or use the Tasker instance directly.
78 // Here we demonstrate the raw API call for completeness.
79 let tasker_ptr = context.tasker_handle();
80 if !tasker_ptr.is_null() {
81 // Using sys:: directly requires unsafe - this matches C API usage
82 let is_stopping = unsafe { sys::MaaTaskerStopping(tasker_ptr) != 0 };
83 if is_stopping {
84 println!(" Task is stopping, returning early!");
85 return Some((
86 common::Rect {
87 x: 0,
88 y: 0,
89 width: 0,
90 height: 0,
91 },
92 r#"{"status": "Task Stopped"}"#.to_string(),
93 ));
94 }
95 }
96
97 // 6. Wait for the async click to complete
98 println!(" [Demo] Async click would complete here");
99
100 // 7. Clone context for independent operations (modifications won't affect original)
101 if let Ok(new_ctx) = context.clone_context() {
102 let _ = new_ctx.override_pipeline(r#"{"MyCustomOCR": {"roi": [100, 200, 300, 400]}}"#);
103
104 // Run recognition and use the result
105 if let Ok(img_buf) = buffer::MaaImageBuffer::new() {
106 let reco_id = new_ctx.run_recognition("MyCustomOCR", "{}", &img_buf);
107
108 // If recognition succeeded, get the tasker to retrieve details
109 if reco_id.is_ok() {
110 // In a real scenario, you would:
111 // 1. Get recognition detail from tasker
112 // 2. Check if it hit
113 // 3. Use the box coordinates to click
114 println!(" [Demo] Would get reco detail and click on result box");
115
116 // Example of clicking on recognition result box:
117 // if let Ok(Some(detail)) = tasker.get_recognition_detail(reco_id) {
118 // if detail.hit {
119 // let box_rect = detail.box_rect;
120 // controller.post_click(box_rect.x, box_rect.y).wait();
121 // }
122 // }
123 }
124 }
125 // new_ctx changes don't affect original context
126 }
127
128 // 8. Get current task ID
129 let task_id = context.task_id();
130 println!(" current task_id: {}", task_id);
131
132 // 9. Get task job for result retrieval
133 let task_job = context.get_task_job();
134 if let Ok(Some(detail)) = task_job.get(false) {
135 println!(" task entry: {}", detail.entry);
136 }
137
138 // Return recognition result: bounding box + detail JSON
139 Some((
140 common::Rect {
141 x: 0,
142 y: 0,
143 width: 100,
144 height: 100,
145 },
146 r#"{"message": "Hello World!"}"#.to_string(),
147 ))
148 }Sourcepub unsafe fn from_raw(handle: *mut MaaImageBuffer) -> Self
pub unsafe fn from_raw(handle: *mut MaaImageBuffer) -> Self
Create from an existing handle.
§Safety
This function assumes the handle is valid. The returned buffer will NOT take ownership of the handle (it won’t be destroyed on drop). Use this when you are borrowing a handle from the C API.
Sourcepub fn from_handle(handle: *mut MaaImageBuffer) -> Option<Self>
pub fn from_handle(handle: *mut MaaImageBuffer) -> Option<Self>
Create from an existing handle safely (checks for null).
Returns None if handle is null.
Sourcepub fn as_ptr(&self) -> *mut MaaImageBuffer
pub fn as_ptr(&self) -> *mut MaaImageBuffer
Get the underlying raw handle.
Sourcepub fn raw(&self) -> *mut MaaImageBuffer
pub fn raw(&self) -> *mut MaaImageBuffer
Get the underlying raw handle (alias for as_ptr).
Source§impl MaaImageBuffer
impl MaaImageBuffer
Sourcepub fn image_type(&self) -> i32
pub fn image_type(&self) -> i32
Get the OpenCV image type constant.
Sourcepub fn to_vec(&self) -> Option<Vec<u8>>
pub fn to_vec(&self) -> Option<Vec<u8>>
Get the encoded image data as a byte vector (PNG format).
Sourcepub fn set_raw_data(
&mut self,
data: &[u8],
width: i32,
height: i32,
img_type: i32,
) -> MaaResult<()>
pub fn set_raw_data( &mut self, data: &[u8], width: i32, height: i32, img_type: i32, ) -> MaaResult<()>
Set the image from raw BGR data.
Sourcepub fn set(&mut self, data: &[u8], width: i32, height: i32) -> MaaResult<()>
pub fn set(&mut self, data: &[u8], width: i32, height: i32) -> MaaResult<()>
Set the image from raw BGR data (assuming 3 channels, CV_8UC3).
Sourcepub fn set_encoded(&mut self, data: &[u8]) -> MaaResult<()>
pub fn set_encoded(&mut self, data: &[u8]) -> MaaResult<()>
Set the image from encoded data (PNG/JPEG).