原文 http://www.cppblog.com/sleepwom/archive/2010/06/30/119003.html
AS3 bitmapdata与bytearray转换
Apr
8
2008
bitmapdata.getpixel 方法是ActionScript 3.0中的一个新方法,可以把矩形内的像素读取成bytearray,但是这种bytearray又不能直接用loader.loadbytes来读,一读就会出现IOERROR。
为了达到可以直接用loader.loadbytes读取的目的,找了一下百度和GOOGLE,找到了以下方法,有需要的可以用用。
PS 以下代码转自互联网,牛C网只负责整理
/*
Copyright (c) 2007 Trevor McCauley - www.senocular.com
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS or IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS or COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES or OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT or OTHERWISE, ARISING
FROM, OUT OF or IN CONNECTION WITH THE SOFTWARE or THE USE or
OTHER DEALINGS IN THE SOFTWARE.
*/
package com.senocular.images {
import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.utils.ByteArray;
import flash.utils.Endian;
public class BMPEncoder {
/**
* Converts a BitmapData instance into a 32-bit
* BMP image.
* @param bitmapData A BitmapData instance of the image
* desired to have converted into a Bitmap (BMP).
* @return A ByteArray containing the binary Bitmap (BMP)
* representation of the BitmapData instance passed.
*/
public static function encode(bitmapData:BitmapData):ByteArray {
// image/file properties
var bmpWidth:int = bitmapData.width;
var bmpHeight:int = bitmapData.height;
var imageBytes:ByteArray = bitmapData.getPixels(bitmapData.rect);
var imageSize:int = imageBytes.length;
var imageDataOffset:int = 0x36;
var fileSize:int = imageSize + imageDataOffset;
// binary BMP data
var bmpBytes:ByteArray = new ByteArray();
bmpBytes.endian = Endian.LITTLE_ENDIAN; // byte order
// header information
bmpBytes.length = fileSize;
bmpBytes.writeByte(0x42); // B
bmpBytes.writeByte(0x4D); // M (BMP identifier)
bmpBytes.writeInt(fileSize); // file size
bmpBytes.position = 0x0A; // offset to image data
bmpBytes.writeInt(imageDataOffset);
bmpBytes.writeInt(0x28); // header size
bmpBytes.position = 0x12; // width, height
bmpBytes.writeInt(bmpWidth);
bmpBytes.writeInt(bmpHeight);
bmpBytes.writeShort(1); // planes (1)
bmpBytes.writeShort(32); // color depth (32 bit)
bmpBytes.writeInt(0); // compression type
bmpBytes.writeInt(imageSize); // image data size
bmpBytes.position = imageDataOffset; // start of image data...
// write pixel bytes in upside-down order
// (as per BMP format)
var col:int = bmpWidth;
var row:int = bmpHeight;
var rowLength:int = col * 4; // 4 bytes per pixel (32 bit)
try {
// make sure we're starting at the
// beginning of the image data
imageBytes.position = 0;
// bottom row up
while (row--) {
// from end of file up to imageDataOffset
bmpBytes.position = imageDataOffset + row*rowLength;
// read through each column writing
// those bits to the image in normal
// left to rightorder
col = bmpWidth;
while (col--) {
bmpBytes.writeInt(imageBytes.readInt());
}
}
}catch(error:Error){
// end of file
}
// return BMP file
return bmpBytes;
}
}
}
分享到:
相关推荐
ActionScript 3 BitmapData 的例子 有助于你了解ActionScript 3位图!
NULL 博文链接:https://as3.iteye.com/blog/964080
Flex RIA AS3网页游戏 BitmapData CopyPixel用法.rar
EaselJS的位图数据EaselJS的BitmapData将AS3(例如BitmapData)添加到EaselJS。例1 // create BitmapData by HTMLImageElement_bmd01 = new createjs.BitmapData(HTMLImageElement);_bitmap01 = new createjs.Bitmap...
as2.0 模拟火焰 import flash.display.*...var blueSpot: BitmapData = new BitmapData( clipBounds.xMax, clipBounds.yMax, true, 0 ) blueSpot.draw( blueSpotClip, new Matrix() ) blueSpotClip.removeMovieClip()
最近要转开发平台,正研究C#。C#好是好,不过处理图片时一个像素一个像素的操作像素不是一般的慢。其实Delphi也一样,但好在Delphi的Bitmap类提供了ScanLines,可以一行一行的读图,效率比较高。C#应该也有类似的...
AS3位图压缩类,PNG编码. 通过PNGEncoder对bitmapdata进行encode转换为对应图像格式的bytearray,PNGEncoder 类使用便携网络图形 (PNG) 无损压缩将原始位图图像转换为编码图像。
想把 draw方法弄明白
通过PNGEncoder、JPEGEncoder对bitmapdata进行encode转换为对应图像格式的bytearray,这时会根据对应图像格式的压缩算法转换为编码图像的bytearray,如JPEGEncoder 类使用联合图像专家组 (JPEG) 压缩将原始的位图...
delphi 找图找色 BitmapData.pas 使用方法请到原作者yeye55的blog查看, http://yeye55blog.blog.163.com/blog/static/19724102120111043252016/
dds as3解析器,用法如下: var dds:DDSParser=new DDSParser(ByteArray(value)); dds.proceedParsing(); this._bitmapData=dds.getResultLevelImg(0);
这是个as3的工具类,用于实现检测2个不规则对象,是否有碰撞。 用法:var res:Boolean=BitmapHitTest.test( mc1,mc2); trace("mc1与mc2碰撞情况为:"+res);
as3libwebp 使用 FlashCC 或更快的 ANE 在 Flash 上加载和保存 WEBP 图像 样本 应用程序接口 package libwebp ; function DecodeWebp(webpByteArray : ByteArray ) : BitmapData ; function EncodeWebp(input : ...
通过 BitmapData.threshold 的方法来实现对图片的特殊效果的展示。附源文件。
最简单的WebGame开发入门 示例是个人所写 实现一张png图片 进行人物的移动 可以上下左右移动人物 实现方法是通过BitMapData 和bitMap操作位图移动帧
将Bitmap转成byte[],进行保存,用到时再取出byte[],转化成Bitmap,进行显示
一个delphi下用于快速找图找色的单元,很好用!!! 原下载地址已经全部失效,在这里分享给大家,希望使用delphi的朋友能够喜欢。
[FLASH/AS1/2]简易涂鸦板(带本地保存功能与撤消上一步功能) 作者:古树悬叶 日期:2010-01-07 二类涂鸦板:一种是主流的通过保存鼠标轨迹的方式来保存涂鸦数据,既使用矢量的方式来保存,样例如闪吧的涂鸦程序;...
BitmapData类实例 运用噪声制作一些好的效果 源码