将图像承诺转换为byteArray AS3

时间:2014-05-05 23:20:55

标签: php image actionscript-3 upload air

我正在尝试做一个拍照的航空应用程序,而不是将其发送到后期网络中的网络,为此我需要将imagePromise转换为字节数组文件...有谁知道如何做到这一点?或者有谁知道更好的解决方案? 谢谢

1 个答案:

答案 0 :(得分:2)

**媒体**承诺 - 处理在iOS上略有不同Android系统。 (很遗憾,你没有说明,你所针对的是哪个平台)

在最简单的情况下,它将在mediaPromise.file属性中返回一个文件。你可以使用Loader或FileStream加载它,无论你最喜欢哪个。

收听MediaEvent。当它触发时,您将获得一个文件,或者您必须首先使用Loader加载mediaPromise

试试这个:(代码不是错误检查也不是完整但是应该给你一个想法:

private function onPictureTaken(e:MediaEvent):void
{
    // first we have to find out, if file is already saved (as on Android or BB TabletOS )
    // or has to be loaded first (like on iOS)
    var mediaPromise:MediaPromise = e.data;

    if(mediaPromise.file == null) // is iOS
    {
        trace("iOS Device found");
        // it would be a good idea to give the anonymous bitmap a name:
        var date:String = DateUtils.dateToString( new Date ,"yyyy-MM-dd_HH-mm-ss"); 
        _fileName = "Image_"  +date + "." + saveMode; // add right extension

        _loader = new Loader();
        _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onIOSImageLoadComplete);
        _loader.loadFilePromise(mediaPromise);

    }  
    else // anything else
    {
            trace("non-iOS Device found");

            file = mediaPromise.file;
           _fileName = mediaPromise.file.name;
           // you can directly access files bytes here:
            var fs:FileStream = new FileStream;
            var bytes:ByteArray = new ByteArray;
            fs.open(file , FileMode.READ);
            fs.readBytes(bytes);
            fs.close();
           // you now have your byteArray

   }
}
private function onIOSImageLoadComplete(e:Event):void
{
    // iOS handles over pictures as Bitmap, so it should to be rencoded to jpeg or png prior to uploading
    // take a look at JPEGEncoder, for example.
    // it will return a byteArray also, which you can use to upload;
    var bmp:Bitmap = _loader.content as Bitmap;
    var bytes:ByteArray = new JPEGEncoder(50).encode(bmp.bitmapData);
}