Posts Tagged ‘filereference’

Updateted : How to write Jpeg || Png Files in Swf Files

Today I wrote a new Class using swf assist for writing Jpeg or PNG files to a SWF File.

Now you could use :

 actionscript3 |  copy |? 
1
_imagePro = new ImageToSwfProcessor();
2
_imagePro.addEventListener(ImageProcessorEvent.NEXT_FILE, handleNextFile, false, 0, true);
3
_imagePro.writeSwfFromImage(image:*,quality : int ,"png" || "jpeg" : String, framecount : int);

Method wirteSwfFromImage has some Parameters.

1) The decent File as swf, jpeg or what else.

2) quality is used if image is jpeg or swf.

3) type is used to determ the current file. Only png or jpeg are used

4) framecount used to increment frames.

The Listener is called if you have many Files to store in a swf file. After each swf writing Process that listener would dispatch.

Here is that one.

 actionscript3 |  copy |? 
001
/**
002
 * Copyright 04.09.2009 Christian Sobolewski
003
 * 
004
 * usage : com.cs.imageProcessor
005
 * 
006
 * Parameters:
007
 * 
008
 * Version:
009
 * 
010
 * History:
011
 * 
012
 * com.cs.imageProcessor.ImageProcessor
013
 * @author Christian Sobolewski
014
 */
