質問

I'm creating a iOS and Android application in Flash Builder that uses the JPGEncoder function to convert the taken image from the device to a bytearray but in iOS my app freezes and in Android the screen goes black for a minute or so and then continues. Below is the code I am using.

private var uploadData:ByteArray;
        protected var camera:CameraUI;
        protected var loader:Loader;
        public var file:File;

        protected function takePhoto():void {
            if (CameraUI.isSupported){
                camera = new CameraUI();
                camera.addEventListener(MediaEvent.COMPLETE, savePhoto);
                camera.launch(MediaType.IMAGE);
            }
        }

        protected function savePhoto(e:MediaEvent):void 
        {
            uploadProgress.visible = true;
            var imagePromise:MediaPromise = e.data;
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onPhotoBitmapLoadComplete);
            loader.loadFilePromise(imagePromise);
        }

        protected function onPhotoBitmapLoadComplete(e:Event):void
        {
            var loaderInfo:LoaderInfo = e.target as LoaderInfo;
            if (CameraRoll.supportsAddBitmapData)
            {
                var bitmapData:BitmapData = new BitmapData(loaderInfo.width, loaderInfo.height);
                bitmapData.draw(loaderInfo.loader);
                file = File.applicationStorageDirectory.resolvePath("receipt" + new Date().time + ".jpg");
                var stream:FileStream = new FileStream()
                stream.open(file, FileMode.WRITE);
                var j:JPEGEncoder = new JPEGEncoder();
                var bytes:ByteArray = j.encode(bitmapData);
                stream.writeBytes(bytes, 0, bytes.bytesAvailable);
                stream.close();

                uploadData = bytes;
                SetupWebService();
            }
        }
役に立ちましたか?

解決

Its because the Encoder is not asynchonous and it can take a while to encode a large image; especially jpegs and especially on mobile.

There are asynchronous encoders out there but self promotion is not allowed. hint, hint.

edit: rules be damned! heres a link to my blog post (and code) on async encoding: http://blog.leeburrows.com/2011/01/saving-an-image-to-hd-with-actionscript-part-2/

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top