简体   繁体   English

从位图创建蒙版

[英]create a mask from bitmap

I have an image as bitmap eg我有一个图像作为位图,例如在此处输入图片说明

I want to create mask programmatically from that bitmap like this我想从这样的位图以编程方式创建掩码

在此处输入图片说明

I searched online but did not find any solution.我在网上搜索,但没有找到任何解决方案。

2003 Java Q and A about Masking Images 2003 Java Q and A about Masking Images

The question posed on this website seems similar to yours and the answers should help you out.本网站上提出的问题似乎与您的相似,答案应该对您有所帮助。 Their code was written in Java back in 2003, but I believe you are asking about something you plan to program in Android Studio from your tags in I am guessing Java.他们的代码是在 2003 年用 Java 编写的,但我相信你是在询问你打算在 Android Studio 中从你的标签中编程的东西,我猜是 Java。 Your question is kind of vague, but maybe this website will be a good starting point.你的问题有点含糊,但也许这个网站会是一个很好的起点。 There are longer solutions, with complete code written out, but I'll post one of the solutions listed.有更长的解决方案,写出完整的代码,但我会发布列出的解决方案之一。

One of the Solutions posted on that forum is this:该论坛上发布的解决方案之一是:

//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);

I found a solution to my problem i solved my problem by tinting my bitmap using PorterDuffColorFilter in a following way.我找到了我的问题的解决方案 我通过以下方式使用 PorterDuffColorFilter 对我的位图进行着色来解决我的问题。

    public Bitmap tintBitmap(Bitmap bitmap, int color) {
          Paint paint = new Paint();
          paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
          Bitmap bitmapResult = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
          Canvas canvas = new Canvas(bitmapResult);
          canvas.drawBitmap(bitmap, 0, 0, paint);
          return bitmapResult;
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM