2

I am using this blog post to generate a the masking effect. I might be missing some thing , I am not able to achieve the intended behavior. I am new to image processing. based on my internet research I am assuming I got to have a JPEG image for the originol image and the PNG format for the mask image with the same dimension . I even tried to create images as below

My Images : some of the images I created to create masking effect : enter image description here enter image description here

But the resulted Image using this logic looks like this ,

enter image description here .

Why is it ? Am I missing some thing here ? The only Images working so far is the one in the example on that link.

My masking code :

 public static Bitmap getMaskedBitmap(Resources res, int sourceResId, int maskResId) {

    BitmapFactory.Options options = new BitmapFactory.Options();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            options.inMutable = true;
    }
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap source = BitmapFactory.decodeResource(res, sourceResId, options);
    Bitmap bitmap;
    if (source.isMutable()) {
        bitmap = source;
    } else {
      bitmap = source.copy(Bitmap.Config.ARGB_8888, true);
      source.recycle();
   }

  bitmap.setHasAlpha(true);
  Canvas canvas = new Canvas(bitmap);
  Bitmap mask = BitmapFactory.decodeResource(res, maskResId);
  Paint paint = new Paint();
  paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
  canvas.drawBitmap(mask, 0, 0, paint);
  mask.recycle();
  return bitmap;

}

  1. Please let me know where am I going wrong ?
  2. Is there any requirement for the Images ? especially Mask Image

I have tested on Samsung S3 OS 4.2


UPDate : I tried the mask Image from the blog post with my own JPEG .IT works perfect so the issue is narrowed down to mask Image. It expects some kind of configuration. Please let me know about it if any one know faced this before or am I missing some thing .


Latest UPDate :

I finally figured out. it is the alpha channel inside a mask image that makes the difference . If you are a programmer without a UI designer then you got to take care of this alpha channel

rana
  • 1,824
  • 3
  • 21
  • 36
  • can some one change the state please ? – rana Jun 11 '14 at 00:38
  • Your question sounds very interesting but lacking things like what you have tried so far. Add some code, and I will try to vote this question for reopen. If you dont post something, we wont be able to figure out where you got it wrong. Wondering why it is put on hold as off-topic though. – Lazy Ninja Jun 11 '14 at 00:41
  • i refered the code from a blog anyways added the code here too – rana Jun 11 '14 at 00:50
  • usually off site links are frowned up in SO. If the link is dead, the question will be become obsolete. Anyway I will vote to reopen. – Lazy Ninja Jun 11 '14 at 00:54

2 Answers2

0

Referring to this SO answer Android: Bitmap recycle() how does it work?

This particular line of code is likely the problem. I can say you probably should not call

mask.recycle();

I haven't tested it but I think its worth a shot.This should work since when you recycle mask the masked image itself is GCed. So if you don't call it it should work.

Community
  • 1
  • 1
Parth Sane
  • 587
  • 7
  • 24
  • i tried without it. it is not working. I figured out the masking image needs to have an alpha masking. Still not sure is it the valid claim ? – rana Jun 11 '14 at 20:21
0

I finally figured out , the mask image got to have a alpha channel . On Photoshop you have to add a transparency layer to get set the alpha channel enabled.

rana
  • 1,824
  • 3
  • 21
  • 36