【问题标题】:How to cut a Shape out of BitmapData?如何从 BitmapData 中切出一个形状?
【发布时间】:2011-05-20 15:41:34
【问题描述】:

我有一个填充 Shape,以及一个与 Shape 的边界框宽度和高度相同的 BitmapData。
我需要从 BitmapData 中剪切形状(基本上将 BitmapData 绘制到形状上......)[像这样:http://imgur.com/uwE5F.png]

我使用了相当老套的方法:

        public static function cutPoly(img:BitmapData, s:Shape, bounds:Bounds):BitmapData {
        var temp:BitmapData = new BitmapData(bounds.width, bounds.height, true);
        Main.inst.stageQuality("low"); //hack to kill anti-aliasing
        temp.draw(s,new Matrix());
        Main.inst.stageQuality("high"); // end hack

        //0xFF00FF00 is the color of the shape
        makeColTrans(temp,0xFF00FF00); //makes the color transparent :P
        //return temp;
        img.draw(temp);
        //img.draw(temp);
        temp.dispose();
        makeColTrans(img, 0xFFFFFFFF);
        return img;
    }

我想知道是否有更好的方法...不仅仅是一种 hack。

【问题讨论】:

    标签: flash actionscript-3 actionscript bitmap bitmapdata


    【解决方案1】:

    它也可以被认为是 hack,但您可以在容器精灵中添加位图和(绘制的)形状,用形状遮盖位图并再次绘制结果图像。您获得的唯一好处是使用运行时的本机绘图算法,并且只有当您的 makeColTrans 逐像素扫描整个位图时才会出现这种情况。

    为代码示例编辑:

        public static function cutPoly(sourceBitmapData:BitmapData, maskShape:Shape, bounds:Rectangle):BitmapData {
            // you might not need this, supplying just the sourceBitmap to finalBitmapData.draw(), it should be tested though.
            var sourceBitmapContainer:Sprite = new Sprite();
            sourceBitmapContainer.addChild(sourceBitmap);
            sourceBitmapContainer.addChild(maskShape);
    
            var sourceBitmap:Bitmap = new Bitmap(sourceBitmapData);
            maskShape.x = bounds.x;
            maskShape.y = bounds.y;
            sourceBitmap.mask = maskShape;
    
            var finalBitmapData:BitmapData = new BitmapData(bounds.width, bounds.height, true, 0x00ffffff);
            // or var finalBitmapData:Bitmap = new BitmapData(maskShape.width, maskShape.height); not too sure about the contents of the bounds...
            finalBitmapData.draw(sourceBitmapContainer);
    
            return finalBitmapData;
        }
    

    【讨论】:

    • 不,makeColTrans 使用 BitmapData.threshold :P。我不完全明白你的意思……你能给出一小段代码吗?
    • 抱歉回复晚了,请查看上面的示例代码。尽管没有针对此特定代码进行测试,但我使用了类似的方法从图像中分割出拼图的各个部分。它只完成了一次,大约一千块的速度相当快。
    【解决方案2】:

    draw() 方法的第二个参数采用一个变换矩阵 - 您可以在此处指定偏移、旋转、倾斜等。然后使用该位图数据对象作为形状上的 beginBitmapFill 源。

    【讨论】:

      【解决方案3】:

      形状周围的颜色是否一致?如果是这样,您可以找到该彩色像素的第一个实例,并从该坐标中对 0x00000000 的位图数据进行 FloodFill(),然后将结果绘制到一个新的透明位图数据中。

      编辑

      这是另一个使用阈值的示例。它远非完美,但它确实让你成为了其中的一部分。在这种情况下,“Tile”类只是您在问题中提供的带有链接 ID 的图像。

      import flash.display.BitmapData;
      import flash.geom.Point;
      import flash.display.Bitmap;
      
      var sample : Tile = new Tile();
      
      var alphaBitmap : BitmapData = new BitmapData ( sample.width, sample.height, true, 0x00000000 );
          alphaBitmap .draw ( sample );
          alphaBitmap.threshold( alphaBitmap, alphaBitmap.rect, new Point(), "<", 0xFFEFEFEF, 0xFFFFFFFF, 0xFFFFFFFF );
          alphaBitmap.threshold( alphaBitmap, alphaBitmap.rect, new Point(), "!=", 0xFFFFFFFF, 0x00000000 );
      
          addChild ( new Bitmap ( alphaBitmap ) );
      
      var source : Tile = new Tile();
      var output : BitmapData = new BitmapData ( sample.width, sample.height, true, 0x00000000 );
          output.copyPixels( source, source.rect, new Point(), alphaBitmap );
      
      var outputBitmap : Bitmap = new Bitmap ( output );
          outputBitmap.x = outputBitmap.width;
      
      addChild ( outputBitmap );
      

      【讨论】:

        【解决方案4】:

        如果您希望主要使用 BitmapData 对象执行这种“cookie-cut”算法,请考虑:

        var bmp:BitmapData = pSourceBmp.clone();
        var alphaBmp:BitmapData = new BitmapData(shape.width, shape.height, true, 0);
        
        alphaBmp.draw( shape );
        
        var alphaOrigin:Point = new Point();
        var pasteOrigin:Point = new Point();
        
        bmp.copyPixels( bmp, bmp.rect, pasteOrigin, alphaBmp, alphaOrigin, true);
        

        注意:此代码未经测试。

        我在 Blitting 引擎中使用了类似的东西来“切饼干”,它可以很好地从任何给定的矢量图形中提取形状。面具的有趣替代品。

        【讨论】:

          猜你喜欢
          • 2014-02-24
          • 2014-02-21
          • 2010-12-31
          • 2012-05-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多