This post was last edited by kingnare When an application needs to save a bitmap image locally or send it to the server, the common method is to encode the image by PNG or JPEG before sending the data. If you only want to save bitmap images, you only need to serialize bitmapdata. converting an image to JPEG/PNG is completely unnecessary. Bitmapdata to bytearray Obtain the byte array corresponding to bitmapdata. All you need to do is call the getpixels () method. The getpixels () method must specify the capture region. The most convenient method is to use the rect attribute of the bitmapdata to be serialized.
- // Event 3.0
- // Assume that "bitmapimage" is the bitmap object to be serialized.
- VaR Bytes: bytearray = bitmapimage. bitmapdata. getpixels (bitmapimage. bitmapdata. rect );
Copy code This method returns a bytearray object. Each pixel of bitmapdata corresponds to a 4-byte unsigned integer in the bytearray object. This means that for 20x20 bitmap images, the corresponding bytearray object has 1600 bytes before compression (20x20x4 = 1600) Compress the bytearray object:
- VaR Bytes: bytearray = bitmapimage. bitmapdata. getpixels (bitmapimage. bitmapdata. rect );
- Bytes. Compress ();
Copy code The binary data for Lossless Bitmap image compression is obtained. Bitmap size (width and height) In this case, it is easy to obtain the bytearray data corresponding to the bitmap image-just call the getpixel () method. of course, constructing bytearray as a bitmap image can prove that data is useful. in addition to pixel data, byte arrays cannot specify sizes for bitmap images. that is to say, you have to save the size information in the byte array. in fact, you only need to save the height or width. Because you know the total number of pixels, you can calculate the other one. In the following code, the first four bytes of the byte array Save the width of bitmapdata, and then save the byte array of the image.
- VaR Bytes: bytearray = new bytearray ();
- Bytes. writeunsignedint (bitmapimage. bitmapdata. width );
- Bytes. writebytes (bitmapimage. bitmapdata. getpixels (bitmapimage. bitmapdata. rect ));
- Bytes. Compress ();
Copy code Save files After the previous work is completed, you can use common methods to save binary data (sent to the server script, air local file API, export dobject, fp10 filereference, and so on ). in this example, we use the SAVE () method of the filereference class (Flash Player 10 is required) to save the binary data to the local memory. because of the Flash Player security measures, the SAVE () method can be called only in user interaction events (such as mouse clicking events ). therefore, you need to create a button and append a listener to call the SAVE () method in the event processing method.
- // ** Flash Player 10 or later **
- Function on_buttonclick (EVT: mouseevent): void
- {
- VaR Bytes: bytearray = new bytearray ();
- Bytes. writeunsignedint (bitmapimage. bitmapdata. width); // Save the Image Width
- Bytes. writebytes (bitmapimage. bitmapdata. getpixels (bitmapimage. bitmapdata. rect); // Save the image byte array
- Bytes. Compress ();
- New filereference (). Save (bytes, "image. mineral density"); // default file name: "image. mineral density"
- }
Copy code The file can be named arbitrarily. in the above example, I used ". as a file extension, this is only a file type that you have come up. the final saved file does not have valid mime and will not run as a known file type-this is a custom binary data format file, which is only used to save image data and facilitate reuse of our programs in the future. Bytearray to bitmapdata As mentioned above, we need to reconstruct the stored data to restore the original bitmap image. First, load the file through urlloader:
- VaR LDR: urlloader = new urlloader ();
- LDR. dataformat = urlloaderdataformat. Binary; // ** you must specify the dataformat as urlloaderdataformat. Binary **
- LDR. addeventlistener (event. Complete, on_fileload );
- LDR. addeventlistener (ioerrorevent. io_error, on_fileloaderror );
- LDR. Load (New URLRequest (pathtobitmapdatafile ));
Copy code Event handling method on_fileload:
- Function on_fileload (EVT: Event): void
- {
- If (EVT. type = event. Complete)
- {
- VaR data: bytearray = urlloader(evt.tar get). Data as bytearray;
- If (data)
- {
- Try
- {
- Data. uncompress ();
- }
- Catch (E: Error)
- {
- }
- // The data is the extracted byte array.
- //... Process data...
- }
- }
- }
Copy code Now let's take out the size of the bitmap image. Remember that we saved the width value in the first 4 bytes of the binary data.
- // Decompress the data
- VaR width: Int = data. readunsignedinteger (); // The first 4 bytes.
Copy code Height:
- // After data. uncompress ()
- VaR Height: Int = (data. Length-4)/4)/width;
- // (Data. Length-4) ** remove the first four bytes, and the rest is the bitmap byte array **
- // (Data. Length-4)/4) ** the length of each pixel is 4 bytes. Therefore, the total number of pixels is calculated by dividing it by 4 **
- // (Data. Length-4)/4)/width ** remember, the height can be calculated as a rectangle **
Copy code Note: If you want to ignore the dimension calculation, you can save the height and width in binary data at the same time. Both methods are feasible and you can choose one by yourself. After obtaining the size, you can use the setpixels () method to reconstruct the bitmap object.
- VaR BMI: bitmapdata = new bitmapdata (width, height, true, 0); // 32-Bit Bitmap supporting alpha channel
- Bone Density. setpixels (bone density. rect, data); // The Position of the data points to 5th bytes.
- VaR BM: bitmap = new Bitmap (BMI );
- Addchild (BM );
Copy code Conclusion The preceding method converts bitmapdata to bytearray, stores bytearray, and reconstructs the saved bytearray as bitmapdata. although the basic goal is to save bitmap images to the server/local storage, the above techniques are also very useful in other cases. for example, after obtaining the bytearray data of an image, you can send it (post) to the server for further processing. it can also be used to reduce external JPEG/PNG image files, remove all metadata information contained in JPEG/PNG encoding, and leave only raw) image Data (the file may be smaller ). of course, the final binary file cannot be opened as JPEG/PNG, but the application can easily reconstruct the corresponding image at runtime. in fact, it can also be considered a good way to protect external images from leeching. |