Flash As3在线图片处理再上传
Flash player 10 终于支持图片客户端处理和预览的功能了, 大大节约了网站上传文件/图片的带宽--高兴,但只有FP 10才支持痛苦, 记录下重要的处理环节, 希望给大家帮助. 我暂时只做了图片上传处理功能, 客户端对图片重新编码, 并且更改图片尺寸.
准备环境 Flash CS4, 或者 Flex builder, 需要FP 10的SDK. 我喜欢Flex builder 建立AS project, 这种小工具体积小是关键, 这个release 才20几K.
几个开源类库, 关键是2个, JPEGEncoder.as(注意这不是Adobe的哪个JPGEncoder.as,那个可以完成,但是效率太低了), UploadPostHelper;
1. 首先利用FP10 内置的 FileReference, browse,select打开和装载文件;
2. 再用JPEGEncoder压缩处理图片成JPG格式;
3. 利用UploadPostHelper.getPostData 可以生成Post的body, 因为我要同时传文件和Form表单字段, 这个地方比较关键, 贴下代码:
关键代码
1 public static function Compress(source:IBitmapDrawable, w:uint = 1024, h:uint = 1024):ByteArray{
2 var orgW:uint =source.width;
3 var orgH:uint source.height;
4 var mat:Matrix = new Matrix();
5 if (orgW > w || orgH >h) {
6 var ip:Number = orgW / orgH;
7 if(ip > 1){
8 ip = 1024/orgW;
9 }else{
10 ip = 1024/orgH;
11 }
12 w = ip * orgW;
13 h = ip * orgH;
14 mat.scale(ip, ip);
15 }else{
16 w = orgW;
17 h = orgH;
18 }
19 var bpd:BitmapData = new BitmapData(w, h, true, 0x00FFFFFF);
20 bpd.draw(source,mat); // 編碼
21 var jpegEnc:JPEGEncoder = new JPEGEncoder(80);
22 return jpegEnc.encode(bpd);
23 }
24
25 var dat:ByteArray = PicProccess.Compress(source,800,600);
26
27 … …
28
29 var postParams:object = new object();
30
31 postParams.userName = “”;
32
33 … … …
34
35 var urlRequest:URLRequest = new URLRequest(_postUrl);
36 urlRequest.method = URLRequestMethod.POST;
37 urlRequest.data = UploadPostHelper.getPostData(_file.name, dat,'filename',postParams);
38 urlRequest.requestHeaders.push(new URLRequestHeader('Cache-Control', 'no-cache'));
39 urlRequest.requestHeaders.push(new URLRequestHeader('Content-Type', 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary()));
40 var urlLoader:URLLoader = new URLLoader();
41 urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
42 urlLoader.addEventListener(IOErrorEvent.IO_ERROR, upPicError);
43 urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, upPicError);
44 urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS,upPicError);
45 urlLoader.addEventListener(Event.COMPLETE, upPicCompleted);
46 urlLoader.load( urlRequest );
47
2 var orgW:uint =source.width;
3 var orgH:uint source.height;
4 var mat:Matrix = new Matrix();
5 if (orgW > w || orgH >h) {
6 var ip:Number = orgW / orgH;
7 if(ip > 1){
8 ip = 1024/orgW;
9 }else{
10 ip = 1024/orgH;
11 }
12 w = ip * orgW;
13 h = ip * orgH;
14 mat.scale(ip, ip);
15 }else{
16 w = orgW;
17 h = orgH;
18 }
19 var bpd:BitmapData = new BitmapData(w, h, true, 0x00FFFFFF);
20 bpd.draw(source,mat); // 編碼
21 var jpegEnc:JPEGEncoder = new JPEGEncoder(80);
22 return jpegEnc.encode(bpd);
23 }
24
25 var dat:ByteArray = PicProccess.Compress(source,800,600);
26
27 … …
28
29 var postParams:object = new object();
30
31 postParams.userName = “”;
32
33 … … …
34
35 var urlRequest:URLRequest = new URLRequest(_postUrl);
36 urlRequest.method = URLRequestMethod.POST;
37 urlRequest.data = UploadPostHelper.getPostData(_file.name, dat,'filename',postParams);
38 urlRequest.requestHeaders.push(new URLRequestHeader('Cache-Control', 'no-cache'));
39 urlRequest.requestHeaders.push(new URLRequestHeader('Content-Type', 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary()));
40 var urlLoader:URLLoader = new URLLoader();
41 urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
42 urlLoader.addEventListener(IOErrorEvent.IO_ERROR, upPicError);
43 urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, upPicError);
44 urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS,upPicError);
45 urlLoader.addEventListener(Event.COMPLETE, upPicCompleted);
46 urlLoader.load( urlRequest );
47
服务端就直接接收Post表单一样接收. 基本没有改造服务端代码; 上传图片 我处理最大1024X1024象素, 150k, 要不然现在高象素的照相机10几M, 那些图片让用户处理基本不可能. 或者干脆1M 2M的上传.