You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android MLKit中将图形叠加层转换为Bitmap

Got it, let's walk through how to convert that segmentation overlay graphic into a Bitmap. Based on the SegmentationGraphic class logic from the ML Kit sample, here's a straightforward, actionable approach:

Convert Segmentation Overlay to Bitmap

Core Idea

Instead of drawing the segmentation mask directly to a preview canvas (like the sample does), we'll create a mutable Bitmap, attach a Canvas to it, and run the same drawing logic on this Bitmap-linked canvas.

Step 1: Prepare the Bitmap & Canvas

First, create a Bitmap that matches the dimensions of your input image or camera frame. Then get a Canvas instance tied to this Bitmap—this is where we'll draw the segmentation overlay.

// Replace these with your actual input image/frame dimensions
int inputWidth = ...; 
int inputHeight = ...;

// Create a mutable Bitmap to hold the segmentation overlay
Bitmap segmentationBitmap = Bitmap.createBitmap(inputWidth, inputHeight, Bitmap.Config.ARGB_8888);
Canvas bitmapCanvas = new Canvas(segmentationBitmap);

Step 2: Adapt the Segmentation Drawing Logic

Take the core mask-rendering code from the sample's draw(Canvas) method and adapt it to draw on your bitmap's canvas. Here's the key part:

// Get your ML Kit segmentation result (adjust this line to match your result retrieval)
SegmentationResult segmentationResult = ...;
SegmentationMask mask = segmentationResult.getSegmentationMask();
ByteBuffer maskBuffer = mask.getBuffer();
int maskWidth = mask.getWidth();
int maskHeight = mask.getHeight();

// Set up the paint for the overlay (semi-transparent green as in the sample)
Paint overlayPaint = new Paint();
overlayPaint.setColor(Color.argb(128, 0, 255, 0));

// Scale the canvas to match the mask to input image size
bitmapCanvas.save();
bitmapCanvas.scale(
    (float) inputWidth / maskWidth,
    (float) inputHeight / maskHeight
);

// Iterate through the mask buffer and draw foreground regions
for (int y = 0; y < maskHeight; y++) {
    for (int x = 0; x < maskWidth; x++) {
        float foregroundConfidence = maskBuffer.getFloat();
        // Only draw pixels where foreground confidence exceeds your threshold
        if (foregroundConfidence > 0.5) {
            bitmapCanvas.drawRect(x, y, x + 1, y + 1, overlayPaint);
        }
    }
}
bitmapCanvas.restore();

Step 3: Use the Resulting Bitmap

Now segmentationBitmap holds your segmentation overlay as a standalone Bitmap. You can display it in an ImageView, save it to storage, or combine it with the original image as needed.

Quick Notes

  • Adjust the threshold (0.5 in the example) and overlay color to fit your use case.
  • If your input image has rotation or scaling transformations (like camera preview orientation), add those to the bitmapCanvas before drawing (mirroring the transformations used in the sample's GraphicOverlay).

内容的提问来源于stack exchange,提问作者Hemant Bharti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 20:12:34