简体   繁体   中英

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

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. 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.

    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;
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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