Process raw RGBA frame data into FrameData
(
rgba: &[u8],
width: jint,
height: jint,
stride: jint,
frame_count: u64,
)
| 354 | |
| 355 | /// Process raw RGBA frame data into FrameData |
| 356 | fn process_frame( |
| 357 | rgba: &[u8], |
| 358 | width: jint, |
| 359 | height: jint, |
| 360 | stride: jint, |
| 361 | frame_count: u64, |
| 362 | ) -> Option<FrameData> { |
| 363 | let width = width as u32; |
| 364 | let height = height as u32; |
| 365 | let stride = stride as u32; |
| 366 | |
| 367 | // Handle stride padding - Android ImageReader may have row padding |
| 368 | let expected_stride = width * 4; // 4 bytes per RGBA pixel |
| 369 | |
| 370 | let image_data = if stride > expected_stride { |
| 371 | // Need to remove padding from each row |
| 372 | let mut unpacked = Vec::with_capacity((width * height * 4) as usize); |
| 373 | for row in 0..height { |
| 374 | let row_start = (row * stride) as usize; |
| 375 | let row_end = row_start + (width * 4) as usize; |
| 376 | if row_end <= rgba.len() { |
| 377 | unpacked.extend_from_slice(&rgba[row_start..row_end]); |
| 378 | } |
| 379 | } |
| 380 | unpacked |
| 381 | } else { |
| 382 | rgba.to_vec() |
| 383 | }; |
| 384 | |
| 385 | // Create RgbaImage |
| 386 | let image = match RgbaImage::from_raw(width, height, image_data) { |
| 387 | Some(img) => img, |
| 388 | None => { |
| 389 | log::error!( |
| 390 | "[AndroidCapture] Failed to create image from raw data ({}x{})", |
| 391 | width, |
| 392 | height |
| 393 | ); |
| 394 | return None; |
| 395 | } |
| 396 | }; |
| 397 | |
| 398 | // Downscale if too large |
| 399 | let resized = if width > MAX_WIDTH { |
| 400 | let scale = MAX_WIDTH as f32 / width as f32; |
| 401 | let new_height = (height as f32 * scale) as u32; |
| 402 | image::imageops::resize(&image, MAX_WIDTH, new_height, FilterType::Nearest) |
| 403 | } else { |
| 404 | image |
| 405 | }; |
| 406 | |
| 407 | let final_width = resized.width(); |
| 408 | let final_height = resized.height(); |
| 409 | |
| 410 | // Convert RGBA to RGB for JPEG encoding |
| 411 | let rgba_bytes = resized.as_raw(); |
| 412 | let mut rgb_bytes = Vec::with_capacity((final_width * final_height * 3) as usize); |
| 413 | for chunk in rgba_bytes.chunks_exact(4) { |
no test coverage detected