015
package com.cs.imageProcessor {
016
	import com.adobe.images.JPGEncoder;
017
	import com.adobe.images.PNGEncoder;
018
	import com.cs.logging.SOS;
019
 
020
	import org.libspark.swfassist.io.ByteArrayOutputStream;
021
	import org.libspark.swfassist.swf.io.SWFWriter;
022
	import org.libspark.swfassist.swf.io.WritingContext;
023
	import org.libspark.swfassist.swf.structures.FillStyle;
024
	import org.libspark.swfassist.swf.structures.FillStyleTypeConstants;
025
	import org.libspark.swfassist.swf.structures.SWF;
026
	import org.libspark.swfassist.swf.structures.ShapeWithStyle;
027
	import org.libspark.swfassist.swf.structures.StraightEdgeRecord;
028
	import org.libspark.swfassist.swf.structures.StyleChangeRecord;
029
	import org.libspark.swfassist.swf.tags.DefineBitsJPEG3;
030
	import org.libspark.swfassist.swf.tags.DefineShape;
031
	import org.libspark.swfassist.swf.tags.PlaceObject2;
032
	import org.libspark.swfassist.swf.tags.SetBackgroundColor;
033
	import org.libspark.swfassist.swf.tags.ShowFrame;
034
 
035
	import flash.display.BitmapData;
036
	import flash.display.Sprite;
037
	import flash.events.Event;
038
	import flash.events.EventDispatcher;
039
	import flash.events.IOErrorEvent;
040
	import flash.events.MouseEvent;
041
	import flash.events.ProgressEvent;
042
	import flash.events.SecurityErrorEvent;
043
	import flash.net.FileReference;
044
	import flash.utils.ByteArray;
045
 
046
	/**
047
	 * @author Christian Sobolewski
048
	 */
049
	public class ImageToSwfProcessor extends EventDispatcher {
050
 
051
		private var _swf : SWF;
052
		private var _bytes : ByteArray;
053
		private var _fileRef : FileReference;
054
		private var _name : String;
055
		private var _eof : Sprite;
056
 
057
		public function ImageToSwfProcessor() {
058
		}
059
 
060
		public function writeSwfFromImage(image : *, quality : int, type : String , frames : int = 1, bgColor : int = 0x000066) : BitmapData {
061
			_name = image.name.split(","+type).join("").toString();
062
 
063
			var w : Number = image.width;
064
			var h : Number = image.height;
065
 
066
			_swf = new SWF();
067
			_swf.header.version = 9;
068
			_swf.header.isCompressed = false;
069
			_swf.header.frameSize.xMax = image.width;
070
			_swf.header.frameSize.yMax = image.height;
071
			_swf.header.frameRate = 1;
072
			_swf.header.numFrames = 1;	
073
 
074
			var bg : SetBackgroundColor = new SetBackgroundColor();
075
			bg.backgroundColor.fromUint(bgColor);
076
			_swf.tags.addTag(bg);
077
 
078
			var bmd : BitmapData;
079
 
080
			if (type == "png") {
081
				bmd = new BitmapData(w, h, true, 0x000000ff);
082
				bmd.draw(image);
083
				_bytes = PNGEncoder.encode(bmd);
084
			} else {
085
				bmd = new BitmapData(w, h, false);
086
				bmd.draw(image);
087
				_bytes = new JPGEncoder(quality).encode(bmd);
088
			}		
089
 
090
//			SOS.alert("image.width = " + image.width);
091
//			SOS.alert("image.height= " + image.height);
092
 
093
			for (var i : uint = 0;i < frames;++i) {
094
 
095
				var defineJPEG : DefineBitsJPEG3 = new DefineBitsJPEG3();
096
				defineJPEG.characterId = (i * 2) + 1;
097
				defineJPEG.jpegData = _bytes;
098
 
099
				_swf.tags.addTag(defineJPEG);
100
 
101
				var defineShape : DefineShape = new DefineShape();
102
				defineShape.shapeId = (i * 2 + 1) + 1;
103
				defineShape.shapeBounds.xMax = w;
104
				defineShape.shapeBounds.yMax = h;
105
				var fillStyle : FillStyle = new FillStyle();
106
				fillStyle.fillStyleType = FillStyleTypeConstants.CLIPPED_BITMAP_FILL;
107
				fillStyle.bitmapId = (i * 2) + 1;
108
				fillStyle.bitmapMatrix.hasScale = true;
109
				fillStyle.bitmapMatrix.scaleX = 0;
110
				fillStyle.bitmapMatrix.scaleY = 0;
111
				var shape : ShapeWithStyle = defineShape.shapes;
112
				shape.fillStyles.fillStyles.push(fillStyle);
113
				var r1 : StyleChangeRecord = new StyleChangeRecord();
114
				r1.fillStyle0 = 1;
115
				r1.moveDeltaX = 0;
116
				r1.moveDeltaY = 0;
117
				r1.stateFillStyle0 = true;
118
				r1.stateMoveTo = true;
119
				var r2 : StraightEdgeRecord = new StraightEdgeRecord();
120
				r2.verticalLine = true;
121
				r2.deltaY = h;
122
				var r3 : StraightEdgeRecord = new StraightEdgeRecord();
123
				r3.horizontalLine = true;
124
				r3.deltaX = w;
125
				var r4 : StraightEdgeRecord = new StraightEdgeRecord();
126
				r4.verticalLine = true;
127
				r4.deltaY = -h;
128
				var r5 : StraightEdgeRecord = new StraightEdgeRecord();
129
				r5.horizontalLine = true;
130
				r5.deltaX = -w;
131
				shape.shapeRecords.push(r1, r2, r3, r4, r5);
132
 
133
				_swf.tags.addTag(defineShape);
134
 
135
				var placeObject : PlaceObject2 = new PlaceObject2();
136
				placeObject.characterId = (i * 2 + 1) + 1;
137
				placeObject.depth = i + 1;
138
				placeObject.hasCharacter = true;
139
 
140
				_swf.tags.addTag(placeObject);
141
				_swf.tags.addTag(new ShowFrame());
142
				_swf.header.numFrames++;
143
			}
144
 
145
 
146
			var swfBytes : ByteArray = new ByteArray();
147
 
148
			var wc : WritingContext = new WritingContext();
149
			//			wc.length = 5000;
150
 
151
			var write : SWFWriter = new SWFWriter();
152
			write.writeSWF(new ByteArrayOutputStream(swfBytes), wc, _swf);
153
 
154
			_fileRef = new FileReference();
155
			_fileRef.addEventListener(Event.COMPLETE, handleCompleteEvent, false, 0, true);
156
			_fileRef.addEventListener(ProgressEvent.PROGRESS, handleProgressEvent, false, 0, true);
157
			_fileRef.addEventListener(Event.CANCEL, handleCancelEvent, false, 0, true);
158
			_fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleSecurityEvent, false, 0, true);
159
			_fileRef.addEventListener(IOErrorEvent.IO_ERROR, onSaveError, false, 0, true);
160
			_fileRef.addEventListener(Event.OPEN, onOpen, false, 0, true);
161
 
162
			_fileRef.save(swfBytes, _name + ".swf");
163
 
164
			/**
165
			 * strange things happen here. if you dont activate Event.ENTER_FRAME no Event.COMPLETE will fire !!! 
166
			 */
167
 
168
			_eof = new Sprite();
169
			_eof.addEventListener(Event.ENTER_FRAME, handleEof, false, 0, true);
170
 
171
			return bmd;
172
		}
173
 
174
		private function handleEof(event : Event) : void {
175
			if(_fileRef.data) SOS.alert(_fileRef.data);
176
		}
177
 
178
		private function onOpen(event : Event) : void {
179
//			SOS.alert('onOpen: ' + (onOpen));
180
		}
181
 
182
		private function handleClick(event : MouseEvent) : void {
183
//			SOS.alert('handleClick: ' + (handleClick));
184
		}
185
 
186
		private function onSaveError(event : IOErrorEvent) : void {
187
//			SOS.alert('onSaveError: ' + (onSaveError));
188
		}
189
 
190
		private function handleSecurityEvent(event : SecurityErrorEvent) : void {
191
//			SOS.alert('handleSecurityEvent: ' + (handleSecurityEvent));
192
		}
193
 
194
		private function handleCancelEvent(event : Event) : void {
195
//			SOS.alert("cancel event");
196
		}
197
 
198
		private function handleProgressEvent(event : ProgressEvent) : void {
199
			SOS.alert(event.bytesLoaded + " | " + event.bytesTotal);
200
		}
201
 
202
		private function handleCompleteEvent(event : Event) : void {
203
			if (_eof.hasEventListener(Event.ENTER_FRAME)) _eof.removeEventListener(Event.ENTER_FRAME, handleEof);
204
			dispatchEvent(new ImageProcessorEvent(ImageProcessorEvent.NEXT_FILE))
205
//			SOS.alert("save complete");
206
		}
207
	}
