2

我正在 Flash Builder 中创建一个 iOS 和 Android 应用程序,该应用程序使用 JPGEncoder 函数将拍摄的图像从设备转换为字节数组,但在 iOS 中我的应用程序冻结,在 Android 中屏幕变黑一分钟左右然后继续。下面是我正在使用的代码。

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();
            }
        }
4

1 回答 1

1

这是因为编码器不是异步的,编码大图像可能需要一段时间;尤其是 jpeg,尤其是在移动设备上。

那里有异步编码器,但不允许自我推广。提示提示。

编辑:该死的规则!这是我关于异步编码的博客文章(和代码)的链接:http: //blog.leeburrows.com/2011/01/saving-an-image-to-hd-with-actionscript-part-2/

于 2013-01-19T16:30:11.750 回答