JPEG Async Encoder class meets merging multi-BitmapData

本文介绍了一种改进Flash中PNG和JPEG编码性能的方法,包括优化编码速度和CPU使用率,通过重写和合并现有类实现异步编码,并提供了一个用于合并多个位图数据到一个JPEG图像的实用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I often use PNG/JPEG Encoding in Flash and during my projects I faced some problems with this classes.
The main problem is speed and CPU usage when encoding large BitmapData objects. First I decided to rewrite PNG Encoder class provided by Tinic Uro and optimized by Aral but we cant make it encoding data asynchronous because it is using built in ByteArray compress method to GZIP image data. When encoder calls this method on large data Flash Player completely freezes. It can take up to 15 seconds to compress data. And you can do nothing all this time but waiting. May be there is an alternate way to compress ByteArray but I didnt find any.

So I turned to JPEG Encoder class provided by Adobe. After some research I found a solution forAsync Encoding. As far as it was designed for Flex I rewrite the class merging everything in one file. Now it is all in one solution. You can encode BitmapData and ByteArray objects Async listening for Progress and Complete Events or encode data through one method call.
I’ve also added method for merging several BitmapData objects in to one JPEG image. This method is very useful when you need to generate large image but can’t do it because of the Flash built in BitmapData size restrictions.
I’m using this class in my Flickr Mosaic engine to encode result mosaic image. See it in action merging several BitmapData objects into one JPEG file.

Class itself could be seen in my Google Code repository within Flickr Mosaic sources.

See some usage examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import ru.inspirit.utils.JPGEncoder;
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.utils.ByteArray;
 
var bmp1:BitmapData = new BitmapData(640, 400, false, 0xFF0000);
var bmp2:BitmapData = new BitmapData(640, 400, false, 0x00FF00);
var bmp3:BitmapData = new BitmapData(640, 400, false, 0x0000FF);
var bmp4:BitmapData = new BitmapData(640, 400, false, 0xFFFF00);
 
var je:JPGEncoder = new JPGEncoder(90);
je.addEventListener(ProgressEvent.PROGRESS, onEncodingProgress);
je.addEventListener(Event.COMPLETE, onEncodingComplete);
 
// Encoding several BitmapData objects into one image
// You should provide 'square' of Bitmaps in 2 dimensional array
je.encodeMultiToOne([
				[bmp1, bmp2],
				[bmp3, bmp4]
				]);
 
// Simple encode
/*
var ImageByteArray:ByteArray = je.encode(bmp1); 
*/
 
// Async encode
// You should provide listeners (see above) to handle data ready event
/*
je.encodeAsync(bmp1);
*/
 
private function onEncodingComplete(e:Event):void 
{
	// Async Encoding Complete
	var ImageByteArray:ByteArray = je.ImageData;
}
 
private function onEncodingProgress(e:ProgressEvent):void 
{
	trace('ENCODING PROGRESS: ' + Math.round(e.bytesLoaded/e.bytesTotal * 100) + '%');
}


转载:http://blog.inspirit.ru/?p=201

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值