208
}
209

http://privatefunction.net/wp-content/plugins/sociofluid/images/digg_32.png http://privatefunction.net/wp-content/plugins/sociofluid/images/delicious_32.png http://privatefunction.net/wp-content/plugins/sociofluid/images/technorati_32.png http://privatefunction.net/wp-content/plugins/sociofluid/images/google_32.png http://privatefunction.net/wp-content/plugins/sociofluid/images/facebook_32.png http://privatefunction.net/wp-content/plugins/sociofluid/images/mixx_32.png http://privatefunction.net/wp-content/plugins/sociofluid/images/twitter_32.png

How to save a SWF file at runtime with swfassist and FileReference

Here`s a little class to build a swf file at runtime with 2 images. Just an example :

Here you could find the swfassist framework. Unfortunately its not documented very well.
Just check out the svn for some examples.

JPGEncoder click here

I made a swf file with 20 frames, each frame the shown bitmap is switched. The swf could be saved locally on your Pc.

  |  copy |? 
001
002
/**
003
 * Copyright 31.08.2009 Christian Sobolewski
004
 * 
005
 * usage : 
006
 * 
007
 * Parameters:
008
 * 
009
 * Version:
010
 * 
011
 * History:
012
 * 
013
 * SwfWrite
014
 * @author Christian Sobolewski
015
 */
016
package {
017
	import com.adobe.images.JPGEncoder;
018
 
019
	import org.libspark.swfassist.io.ByteArrayOutputStream;
020
	import org.libspark.swfassist.swf.io.SWFWriter;
021
	import org.libspark.swfassist.swf.io.WritingContext;
022
	import org.libspark.swfassist.swf.structures.FillStyle;
023
	import org.libspark.swfassist.swf.structures.FillStyleTypeConstants;
024
	import org.libspark.swfassist.swf.structures.SWF;
025
	import org.libspark.swfassist.swf.structures.ShapeWithStyle;
026
	import org.libspark.swfassist.swf.structures.StraightEdgeRecord;
027
	import org.libspark.swfassist.swf.structures.StyleChangeRecord;
028
	import org.libspark.swfassist.swf.tags.DefineBitsJPEG2;
029
	import org.libspark.swfassist.swf.tags.DefineShape;
030
	import org.libspark.swfassist.swf.tags.PlaceObject2;
031
	import org.libspark.swfassist.swf.tags.RemoveObject2;
032
	import org.libspark.swfassist.swf.tags.SetBackgroundColor;
033
	import org.libspark.swfassist.swf.tags.ShowFrame;
034
 
035
	import flash.display.BitmapData;
036
	import flash.display.DisplayObject;
037
	import flash.display.Sprite;
038
	import flash.geom.Rectangle;
039
	import flash.net.FileReference;
040
	import flash.utils.ByteArray;
041
 
042
	/**
043
	 * @author Christian Sobolewski
044
	 */
045
	public class SwfWrite extends Sprite {
046
 
047
		private var _swf : SWF;
048
 
049
		[Embed(source="test.jpg")] 
050
		public var imgCls : Class;
051
 
052
		[Embed(source="logo.jpg")] 
053
		public var imgCls2 : Class;
054
 
055
		private var _pixels : ByteArray;
056
		private var _pixelsOne : ByteArray;
057
		private var _data : Array;
058
		private var _maxLength : uint;
059
 
060
		public function SwfWrite() {
061
 
062
			_maxLength = 20;
063
 
064
			var imgCls : DisplayObject = new imgCls();
065
			var imgCls2 : DisplayObject = new imgCls2();
066
 
067
			_swf = new SWF();
068
			_swf.header.version = 9;
069
			_swf.header.frameSize.xMax = 1024;
070
			_swf.header.frameSize.yMax = 768;
071
			_swf.header.frameRate = 2;
072
			_swf.header.numFrames = 1;
073
 
074
			var bgColor : SetBackgroundColor = new SetBackgroundColor();
075
			bgColor.backgroundColor.fromUint(0x000066);
076
			_swf.tags.addTag(bgColor);
077
 
078
			var bmd : BitmapData = new BitmapData(640, 480, false);
079
			bmd.draw(imgCls);
080
 
081
			_pixelsOne = new JPGEncoder(50).encode(bmd);
082
 
083
			var bmd2: BitmapData= new BitmapData(640, 480, false);
084
			bmd2.draw(imgCls2);
085
 
086
			_pixels = new JPGEncoder(50).encode(bmd2);
087
			_data = [];
088
			var q : Number;
089
			for (var i : int = 0;i < _maxLength;i++) {
090
				q = i % 2;
091
				if (q == 0) {
092
					_data.push(_pixelsOne);
093
				} else {
094
					_data.push(_pixels);
095
				}
096
			}
097
 
098
			test();
099
 
100
 
101
			var bytes : ByteArray = new ByteArray();
102
			var wc : WritingContext = new WritingContext();
103
			wc.length = 5000;
104
 
105
			var write : SWFWriter = new SWFWriter();
106
			write.writeSWF(new ByteArrayOutputStream(bytes), wc, _swf);
107
 
108
			var fileRef : FileReference = new FileReference();
109
			fileRef.save(bytes, "writen" + Math.random() + 100 + ".swf");
110
		}
111
 
112
		private function test() : void {
113
			for (var i : uint = 0;i < _maxLength;++i) {
114
				//				var file:File = File(files[i]);
115
 
116
				var defineJPEG : DefineBitsJPEG2 = new DefineBitsJPEG2();
117
				defineJPEG.characterId = (i * 2) + 1;
118
 
119
				//				var fileStream:FileStream = new FileStream();
120
				//				fileStream.open(file, FileMode.READ);
121
				//				fileStream.readBytes(defineJPEG.jpegData);
122
				//				fileStream.close();
123
				//				fileStream = null;
124
 
125
				defineJPEG.jpegData = _data[i];
126
 
127
				_swf.tags.addTag(defineJPEG);
128
 
129
				var defineShape : DefineShape = new DefineShape();
130
				defineShape.shapeId = (i * 2 + 1) + 1;
131
				defineShape.shapeBounds.xMax = 500;
132
				defineShape.shapeBounds.yMax = 375;
133
				var fillStyle : FillStyle = new FillStyle();
134
				fillStyle.fillStyleType = FillStyleTypeConstants.CLIPPED_BITMAP_FILL;
135
				fillStyle.bitmapId = (i * 2) + 1;
136
				fillStyle.bitmapMatrix.hasScale = true;
137
				fillStyle.bitmapMatrix.scaleX = 20;
138
				fillStyle.bitmapMatrix.scaleY = 20;
139
				var shape : ShapeWithStyle = defineShape.shapes;
140
				shape.fillStyles.fillStyles.push(fillStyle);
141
				var r1 : StyleChangeRecord = new StyleChangeRecord();
142
				r1.fillStyle0 = 1;
143
				r1.moveDeltaX = 50;
144
				r1.moveDeltaY = 50;
145
				r1.stateFillStyle0 = true;
146
				r1.stateMoveTo = true;
147
				var r2 : StraightEdgeRecord = new StraightEdgeRecord();
148
				r2.verticalLine = true;
149
				r2.deltaY = 375;
150
				var r3 : StraightEdgeRecord = new StraightEdgeRecord();
151
				r3.horizontalLine = true;
152
				r3.deltaX = 500;
153
				var r4 : StraightEdgeRecord = new StraightEdgeRecord();
154
				r4.verticalLine = true;
155
				r4.deltaY = -375;
156
				var r5 : StraightEdgeRecord = new StraightEdgeRecord();
157
				r5.horizontalLine = true;
158
				r5.deltaX = -500;
159
				shape.shapeRecords.push(r1, r2, r3, r4, r5);
160
 
161
				_swf.tags.addTag(defineShape);
162
 
163
				var placeObject : PlaceObject2 = new PlaceObject2();
164
				placeObject.characterId = (i * 2 + 1) + 1;
165
				placeObject.depth = i + 1;
166
				placeObject.hasCharacter = true;
167
 
168
				_swf.tags.addTag(placeObject);
169
				_swf.tags.addTag(new ShowFrame());
170
				_swf.header.numFrames++;
171
 
172
//				for (var fadeIn:uint = 0; fadeIn < 20; ++fadeIn) {
173
//					var fadeObject : PlaceObject2 = new PlaceObject2();
174
//					fadeObject.depth = i + 1;
175
//					fadeObject.isMove = true;
176
//					fadeObject.hasColorTransform = true;
177
//					fadeObject.colorTransform.hasMultiplication = true;
178
//					fadeObject.colorTransform.redMultiplication = 256;
179
//					fadeObject.colorTransform.greenMultiplication = 256;
180
//					fadeObject.colorTransform.blueMultiplication = 256;
181
//					fadeObject.colorTransform.alphaMultiplication = (((1.0 / 20) * 20) * 256);
182
//					_swf.tags.addTag(fadeObject);
183
//					_swf.tags.addTag(new ShowFrame());
184
//					_swf.header.numFrames++;
185
//				}
186
//
187
//				if (i > 0) {
188
//					var removeObject : RemoveObject2 = new RemoveObject2();
189
//					removeObject.depth = (i - 1) + 1;
190
//					_swf.tags.addTag(removeObject);
191
//				}
192
 
193
//				for (var n:uint = 0; n < len; ++n) {
194
//					_swf.tags.addTag(new ShowFrame());
195
//					_swf.header.numFrames++;
196
//				}
197
			}
198
		}
199
	}
200
}
201

http://privatefunction.net/wp-content/plugins/sociofluid/images/digg_32.png http://privatefunction.net/wp-content/plugins/sociofluid/images/delicious_32.png http://privatefunction.net/wp-content/plugins/sociofluid/images/technorati_32.png http://privatefunction.net/wp-content/plugins/sociofluid/images/google_32.png http://privatefunction.net/wp-content/plugins/sociofluid/images/facebook_32.png http://privatefunction.net/wp-content/plugins/sociofluid/images/mixx_32.png http://privatefunction.net/wp-content/plugins/sociofluid/images/twitter_32.png
Return top

INFORMATION