如何在Android中XOR两个图像?

时间:2012-08-01 18:46:15

标签: android android-layout android-intent xor

我想在Android中对两个图像进行异或,因为iam正在处理图像加密应用程序iam带来SD卡中的图像并将它们加载到图像视图中,因为我已经加载了两个我想要对它们进行异或的图像

2 个答案:

答案 0 :(得分:3)

另一种选择是在Canvas两个位图上绘制。一个位图未指定任何设置,但另一个位图应在其PorterDuffXfermode对象中指定Mode.XORPaint

前:

ImageView compositeImageView = (ImageView) findViewById(R.id.imageView);

Bitmap bitmap1=BitmapFactory.decodeResource(getResources(), R.drawable.batman_ad);
Bitmap bitmap2=BitmapFactory.decodeResource(getResources(), R.drawable.logo);

Bitmap resultingImage=Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), bitmap1.getConfig());

Canvas canvas = new Canvas(resultingImage);

// Drawing first image on Canvas
Paint paint = new Paint();
canvas.drawBitmap(bitmap1, 0, 0, paint);

// Drawing second image on the Canvas, with Xfermode set to XOR
paint.setXfermode(new PorterDuffXfermode(Mode.XOR));
canvas.drawBitmap(bitmap2, 0, 0, paint);

compositeImageView.setImageBitmap(resultingImage);

答案 1 :(得分:1)

这取决于你想要的xor,像素或数据本身。 任何方式一种简单的方法是将图像转换为所有像素的数组,将它们混合在一起,然后将其转换回位图。请注意,此示例仅适用于具有相同分辨率的位图。

//convert the first bitmap to array of ints
int[] buffer1 = new int[bmp1.getWidth()*bmp1.getHeight()];
bmp1.getPixels(buffer1,0,bmp1.getWidth(),0,0,bmp1.getWidth(),bmp1.getHeight() );

//convert the seconds bitmap to array of ints
int[] buffer2 = new int[bmp2.getWidth()*bmp2.getHeight()];
bmp2.getPixels(buffer2,0,bmp2.getWidth(),0,0,bmp2.getWidth(),bmp2.getHeight() );

//XOR all the elements
for( int i = 0 ; i < bmp1.getWidth()*bmp1.getHeight() ; i++ )
    buffer1[i] = buffer1[i] ^ buffer2[i];

//convert it back to a bitmap, you could also create a new bitmap in case you need them
//for some thing else
bmp1.setPixels(buffer1,0,bmp1.getWidth(),0,0,bmp2.getWidth(),bmp2.getHeight() );

请参阅: http://developer.android.com/reference/android/graphics/Bitmap.html