how to use alpha blending for android to blend 2 photos

943 views Asked by At

For an application I need to display a photo on the screen, and over it show each frame of the photo i'm taking now through the camera. I know I need to use Alpha blending to show part of each photo for each pixel, for example: on a certain pixel i'll display 30% of one pic and 70% of the other. Can I please get an explanation on how this could be done.

1

There are 1 answers

1
Jonah G On BEST ANSWER

To answer my own question, it can be done by accessing each pixel on both Bitmaps. After that, I needed to give each Bitmap it's precentage of the final Bitmap, then I displayed the changes on a new Bitmap.

     public void blend(View v){
     operation= Bitmap.createBitmap(bmp.getWidth(),
     bmp.getHeight(),bmp.getConfig());

     for(int i=0; i<bmp.getWidth(); i++){
            for(int j=0; j<bmp.getHeight(); j++){
               int p = bmp.getPixel(i, j);
               int p2 = bmp2.getPixel(i, j);


               int r = Color.red(p);
               int g = Color.green(p);
               int b = Color.blue(p);

               int r2 = Color.red(p2);
               int g2 = Color.green(p2);
               int b2 = Color.blue(p2);

               r=(r+r2)/2;
               g=(g+g2)/2;
               b=(b+b2)/2;

               operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
            }
     }
     img.setImageBitmap(operation); 
}