首页 > 解决方案 > 从位图创建蒙版

问题描述

我有一个图像作为位图,例如 在此处输入图像描述

我想像这样从该位图以编程方式创建掩码

在此处输入图像描述

我在网上搜索但没有找到任何解决方案。

标签: androidbitmapmasking

解决方案


2003 Java Q and A 关于屏蔽图像

该网站上提出的问题似乎与您的问题相似,答案应该可以帮助您。他们的代码早在 2003 年是用 Java 编写的,但我相信您是在询问您计划在 Android Studio 中从您的标签中编程的东西,我猜是 Java。您的问题有点模糊,但也许这个网站将是一个很好的起点。有更长的解决方案,写出了完整的代码,但我会发布列出的解决方案之一。

该论坛上发布的解决方案之一是:

//get the image pixel

int imgPixel = image.getRGB(x,y);

//get the mask pixel

int maskPixel = mask.getRGB(x,y);

//now, get rid of everything but the blue channel

//and shift the blue channel into the alpha channels sample space.

maskPixel = (maskPixel &0xFF)<<24

//now, merge img and mask pixels and copy them back to the image

image.setRGB(x,y,imgPixel|maskPixel);

推荐阅读