无法从 Flex Air AS3 中删除的图像中获取位图数据

发布于 2024-10-29 09:36:01 字数 1450 浏览 6 评论 0原文

当我将图像拖放到画布上时,我可以获取图像的 nativePath,但不能获取我需要的位图数据。

在调试模式下,当我查看文件属性时,数据设置为 NULL。

我在这里做错了什么?在我的代码中 file.data 没有给我任何东西。

protected function creationCompleteHandler(event:FlexEvent):void
{
    this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,onDragIn);
    this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onDrop);
    NativeDragActions.COPY;
}

        private function onDrop(e:NativeDragEvent):void
        {
            trace("Dropped!");
            
            var dropfiles:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
            for each (var file:File in dropfiles){
                switch (file.extension.toLowerCase()){
                    case "png" :
                        trace('png');
                        //resizeImage(file.nativePath);
                        break;
                    case "jpg" :
                        trace('jpg');
                        //resizeImage(file.nativePath);
                        break;
                    case "jpeg" :
                        trace('jpeg');
                        //resizeImage(file.nativePath);
                        break;
                    case "gif" :
                        resizeImage(file.nativePath);
                        break;
                    default:
                        Alert.show("choose an image file!");
                }
            }
        }

When I drop an image onto my canvas I can get the nativePath to the image but not the bitmapdata which is the one I need.

In debug mode when I look into the file properties the data is set to NULL.

What am I doing wrong here? In my code file.data doesn't give me anything.

protected function creationCompleteHandler(event:FlexEvent):void
{
    this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,onDragIn);
    this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onDrop);
    NativeDragActions.COPY;
}

        private function onDrop(e:NativeDragEvent):void
        {
            trace("Dropped!");
            
            var dropfiles:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
            for each (var file:File in dropfiles){
                switch (file.extension.toLowerCase()){
                    case "png" :
                        trace('png');
                        //resizeImage(file.nativePath);
                        break;
                    case "jpg" :
                        trace('jpg');
                        //resizeImage(file.nativePath);
                        break;
                    case "jpeg" :
                        trace('jpeg');
                        //resizeImage(file.nativePath);
                        break;
                    case "gif" :
                        resizeImage(file.nativePath);
                        break;
                    default:
                        Alert.show("choose an image file!");
                }
            }
        }

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

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

发布评论

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

评论(1

A君 2024-11-05 09:36:01

首先,您必须加载字节数组:

var ba:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(ba);
stream.close();

接下来,使用以下方式加载位图:

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fileLoaded);
loader.loadBytes(ba);

最后,获取位图

private function fileLoaded(event:Event):void {
    var bitmap:Bitmap = Bitmap(event.target.content);

    // finally you have:   bitmap.bitmapData

    // cleanup
    loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, fileLoaded);
}

first, you have to load the bytearray:

var ba:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(ba);
stream.close();

next, load the bitmap using:

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fileLoaded);
loader.loadBytes(ba);

finally, get the bitmap

private function fileLoaded(event:Event):void {
    var bitmap:Bitmap = Bitmap(event.target.content);

    // finally you have:   bitmap.bitmapData

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