Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tencent Ncnn Mat Pixel Android

From Leeroopedia


Knowledge Sources
Domains Android, Computer Vision
Last Updated 2026-02-09 19:00 GMT

Overview

Provides Android-specific convenience methods for converting between Android Bitmap objects (accessed via JNI) and ncnn Mat tensors, enabling direct image input from Android's native bitmap format.

Description

This file (213 lines) implements five Mat static and instance methods guarded by NCNN_PLATFORM_API and __ANDROID_API__ >= 9. The methods bridge the gap between Android's android.graphics.Bitmap Java class (accessed through JNI) and ncnn's Mat tensor format.

Each method follows the same pattern:

  1. Retrieve bitmap info via AndroidBitmap_getInfo() to determine format and dimensions
  2. Determine the source pixel format (PIXEL_GRAY for ANDROID_BITMAP_FORMAT_A_8 or PIXEL_RGBA for ANDROID_BITMAP_FORMAT_RGBA_8888)
  3. Lock the pixel buffer with AndroidBitmap_lockPixels()
  4. Delegate to the appropriate Mat::from_pixels* or to_pixels_resize method for format conversion
  5. Unlock with AndroidBitmap_unlockPixels()

The five methods are:

  • Mat::from_android_bitmap() -- Basic conversion from bitmap to Mat with optional pixel format conversion.
  • Mat::from_android_bitmap_resize() -- Converts and resizes to target dimensions in one step.
  • Mat::from_android_bitmap_roi() -- Extracts a region of interest (ROI) from the bitmap.
  • Mat::from_android_bitmap_roi_resize() -- Extracts ROI and resizes to target dimensions.
  • Mat::to_android_bitmap() -- Writes Mat data back into an Android Bitmap.

All methods handle automatic pixel format negotiation using PIXEL_CONVERT_MASK and PIXEL_CONVERT_SHIFT bit manipulation to determine whether a format conversion is needed between the bitmap's native format and the requested target format.

Usage

Use these methods in Android JNI native code to load camera frames or gallery images directly into ncnn Mat tensors for inference, and to write results back to bitmaps for display. This eliminates the need for manual pixel buffer management in Android applications.

Code Reference

Source Location

Signature

// Convert Android Bitmap to ncnn Mat
static Mat Mat::from_android_bitmap(JNIEnv* env, jobject bitmap,
    int type_to, Allocator* allocator);

// Convert and resize in one step
static Mat Mat::from_android_bitmap_resize(JNIEnv* env, jobject bitmap,
    int type_to, int target_width, int target_height, Allocator* allocator);

// Extract region of interest from bitmap
static Mat Mat::from_android_bitmap_roi(JNIEnv* env, jobject bitmap,
    int type_to, int roix, int roiy, int roiw, int roih, Allocator* allocator);

// Extract ROI and resize
static Mat Mat::from_android_bitmap_roi_resize(JNIEnv* env, jobject bitmap,
    int type_to, int roix, int roiy, int roiw, int roih,
    int target_width, int target_height, Allocator* allocator);

// Write Mat back to Android Bitmap
void Mat::to_android_bitmap(JNIEnv* env, jobject bitmap, int type_from) const;

Import

#include "mat.h"
#include <android/bitmap.h>
#include <jni.h>

I/O Contract

Inputs

Name Type Required Description
env JNIEnv* Yes JNI environment pointer from the Android runtime
bitmap jobject Yes Android Bitmap Java object reference
type_to int Yes Target pixel format (e.g., ncnn::Mat::PIXEL_BGR, PIXEL_RGB)
target_width int For resize variants Target width for resizing
target_height int For resize variants Target height for resizing
roix, roiy int For ROI variants Top-left corner of the region of interest
roiw, roih int For ROI variants Width and height of the region of interest
allocator Allocator* No Custom memory allocator (can be nullptr)
type_from int For to_android_bitmap Source pixel format of the Mat data

Outputs

Name Type Description
return (from_*) ncnn::Mat New Mat containing the converted pixel data; empty Mat on unsupported bitmap format
bitmap (to_android_bitmap) jobject The Android Bitmap is written to in-place with Mat pixel data

Usage Examples

Loading Android Camera Frame for Inference

// In JNI native method:
extern "C" JNIEXPORT void JNICALL
Java_com_example_MyActivity_detect(JNIEnv* env, jobject, jobject bitmap)
{
    // Convert RGBA bitmap to BGR ncnn Mat
    ncnn::Mat in = ncnn::Mat::from_android_bitmap(env, bitmap,
        ncnn::Mat::PIXEL_BGR);

    // Or convert and resize to model input size
    ncnn::Mat in_resized = ncnn::Mat::from_android_bitmap_resize(env, bitmap,
        ncnn::Mat::PIXEL_BGR, 224, 224);

    // Run inference...
}

Writing Results Back to Bitmap

// Write detection result visualization back to bitmap
ncnn::Mat result_pixels = /* processed image */;
result_pixels.to_android_bitmap(env, output_bitmap, ncnn::Mat::PIXEL_RGBA);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment