如何在 ActionScript 3.0 中用另一个图像遮盖图像

发布于 2024-09-03 22:03:26 字数 150 浏览 10 评论 0原文

我的问题是这样的,我的用户导入图像 FileReference 和我需要屏蔽它然后将其发送到服务器。

我的问题是这样的:我能够保留文件引用事件并且 将图像数据传输到我的画布中。我能够发送到服务器 屏蔽的结果。

但我无法掩盖用户加载的图像 我的画布。

my issue is this, my users import an image with
FileReference and I need to mask it and then send it to server.

My problem is this: I'm be able do keep the filereference event and
transfer the image data into my canvas. I'm be able to send to server
the result of masking.

But I'm NOT be able to mask the image that my users have load in
my canvas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨凡馨 2024-09-10 22:03:27

这改变了情况,但变化不大。查看修改后的解决方案:

package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.FileFilter;
import flash.net.FileReference;

public class Test extends Sprite {

    private var _fileReference:FileReference;
    private var _fileFilter:FileFilter;
    private var _imageLoader:Loader;
    private var _maskLoader:Loader;
    private var _canvas:Sprite;

    private var _imageLoaded:Boolean = false;

    public function Test() {
        addEventListener(Event.ADDED_TO_STAGE, addedToStageListener, false, 0, true);
    }

    private function addedToStageListener(event:Event):void {
        removeEventListener(Event.ADDED_TO_STAGE, addedToStageListener);

        _fileReference = new FileReference();
        _fileReference.addEventListener(Event.SELECT, fileSelectedListener, false, 0, true);
        _fileReference.addEventListener(Event.COMPLETE, fileLoadCompleteListener, false, 0, true);

        _fileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");

        _imageLoader = new Loader();
        _imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoadCompleteListener, false, 0, true);
        _maskLoader = new Loader();
        _maskLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, maskLoadCompleteListener, false, 0, true);

        _canvas = new Sprite();
        addChild(_canvas);

        stage.addEventListener(MouseEvent.CLICK, mouseClickListener, false ,0, true);
    }

    private function mouseClickListener(event:Event):void {
        _fileReference.browse([_fileFilter]);
    }

    private function fileSelectedListener(event:Event):void {
        _fileReference.load();
    }

    private function fileLoadCompleteListener(event:Event):void {
        if (!_imageLoaded) {
            _imageLoader.loadBytes(event.target.data);
        } else {
            _maskLoader.loadBytes(event.target.data);
        }
    }

    private function imageLoadCompleteListener(event:Event):void {
        _imageLoaded = true;
    }

    private function maskLoadCompleteListener(event:Event):void {
        var imageBitmap:Bitmap = _imageLoader.content as Bitmap;
        var maskBitmap:Bitmap = _maskLoader.content as Bitmap;

        if (imageBitmap && maskBitmap) {
            var imageBitmapData:BitmapData = imageBitmap.bitmapData;
            var maskBitmapData:BitmapData = maskBitmap.bitmapData;

            var boundRectange:Rectangle = new Rectangle(0, 0, maskBitmap.width, maskBitmap.height);
            var destinationPoint:Point = new Point(0, 0);

            var finalBitmapData:BitmapData = new BitmapData(maskBitmap.width, maskBitmap.height);
            finalBitmapData.copyPixels(imageBitmapData, boundRectange, destinationPoint, maskBitmapData, destinationPoint);
            var finalBitmap:Bitmap = new Bitmap(finalBitmapData);

            while(_canvas.numChildren) {
                _canvas.removeChildAt(0);
            }
            _canvas.addChild(finalBitmap);
        }
    }
}}

首先单击舞台允许您选择蒙版图像。第二次单击允许您选择包含遮罩信息的图像(例如具有透明度的 png 文件)。希望这有帮助。

This changes the situation, but not dramatically. Check out the modified solution:

package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.FileFilter;
import flash.net.FileReference;

public class Test extends Sprite {

    private var _fileReference:FileReference;
    private var _fileFilter:FileFilter;
    private var _imageLoader:Loader;
    private var _maskLoader:Loader;
    private var _canvas:Sprite;

    private var _imageLoaded:Boolean = false;

    public function Test() {
        addEventListener(Event.ADDED_TO_STAGE, addedToStageListener, false, 0, true);
    }

