Adnroid上的简单图像合成类——PorterDuffXfermode

来源:互联网 发布:excel剔除异常数据 编辑:程序博客网 时间:2024/06/05 23:52

原:http://blog.csdn.net/panda1234lee/article/details/8722386?utm_source=tuicool


图像合成,是将两幅退昂放在一起的动作,它使得我们能够同时看到两幅图像的特征。

我们可以首先在Canvas对象上绘制一个位图对象,然后再相同的Canvas对象上绘制第二个位图对象的方式来实现合成。不过这里在绘制第二幅图像的时候,需要在Paint对象上指定一个过渡模式(Xfermode)。

可用作过渡模式的类集合都继承自Xfermode基类,而其中包括一个成为PorterDuffXfermode的类。PorterDuffXfermode因Thomas Porter和Tom Duff而得名,他们于1984年在ACM SIGGRAPH计算机图形学出版物上发表了题为“Compositing digital images”(合成数字图像)的文章,详细介绍了一系列不同的规则,用于彼此重叠的绘制图像。

在Android的PorterDuff.Mode类中列举了他们制定的规则:

android.graphics.PorterDuff.Mode.SRC:只绘制源图像

android.graphics.PorterDuff.Mode.DST:只绘制目标图像

android.graphics.PorterDuff.Mode.DST_OVER:在源图像的顶部绘制目标图像

android.graphics.PorterDuff.Mode.DST_IN:只在源图像和目标图像相交的地方绘制目标图像

android.graphics.PorterDuff.Mode.DST_OUT:只在源图像和目标图像不相交的地方绘制目标图像

android.graphics.PorterDuff.Mode.DST_ATOP:在源图像和目标图像相交的地方绘制目标图像,在不相交的地方绘制源图像

android.graphics.PorterDuff.Mode.SRC_OVER:在目标图像的顶部绘制源图像

android.graphics.PorterDuff.Mode.SRC_IN:只在源图像和目标图像相交的地方绘制源图像

android.graphics.PorterDuff.Mode.SRC_OUT:只在源图像和目标图像不相交的地方绘制源图像

android.graphics.PorterDuff.Mode.SRC_ATOP:在源图像和目标图像相交的地方绘制源图像,在不相交的地方绘制目标图像

android.graphics.PorterDuff.Mode.XOR:在源图像和目标图像重叠之外的任何地方绘制他们,而在不重叠的地方不绘制任何内容

android.graphics.PorterDuff.Mode.LIGHTEN:获得每个位置上两幅图像中最亮的像素并显示

android.graphics.PorterDuff.Mode.DARKEN:获得每个位置上两幅图像中最暗的像素并显示

android.graphics.PorterDuff.Mode.MULTIPLY:将每个位置的两个像素相乘,除以255,然后使用该值创建一个新的像素进行显示。结果颜色=顶部颜色*底部颜色/255

android.graphics.PorterDuff.Mode.SCREEN:反转每个颜色,执行相同的操作(将他们相乘并除以255),然后再次反转。结果颜色=255-(((255-顶部颜色)*(255-底部颜色))/255)

以下是使用的范例源码:

[java] view plaincopy
  1. public class MainActivity extends Activity implements OnClickListener  
  2. {  
  3.     static final int PICKED_ONE = 0;  
  4.     static final int PICKED_TWO = 1;  
  5.     boolean onePicked = false;  
  6.     boolean twoPicked = false;  
  7.     ImageView compositeImageView;  
  8.     Button choosePicture1, choosePicture2;  
  9.     Bitmap bmp1, bmp2;  
  10.     Canvas canvas;  
  11.     Paint paint;  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState)  
  15.     {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.         compositeImageView = (ImageView) findViewById(R.id.ChosenImageView);  
  19.         choosePicture1 = (Button) findViewById(R.id.ChoosePictureButton1);  
  20.         choosePicture2 = (Button) findViewById(R.id.ChoosePictureButton2);  
  21.         choosePicture1.setOnClickListener(this);  
  22.         choosePicture2.setOnClickListener(this);  
  23.     }  
  24.   
  25.     @Override  
  26.     public void onClick(View v)  
  27.     {  
  28.         // TODO Auto-generated method stub  
  29.         int which = -1;  
  30.         if (v == choosePicture1)  
  31.         {  
  32.             which = PICKED_ONE;  
  33.         } else  
  34.         {  
  35.             which = PICKED_TWO;  
  36.         }  
  37.         Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,  
  38.                 Media.EXTERNAL_CONTENT_URI);  
  39.         startActivityForResult(choosePictureIntent, which);  
  40.     }  
  41.   
  42.     private Bitmap loadBitmap(Uri imageFileUri)  
  43.     {  
  44.         Display currentDisplay = getWindowManager().getDefaultDisplay();  
  45.         int dw = currentDisplay.getWidth();  
  46.         int dh = currentDisplay.getHeight();  
  47.         Bitmap returnBmp = Bitmap.createBitmap(dw, dh, Config.ARGB_4444);  
  48.   
  49.         BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();  
  50.         bmpFactoryOptions.inJustDecodeBounds = true;  
  51.         try  
  52.         {  
  53.             returnBmp = BitmapFactory.decodeStream(getContentResolver()  
  54.                     .openInputStream(imageFileUri), null, bmpFactoryOptions);  
  55.         } catch (FileNotFoundException e)  
  56.         {  
  57.             // TODO Auto-generated catch block  
  58.             e.printStackTrace();  
  59.         }  
  60.   
  61.         int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight  
  62.                 / (float) dh);  
  63.         int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth  
  64.                 / (float) dw);  
  65.         if (heightRatio > 1 && widthRatio > 1)  
  66.         {  
  67.             if (heightRatio > widthRatio)  
  68.             {  
  69.                 bmpFactoryOptions.inSampleSize = heightRatio;  
  70.             } else  
  71.             {  
  72.                 bmpFactoryOptions.inSampleSize = widthRatio;  
  73.             }  
  74.         }  
  75.         bmpFactoryOptions.inJustDecodeBounds = false;  
  76.         try  
  77.         {  
  78.             returnBmp = BitmapFactory.decodeStream(getContentResolver()  
  79.                     .openInputStream(imageFileUri), null, bmpFactoryOptions);  
  80.         } catch (FileNotFoundException e)  
  81.         {  
  82.             // TODO Auto-generated catch block  
  83.             e.printStackTrace();  
  84.         }  
  85.         return returnBmp;  
  86.     }  
  87.   
  88.     @Override  
  89.     protected void onActivityResult(int requestCode, int resultCode, Intent data)  
  90.     {  
  91.         // TODO Auto-generated method stub  
  92.         super.onActivityResult(requestCode, resultCode, data);  
  93.         if (resultCode == RESULT_OK)  
  94.         {  
  95.             Uri imageFileUri = data.getData();  
  96.   
  97.             if (requestCode == PICKED_ONE)  
  98.             {  
  99.                 bmp1 = loadBitmap(imageFileUri);  
  100.                 onePicked = true;  
  101.             } else  
  102.             {  
  103.                 bmp2 = loadBitmap(imageFileUri);  
  104.                 twoPicked = true;  
  105.             }  
  106.   
  107.             if (onePicked && twoPicked)  
  108.             {  
  109.                 Bitmap drawingBitmap = Bitmap.createBitmap(bmp1.getWidth(),  
  110.                         bmp1.getHeight(), bmp1.getConfig());  
  111.                 canvas = new Canvas(drawingBitmap);  
  112.                 paint = new Paint();  
  113.                 canvas.drawBitmap(bmp1, 00, paint);  
  114.                 paint.setXfermode(new PorterDuffXfermode(  
  115.                         android.graphics.PorterDuff.Mode.DARKEN));  
  116.                 canvas.drawBitmap(bmp2, 00, paint);  
  117.                 compositeImageView.setImageBitmap(drawingBitmap);  
  118.             }  
  119.         }  
  120.     }  
  121.   
  122. }  
0 0
原创粉丝点击