1. Allow user to browse for image on their local system
2. Convert file reference into BitmapData
3. Scale BitmapData to fit inside a MAX resolution
4. Compress and encode the BitmapData into a ByteArray
5. Send ByteArray to server-side script to create and save the image file
After finding several articles covering pieces of these steps, I was able to stitch together the code to achieve the above goal. To use my code examples, you will need to use Flex 3 targeted for Flash Player 10 and Zend AMF to send the image data to your server.
<?xml version=“1.0″ encoding=“utf-8″?>
<mx:Application xmlns:mx=“http://www.adobe.com/2006/mxml” layout=“absolute”>
<mx:Script>
<![CDATA[
import mx.graphics.codec.JPEGEncoder;
private var _fileRef:FileReference;
private var _netConnection:NetConnection;
private var _imageByteArray:ByteArray;
private static const MAX_SIZE:Number = 300;
private function getFile():void{
_fileRef = new FileReference();
_fileRef.addEventListener(Event.SELECT,onFileSelect);
_fileRef.browse();
}
private function onFileSelect(e:Event):void{
_fileRef.addEventListener(Event.COMPLETE,onFileLoad);
_fileRef.load();
}
private function onFileLoad(e:Event):void{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
loader.loadBytes (_fileRef.data);
}
private function onImageLoaded(e:Event):void{
var loadedBitmap:Bitmap = Bitmap(e.target.content);
var scale:Number = getScale(loadedBitmap);
var bmpData:BitmapData = new BitmapData(loadedBitmap.width * scale, loadedBitmap.height * scale,false);
var matrix:Matrix = new Matrix();
matrix.scale(scale,scale);
bmpData.draw(loadedBitmap,matrix)
var jpg:JPEGEncoder = new JPEGEncoder(60);
_imageByteArray = jpg.encode(bmpData);
uploadImage();
}
private function getScale(img:Bitmap):Number{
var scale:Number;
if(img.width > MAX_SIZE){
scale = MAX_SIZE / img.width;
}
else if(img.height > MAX_SIZE){
scale = MAX_SIZE / img.height;
}
else{
scale = 1;
}
return scale;
}
private function uploadImage():void{
_netConnection = new NetConnection();
_netConnection.connect(‘http://localhost:8888/_PHP/classes/’);
var res:Responder = new Responder(onDataResult, onDataError);
_netConnection.call(“KingHippo.saveImage”,res,_imageByteArray);
}
public function onDataResult(obj:Object):void{
trace(obj);/////FILE SAVED
}
public function onDataError(obj:Object):void{
trace(‘error’);///////OOPS
}
]]>
</mx:Script>
<mx:Button label=“browse” click=“getFile()” />
</mx:Application>
That pretty much wraps up the Flex code. I’ll explain the code referring to our original goal list.
1. Allow user to browse for image
Here we have a simple Button that calls a function that will instantiate a file reference object and use its browse() method to prompt the user with their local file browser.
2. Convert file reference into BitmapData
Upon file select, we will need to get this file loaded in to grab its BitmapData. This is where Flash Player 10 comes in. We now have some cool FileReference additions to play with and can now load in the file selected. First we call the load() method. When COMPLETE, we have access to the data of that file which means we can use a Loader to load the bytes of the data using the loadBytes() method from our Loader.
3. Scale BitmapData to fit inside a MAX resolution
OK, so now we have our image data and its time to do some BitmapData trickery. First we need to set the incoming data to a Bitmap object. Next we determine how much we need to scale down the image by using our MAX_SIZE variable. Here I’m using 300 so the width or height will not exceed that number. Once I determine the scale value (using the getScale function) , I can now create and manipulate my Bitmap Data. I create a new BitmapData object and set it to the appropriate size using my scale value against the image’s original size. Now I need to draw from the loaded image. It’s not enough to just draw it b/c it will just grab that rect from the original image data, essentially cropping it. We need to use Matrix to set the scale of the original data within our draw() method.
Now we have our image data and appropriate resolution.
4. Compress and encode BitmapData into a ByteArray
Next, using Flex’s JPEGEncoder, we can compress the data and encode it to a ByteArray. Simply create a new JPEGEncoder object and pass a quality value to its constructor. The values are 1 – 100. Here I’m using 60. Then we call the JPEGEncoder’s encode() method which will then give us our long-awaited ByteArray. Now we can pass it on to the server.
5. Send ByteArray to server-side script to create image file
Lastly, we need to send off the image data to the server. Here I’m using Zend AMF and sending the ByteArray to a PHP class.
Now we are done on the Flex end. The last thing we need to do is create the image file from the data sent to the server and save it.
PHP code below..
<?php
class KingHippo{
public function __construct(){
//////construct
}
public function saveImage($obj){
$fp = fopen(‘myNewImage.jpg’, ‘wb’);
fwrite( $fp, implode(”,$obj));
fclose( $fp );
return “FILE SAVED”;
}
}
<?
2. Convert file reference into BitmapData
3. Scale BitmapData to fit inside a MAX resolution
4. Compress and encode the BitmapData into a ByteArray
5. Send ByteArray to server-side script to create and save the image file
After finding several articles covering pieces of these steps, I was able to stitch together the code to achieve the above goal. To use my code examples, you will need to use Flex 3 targeted for Flash Player 10 and Zend AMF to send the image data to your server.
<?xml version=“1.0″ encoding=“utf-8″?>
<mx:Application xmlns:mx=“http://www.adobe.com/2006/mxml” layout=“absolute”>
<mx:Script>
<![CDATA[
import mx.graphics.codec.JPEGEncoder;
private var _fileRef:FileReference;
private var _netConnection:NetConnection;
private var _imageByteArray:ByteArray;
private static const MAX_SIZE:Number = 300;
private function getFile():void{
_fileRef = new FileReference();
_fileRef.addEventListener(Event.SELECT,onFileSelect);
_fileRef.browse();
}
private function onFileSelect(e:Event):void{
_fileRef.addEventListener(Event.COMPLETE,onFileLoad);
_fileRef.load();
}
private function onFileLoad(e:Event):void{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
loader.loadBytes (_fileRef.data);
}
private function onImageLoaded(e:Event):void{
var loadedBitmap:Bitmap = Bitmap(e.target.content);
var scale:Number = getScale(loadedBitmap);
var bmpData:BitmapData = new BitmapData(loadedBitmap.width * scale, loadedBitmap.height * scale,false);
var matrix:Matrix = new Matrix();
matrix.scale(scale,scale);
bmpData.draw(loadedBitmap,matrix)
var jpg:JPEGEncoder = new JPEGEncoder(60);
_imageByteArray = jpg.encode(bmpData);
uploadImage();
}
private function getScale(img:Bitmap):Number{
var scale:Number;
if(img.width > MAX_SIZE){
scale = MAX_SIZE / img.width;
}
else if(img.height > MAX_SIZE){
scale = MAX_SIZE / img.height;
}
else{
scale = 1;
}
return scale;
}
private function uploadImage():void{
_netConnection = new NetConnection();
_netConnection.connect(‘http://localhost:8888/_PHP/classes/’);
var res:Responder = new Responder(onDataResult, onDataError);
_netConnection.call(“KingHippo.saveImage”,res,_imageByteArray);
}
public function onDataResult(obj:Object):void{
trace(obj);/////FILE SAVED
}
public function onDataError(obj:Object):void{
trace(‘error’);///////OOPS
}
]]>
</mx:Script>
<mx:Button label=“browse” click=“getFile()” />
</mx:Application>
That pretty much wraps up the Flex code. I’ll explain the code referring to our original goal list.
1. Allow user to browse for image
Here we have a simple Button that calls a function that will instantiate a file reference object and use its browse() method to prompt the user with their local file browser.
2. Convert file reference into BitmapData
Upon file select, we will need to get this file loaded in to grab its BitmapData. This is where Flash Player 10 comes in. We now have some cool FileReference additions to play with and can now load in the file selected. First we call the load() method. When COMPLETE, we have access to the data of that file which means we can use a Loader to load the bytes of the data using the loadBytes() method from our Loader.
3. Scale BitmapData to fit inside a MAX resolution
OK, so now we have our image data and its time to do some BitmapData trickery. First we need to set the incoming data to a Bitmap object. Next we determine how much we need to scale down the image by using our MAX_SIZE variable. Here I’m using 300 so the width or height will not exceed that number. Once I determine the scale value (using the getScale function) , I can now create and manipulate my Bitmap Data. I create a new BitmapData object and set it to the appropriate size using my scale value against the image’s original size. Now I need to draw from the loaded image. It’s not enough to just draw it b/c it will just grab that rect from the original image data, essentially cropping it. We need to use Matrix to set the scale of the original data within our draw() method.
Now we have our image data and appropriate resolution.
4. Compress and encode BitmapData into a ByteArray
Next, using Flex’s JPEGEncoder, we can compress the data and encode it to a ByteArray. Simply create a new JPEGEncoder object and pass a quality value to its constructor. The values are 1 – 100. Here I’m using 60. Then we call the JPEGEncoder’s encode() method which will then give us our long-awaited ByteArray. Now we can pass it on to the server.
5. Send ByteArray to server-side script to create image file
Lastly, we need to send off the image data to the server. Here I’m using Zend AMF and sending the ByteArray to a PHP class.
Now we are done on the Flex end. The last thing we need to do is create the image file from the data sent to the server and save it.
PHP code below..
<?php
class KingHippo{
public function __construct(){
//////construct
}
public function saveImage($obj){
$fp = fopen(‘myNewImage.jpg’, ‘wb’);
fwrite( $fp, implode(”,$obj));
fclose( $fp );
return “FILE SAVED”;
}
}
<?
发表评论
-
消息拦截器(MessageInterceptors)
2010-11-02 16:20 1051public class DeleteItemIntercep ... -
arguments
2010-11-02 11:33 738arguments 对象 在将参数传递给某个函数时,可以使用 ... -
PropertyChangeEvent的应用
2010-11-02 11:14 1310private var object:Object = {}; ... -
js与flash的函数交互
2010-08-10 11:01 808flash使用的actionscript跟javascript ... -
as 传输游戏数据
2010-07-09 16:28 815AS3]利用ByteArray压缩游戏关卡数据 ByteArr ... -
Taobao Logo显示
2010-04-28 10:26 755今天看见淘宝的首页的logo居然是swf格式,然后觉得要是如果 ... -
cairngorm commands deal with lots of things
2010-03-30 14:16 679Yeah. As the best practice say ... -
关于安全认证的几点思路
2010-03-29 01:23 7101 php cookie , sig_str md5 (u ... -
Flex与后台交互的几种方法详解(FlashVars、 HTTPService、URLLoader、WebService)
2010-03-25 22:11 22111.FlashVars(单向传递) A.传递:在<em ... -
flash背景平铺
2010-03-25 00:58 1014其实有很多方法实现,这里就按播放器的版本从低到高的实现方法来说 ... -
Socket通信学习资料
2009-11-27 12:33 1146Socket通信学习资料 什么是Socket?Socket协 ... -
CallLater简单解释
2009-11-18 17:11 1859目前FlashPlayer的运行机理是单线程模式,所以,在每一 ... -
Tweener _bezier 使用
2009-11-17 15:31 1254在做camera曲线运动的时候,发现新版的Tweener对_b ...
相关推荐
在这个场景中,"CompressImages.rar"是一个压缩包,包含了用于在C#环境下使用VS2015进行图片压缩的相关资源。C#是一门强大的面向对象的编程语言,而Visual Studio 2015是微软提供的一个集成开发环境(IDE),支持...
咕-压缩图像使用compress-images包的插件入门该插件要求: 咕unt声~1.0.1 npm软件包如果您以前从未使用过 ,请务必查看《指南》,因为它说明了如何创建以及安装和使用Grunt插件。 熟悉该过程后,可以使用以下命令...
images-quickly-compress 利用canvas画布压缩图片(ES6语法) 安装: npm install images-quickly-compress --save 使用方式: import ImagesQuicklyCompress from "images-quickly-compress";//导出:压缩图片插件 //...
apache.commons.compress 第三方开源软件。能解压,压缩文件。里面包括commons-compress-1.9、commons-compress-1.2.1的版本。 当遇到这种错误,应该重点关注Caused by:后面的内容 Caused by:xxx Unsupported major....
赠送jar包:commons-compress-1.21.jar; 赠送原API文档:commons-compress-1.21-javadoc.jar; 赠送源代码:commons-compress-1.21-sources.jar; 赠送Maven依赖信息文件:commons-compress-1.21.pom; 包含翻译后...
赠送jar包:commons-compress-1.19.jar; 赠送原API文档:commons-compress-1.19-javadoc.jar; 赠送源代码:commons-compress-1.19-sources.jar; 赠送Maven依赖信息文件:commons-compress-1.19.pom; 包含翻译后...
`commons-compress.jar` 是一个Java库,由Apache软件基金会开发并维护,它提供了一种统一的接口来处理各种常见的压缩格式。这个库的核心在于它能够处理多种压缩和归档格式,如7z、ar、arj、bz2、cpio、dump、gz、jar...
KIC_Compress.KD
赠送jar包:commons-compress-1.4.1.jar; 赠送原API文档:commons-compress-1.4.1-javadoc.jar; 赠送源代码:commons-compress-1.4.1-sources.jar; 赠送Maven依赖信息文件:commons-compress-1.4.1.pom; 包含...
安装添加到您的Gemfile : gem 'jekyll-compress-images' 并在_config.yml : plugins : - jekyll - compress - images 在项目文件夹中运行bundle install组态如果要为图像设置其他路径,请打开_config.yml添加...
赠送jar包:commons-compress-1.20.jar; 赠送原API文档:commons-compress-1.20-javadoc.jar; 赠送源代码:commons-compress-1.20-sources.jar; 赠送Maven依赖信息文件:commons-compress-1.20.pom; 包含翻译后...
赠送jar包:commons-compress-1.20.jar; 赠送原API文档:commons-compress-1.20-javadoc.jar; 赠送源代码:commons-compress-1.20-sources.jar; 赠送Maven依赖信息文件:commons-compress-1.20.pom; 包含翻译后...
Apache的Compress包是Apache软件基金会的一个项目,主要目的是提供一套通用的压缩和解压缩工具。这个包包含了一系列用于处理各种常见压缩格式的类库,如ZIP、TAR、GZIP、BZ2等,方便Java开发者在他们的应用程序中...
《深入解析Common Compress压缩库》 在Java开发中,数据压缩是一项常用的技术,它可以有效减少文件存储空间,提高传输效率。Common Compress是Apache软件基金会提供的一个开源库,专门用于处理各种压缩格式,如ZIP...
赠送jar包:commons-compress-1.21.jar; 赠送原API文档:commons-compress-1.21-javadoc.jar; 赠送源代码:commons-compress-1.21-sources.jar; 赠送Maven依赖信息文件:commons-compress-1.21.pom; 包含翻译后...
《Apache Commons Compress 1.7 源代码解析》 Apache Commons Compress库是Java平台上用于处理各种压缩格式的工具集,它提供了对多种压缩算法的支持,包括但不限于ARJ、BZip2、CPIO、7-Zip、Gzip、LZMA、Pack200、...
在Perl中,`Compress-Zlib`模块为开发者提供了一个方便的接口,来实现ZLIB的压缩和解压缩功能。 ZLIB库本身是由Jean-loup Gailly和Mark Adler开发的,它是DEFLATE算法的一个实现,DEFLATE是一种结合了LZ77(一种...
`NativeScript-Compress-Images` 是一个专为 NativeScript 框架设计的插件,它允许开发者在原生 Android 和 iOS 应用中进行图像的压缩和上传操作。这个插件能够有效地减小图像文件大小,从而提高应用程序的性能,...
在前端开发中,为了提高页面加载速度、减小网络传输数据量,开发者常常会使用压缩技术来处理JavaScript代码,这就是JSCompress的概念。JSCompress通常包括两个主要方面:混淆(Obfuscation)和压缩(Minification)...
Commons Compress库是Apache软件基金会开发的一个开源项目,它的主要目标是提供一个统一的API来处理各种不同的压缩格式。在给定的"commons-compress-1.18-bin" jar包中,包含了这个库的可执行版本,适用于Java平台。...