    private function addedToStageListener(event:Event):void {
        removeEventListener(Event.ADDED_TO_STAGE, addedToStageListener);

        _fileReference = new FileReference();
        _fileReference.addEventListener(Event.SELECT, fileSelectedListener, false, 0, true);
        _fileReference.addEventListener(Event.COMPLETE, fileLoadCompleteListener, false, 0, true);

        _fileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");

        _imageLoader = new Loader();
        _imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoadCompleteListener, false, 0, true);
        _maskLoader = new Loader();
        _maskLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, maskLoadCompleteListener, false, 0, true);

        _canvas = new Sprite();
        addChild(_canvas);

        stage.addEventListener(MouseEvent.CLICK, mouseClickListener, false ,0, true);
    }

    private function mouseClickListener(event:Event):void {
        _fileReference.browse([_fileFilter]);
    }

    private function fileSelectedListener(event:Event):void {
        _fileReference.load();
    }

    private function fileLoadCompleteListener(event:Event):void {
        if (!_imageLoaded) {
            _imageLoader.loadBytes(event.target.data);
        } else {
            _maskLoader.loadBytes(event.target.data);
        }
    }

    private function imageLoadCompleteListener(event:Event):void {
        _imageLoaded = true;
    }

    private function maskLoadCompleteListener(event:Event):void {
        var imageBitmap:Bitmap = _imageLoader.content as Bitmap;
        var maskBitmap:Bitmap = _maskLoader.content as Bitmap;

        if (imageBitmap && maskBitmap) {
            var imageBitmapData:BitmapData = imageBitmap.bitmapData;
            var maskBitmapData:BitmapData = maskBitmap.bitmapData;

            var boundRectange:Rectangle = new Rectangle(0, 0, maskBitmap.width, maskBitmap.height);
            var destinationPoint:Point = new Point(0, 0);

            var finalBitmapData:BitmapData = new BitmapData(maskBitmap.width, maskBitmap.height);
            finalBitmapData.copyPixels(imageBitmapData, boundRectange, destinationPoint, maskBitmapData, destinationPoint);
            var finalBitmap:Bitmap = new Bitmap(finalBitmapData);

            while(_canvas.numChildren) {
                _canvas.removeChildAt(0);
            }
            _canvas.addChild(finalBitmap);
        }
    }
}}

First click on the stage allows you to select masked image. Second click allows you to select image, which contains the masking information (e.g. png file with transparency). Hope this helps.

野侃 2024-09-10 22:03:26

您只需将加载的位图添加到可屏蔽容器(例如 Sprite)。例如:
测试程序

package {
import flash.display.Graphics;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.FileFilter;
import flash.net.FileReference;

public class Test extends Sprite {

    private var _fileReference:FileReference;
    private var _fileFilter:FileFilter;
    private var _loader:Loader;
    private var _imageContainer:Sprite;
    private var _mask:Sprite;
    private var _canvas:Sprite;

    public function Test() {
        addEventListener(Event.ADDED_TO_STAGE, addedToStageListener, false, 0, true);
    }

    private function addedToStageListener(event:Event):void {
        removeEventListener(Event.ADDED_TO_STAGE, addedToStageListener);

        _fileReference = new FileReference();
        _fileReference.addEventListener(Event.SELECT, fileSelectedListener, false, 0, true);
        _fileReference.addEventListener(Event.COMPLETE, fileLoadCompleteListener, false, 0, true);

        _fileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");

        _loader = new Loader();
        _canvas = new Sprite();

        _mask = new Sprite();
        var maskGraphics:Graphics = _mask.graphics;
        maskGraphics.beginFill(0xFFFFFF);
        maskGraphics.drawCircle(50, 50, 50);
        maskGraphics.endFill();

        _imageContainer = new Sprite();
        _imageContainer.mask = _mask;

        _canvas.addChild(_imageContainer);
        _canvas.addChild(_mask);

        addChild(_canvas);

        stage.addEventListener(MouseEvent.CLICK, mouseClickListener, false ,0, true);
    }

    private function mouseClickListener(event:Event):void {
        _fileReference.browse([_fileFilter]);
    }

    private function fileSelectedListener(event:Event):void {
        _fileReference.load();
    }

    private function fileLoadCompleteListener(event:Event):void {
        _loader.loadBytes(event.target.data);

        while(_imageContainer.numChildren) {
            _imageContainer.removeChildAt(0);
        }
        _imageContainer.addChild(_loader);
    }
}}

You just need to add loaded bitmap to maskable container (e.g. Sprite). For exmaple:
Test.as

package {
import flash.display.Graphics;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.FileFilter;
import flash.net.FileReference;

public class Test extends Sprite {

    private var _fileReference:FileReference;
    private var _fileFilter:FileFilter;
    private var _loader:Loader;
    private var _imageContainer:Sprite;
    private var _mask:Sprite;
    private var _canvas:Sprite;

    public function Test() {
        addEventListener(Event.ADDED_TO_STAGE, addedToStageListener, false, 0, true);
    }

    private function addedToStageListener(event:Event):void {
        removeEventListener(Event.ADDED_TO_STAGE, addedToStageListener);

        _fileReference = new FileReference();
        _fileReference.addEventListener(Event.SELECT, fileSelectedListener, false, 0, true);
        _fileReference.addEventListener(Event.COMPLETE, fileLoadCompleteListener, false, 0, true);

        _fileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");

        _loader = new Loader();
        _canvas = new Sprite();

        _mask = new Sprite();
        var maskGraphics:Graphics = _mask.graphics;
        maskGraphics.beginFill(0xFFFFFF);
        maskGraphics.drawCircle(50, 50, 50);
        maskGraphics.endFill();

        _imageContainer = new Sprite();
        _imageContainer.mask = _mask;

        _canvas.addChild(_imageContainer);
        _canvas.addChild(_mask);

        addChild(_canvas);

        stage.addEventListener(MouseEvent.CLICK, mouseClickListener, false ,0, true);
    }

    private function mouseClickListener(event:Event):void {
        _fileReference.browse([_fileFilter]);
    }

    private function fileSelectedListener(event:Event):void {
        _fileReference.load();
    }

    private function fileLoadCompleteListener(event:Event):void {
        _loader.loadBytes(event.target.data);

        while(_imageContainer.numChildren) {
            _imageContainer.removeChildAt(0);
        }
        _imageContainer.addChild(_loader);
    }
}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文