Flash as3 online image processing before uploading

Source: Internet
Author: User

Flash Player 10 finally supports the image client processing and preview functions, greatly saving the bandwidth for uploading files/images to the website-happy, but only FP 10 supports pain, record important processes and hope to help you. currently, I only use the image upload function. The client re-encodes the image and changes the image size.

Prepare the flash cs4 environment, or flex builder. You need the FP 10 SDK. I like flex builder to create an as project. This small tool is the key. This release is only 20 K.

There are several open-source class libraries. The key is two, namely, javasencoder. As (note that this is not Adobe's jpgencoder. As, which can be done, but the efficiency is too low), uploadposthelper;

1. Use the built-in filereference, browse, and select of fp10 to open and load files;

2. Use javasencoder to compress and process images in JPG format;

3. You can use uploadposthelper. getpostdata to generate the post body. Because I want to upload both the file and form fields, it is critical to paste them.Code:

Key code

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 ); // Zookeeper
21 VaR incluenc: incluencoder =   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

 

 

The server directly receives the same post form. there is basically no modification to the server code. When uploading images, I can process a maximum of 150 x pixels and kb. Otherwise, the current high-pixel camera is 10 MB, which makes it impossible for users to process images. or simply upload the data at 1 MB or 2 MB.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.