简体   繁体   English

如何异或 Flutter 中的两个图像

[英]How to XOR two images in Flutter

I have these two images我有这两张图片

图像A

图像B

What I want to do is to xor them to get this image:我想要做的是对它们进行异或以获得此图像:

原来的

I have tried to do it using CustomPaint and setting BlendMode to XOR But it gives me a black screen我曾尝试使用CustomPaint并将BlendMode设置为XOR但它给了我一个黑屏

Here's the code I used:这是我使用的代码:

class XorPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) async {
    if (imageA != null && imageB != null) {
      canvas.drawImage(imageA, Offset.zero, Paint());
      canvas.save();
      canvas.drawImage(
          imageB, Offset.zero, Paint()..blendMode = BlendMode.xor);
      canvas.restore();
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }

For more information: see this有关更多信息: 请参阅此

I believe its more of aa misconception of the BlendMode.xor , what they do is just like a regular xor truth table where every pixel is either 0 (transparent) if both of them are some color or unaffected where both overlap if one of them is transparent in that same area, because of that you will always get a transparent black square (unless for example you make the apple image with a transparent background, in which case the second image will have an empty space with the shape of the apple in the middle).我相信它更多的是对BlendMode.xor的误解,它们所做的就像一个常规的异或真值表,如果它们都是某种颜色,每个像素要么是 0(透明),要么如果它们中的一个是重叠则不受影响在同一区域透明,因此您将始终得到一个透明的黑色正方形(除非例如您制作具有透明背景的苹果图像,在这种情况下,第二个图像将有一个空白区域,其中苹果的形状中间)。 I think what you really wanted was difference or exlusion to have that negated effect我认为你真正想要的是差异或排斥来产生这种否定的效果

class XorPainter extends CustomPainter {
  final ui.Image imageA;
  final ui.Image imageB;
  
  XorPainter(this.imageA, this.imageB);
  
  @override
  void paint(Canvas canvas, Size size) async {
    canvas.drawImage(imageA, Offset.zero, Paint());
    canvas.saveLayer(null, Paint()..blendMode = BlendMode.difference); // or BlendMode.exclusion
    canvas.drawImage(imageB, Offset.zero, Paint());
    canvas.restore();
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM