0

我想在android中对两个图像进行异或,因为我正在开发图像加密应用程序,我从SD卡中获取图像并将它们加载到图像视图中,因为我已经加载了两个我想对它们进行异或的图像

4

2 回答 2

3

另一种选择是在Canvas你的两个位图上绘制。一个位图没有指定任何设置,但另一个应该在他的对象中指定一个PorterDuffXfermodeto 。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);
于 2012-08-02T12:23:11.070 回答
1

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

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

于 2012-08-01T19:10:36.073 回答