As3] bitmap serialization (save bitmapdata as Native Binary/bytearray)

Source: Internet
Author: User
Tags uncompress

Http://flash.9ria.com/viewthread.php? Tid = 40783 & utm_source = feedburner & utm_medium = feed & utm_campaign = feed % 3A + 9 rianews + % 289ria.com % E5 % A4 % A9 % E5 % 9C % B0 % E4 % BC % 9A +-++ % E4 % Ba % E4 % Ba % E5 % BF % AB % E6 % 8A % a5 % 29 & utm_content = Google + Reader

 

 

This method is very practical: it is strongly recommended that:

 

 

 

 

 

  • Knowledge leakage:
  •  

  • 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.
  • Getpixels (rect: rectangle): bytearray

    A byte array is generated in the rectangle area of the pixel data. Writes an unsigned integer (32-bit unmultiplied pixel value) to a byte array for each pixel.

    The rect parameter is the image size!

    Readunsignedbyte (): uint

    Reads an unsigned 32-bit integer from a byte stream.

     

    Writeunsignedint (value: uint): void

    Write an unsigned 32-bit integer into the byte stream.

     

    // Obtain bitmap binary data

    1. // Event 3.0
    2. // Assume that "bitmapimage" is the bitmap object to be serialized.
    3. VaR Bytes: bytearray = bitmapimage. bitmapdata. getpixels (bitmapimage. bitmapdata. rect );
  • */
  • <Bitmap is 32-bit> each pixel occupies 4 bytes (because the bitmap is rgba and 32-bit, because the 8-bit represents one byte, the pixel (consisting of rgba) has only 4 bytes)

    The unit of binary data is byte.

    Storage width and height: (because the total number of pixels is: width * height) and the image size rect knows, you only need to know the width or height:

    1. VaR Bytes: bytearray = new bytearray ();
    2. Bytes. writeunsignedint (bitmapimage. bitmapdata. width); // because the width and height of Flash are represented by pixels, four bytes are written, that is, 32 bytes.
    3. Bytes. writebytes (bitmapimage. bitmapdata. getpixels (bitmapimage. bitmapdata. rect); // Save the byte stream to bytes
    4. // Write the width before writing the pixel data of the image! Therefore, the first byte of bytes represents the width, followed by the pixel information.
    5. Bytes. Compress (); // Compression
    6. Read Information:
    7. Extract: Data. unpress (); (data is of the bytearray type and has binary image data)

    Read width: var width: Int = data. readunsignedinteger (); // The first 4 bytes.

    Height:

    1. // After data. uncompress ()
    2. VaR Height: Int = (data. Length-4)/4)/width;
    3. // (Data. Length-4) ** remove the first four bytes, and the rest is the bitmap byte array **
    4. // (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 **
    5. // (Data. Length-4)/4)/width ** remember, the height can be calculated as a rectangle **

     

    After obtaining the width and height, restore the image:

    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 );

     

     

     

     

     

     

     

     

  • Source: http://www.ghostwire.com/blog/archives/as3-serializing-bitmaps-storing-bitmapdata-as-raw-binarybytearray/
  • Original information title: [as3] serializing bitmaps (storing bitmapdata as raw binary/bytearray)
  • Original Author: Sunny

    My comments:
    What do you think about this article! Welcome to drool and brick, haha. You are welcome to share more information with us.

    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.

    1. // Event 3.0
    2. // Assume that "bitmapimage" is the bitmap object to be serialized.
    3. 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:

    1. VaR Bytes: bytearray = bitmapimage. bitmapdata. getpixels (bitmapimage. bitmapdata. rect );
    2. 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.

    1. VaR Bytes: bytearray = new bytearray ();
    2. Bytes. writeunsignedint (bitmapimage. bitmapdata. width );
    3. Bytes. writebytes (bitmapimage. bitmapdata. getpixels (bitmapimage. bitmapdata. rect ));
    4. 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.

    1. // ** Flash Player 10 or later **
    2. Function on_buttonclick (EVT: mouseevent): void
    3. {
    4. VaR Bytes: bytearray = new bytearray ();
    5. Bytes. writeunsignedint (bitmapimage. bitmapdata. width); // Save the Image Width
    6. Bytes. writebytes (bitmapimage. bitmapdata. getpixels (bitmapimage. bitmapdata. rect); // Save the image byte array
    7. Bytes. Compress ();
    8. New filereference (). Save (bytes, "image. mineral density"); // default file name: "image. mineral density"
    9. }

    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:

    1. VaR LDR: urlloader = new urlloader ();
    2. LDR. dataformat = urlloaderdataformat. Binary; // ** you must specify the dataformat as urlloaderdataformat. Binary **
    3. LDR. addeventlistener (event. Complete, on_fileload );
    4. LDR. addeventlistener (ioerrorevent. io_error, on_fileloaderror );
    5. LDR. Load (New URLRequest (pathtobitmapdatafile ));

    Copy code

    Event handling method on_fileload:

    1. Function on_fileload (EVT: Event): void
    2. {
    3. If (EVT. type = event. Complete)
    4. {
    5. VaR data: bytearray = urlloader(evt.tar get). Data as bytearray;
    6. If (data)
    7. {
    8. Try
    9. {
    10. Data. uncompress ();
    11. }
    12. Catch (E: Error)
    13. {
    14. }
    15. // The data is the extracted byte array.
    16. //... Process data...
    17. }
    18. }
    19. }

    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.

    1. // Decompress the data
    2. VaR width: Int = data. readunsignedinteger (); // The first 4 bytes.

    Copy code

    Height:

    1. // After data. uncompress ()
    2. VaR Height: Int = (data. Length-4)/4)/width;
    3. // (Data. Length-4) ** remove the first four bytes, and the rest is the bitmap byte array **
    4. // (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 **
    5. // (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.

    1. VaR BMI: bitmapdata = new bitmapdata (width, height, true, 0); // 32-Bit Bitmap supporting alpha channel
    2. Bone Density. setpixels (bone density. rect, data); // The Position of the data points to 5th bytes.
    3. VaR BM: bitmap = new Bitmap (BMI );
    4. 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.

  • 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.