LogoLogo
  • Home
  • Projects
  • About
  • Contact

Loading, Filtering, and Saving Images with Flash Player 10

Devon O. · June 16, 2008 · Actionscript · 3 comments
1

Still playing with the new features of Flash Player 10.

This example uses a halftone Pixel Bender filter created by Justin Everett-Church which you can check out here. It also uses the new additions to the FileReference api to upload and download files.

To use, just click on “load image” and browse for an image file to upload. Make sure the file is under 50k or the application will just fail silently. Once the image is uploaded the halftone filter will be automatically applied. Use the sliders to make adjustments and the color picker to choose a different background color. Once satisfied with your creation, click on save image to save the altered file back on your own computer. Amazing, even if I do say so myself.

Incidentally, if your image is larger than the viewing area, you can drag it around to view the entire thing.

With Flash CS4/Flash Player 10 Image and Sound editing applications are going to be so freaking easy to build (not sure about video yet – that may take a bit of finagling).

Again, to view the example, you’ll need Flash Player 10 installed:

And the code to create the above (I wouldn't leave ya hangin'):

C#
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package {
 
    import com.adobe.images.JPGEncoder;
    import com.onebyonedesign.ui.events.ColorEvent;
    import com.onebyonedesign.ui.events.ValueSliderEvent;
    import com.onebyonedesign.ui.OBO_ColorPicker;
    import com.onebyonedesign.ui.OBO_ValueSlider;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.DisplayObject;
    import flash.display.Loader;
    import flash.display.Shader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.DropShadowFilter;
    import flash.filters.ShaderFilter;
    import flash.geom.Rectangle;
    import flash.net.FileFilter;
    import flash.net.FileReference;
    import flash.net.URLLoader;
    import flash.net.URLLoaderDataFormat;
    import flash.net.URLRequest;
    import flash.text.AntiAliasType;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import flash.utils.ByteArray;
    
    /**
    * Load image manipulate halftone effect then save image.
    * @author Devon O.
    */
    [SWF(width="500", height="500", backgroundColor="#869CA7", framerate="31")]
    public class main extends Sprite {
        
        [Embed(source="halftone.pbj", mimeType="application/octet-stream")]
        private var ShaderData:Class;
        
        private var _fileRef:FileReference;
        
        private var _shader:Shader;
        private var _shaderFilter:ShaderFilter;
        private var _image:Sprite = new Sprite();    
        private var _dragBounds:Rectangle = new Rectangle();
        private var _imgWidth:Number;
        private var _imgHeight:Number;
        
        // UI
        private var _imageHolder:Sprite;
        private var _controlHolder:Sprite;
        private var _imageMask:Sprite;
        private var _radiusControl:OBO_ValueSlider;
        private var _offsetControl:OBO_ValueSlider;
        private var _bgcolorControl:OBO_ColorPicker;
            
        public function main():void {
            _shader = new Shader();
            _shader.byteCode = new ShaderData();
            _shaderFilter = new ShaderFilter(_shader);
            
            _fileRef = new FileReference();
            
            var titleText:TextField = new TextField();
            titleText.defaultTextFormat = new TextFormat("_sans", 30, 0x000000, true);
            titleText.selectable = false;
            titleText.antiAliasType = AntiAliasType.ADVANCED;
            titleText.mouseEnabled = false;
            titleText.text = "Half Tone Image Filter";
            titleText.autoSize = TextFieldAutoSize.LEFT;
            titleText.x = 40;
            titleText.y = 350;
            titleText.rotationZ = -90;
            titleText.alpha = .2
            addChild(titleText);
            
            init();
        }
        
        private function init():void {
            initUI();
        }
        
        private function initUI():void {
            var shadow:DropShadowFilter = new DropShadowFilter(2, 90);
            var format:TextFormat = new TextFormat("_sans", 11);
            
            _imageHolder = new Sprite();
            _imageHolder.graphics.beginFill(0xFFFFCC);
            _imageHolder.graphics.drawRect(0, 0, 350, 350);
            _imageHolder.graphics.endFill();
            _imageHolder.filters = [shadow];
            _imageHolder.x = 75;
            _imageHolder.y = 20;
            addChild(_imageHolder);
            
            _imageMask = new Sprite();
            _imageMask.graphics.beginFill(0x232323);
            _imageMask.graphics.drawRect(0, 0, 350, 350);
            _imageMask.graphics.endFill();
            _imageMask.visible = false;
            _imageHolder.addChild(_imageMask);
            
            _controlHolder = new Sprite();
            _controlHolder.graphics.beginFill(0xEEEEE6);
            _controlHolder.graphics.drawRoundRect(0, 0, 300, 90, 10, 10);
            _controlHolder.graphics.endFill();
            _controlHolder.x = 100;
            _controlHolder.y = 380;
            _controlHolder.filters = [shadow];
            addChild(_controlHolder);
            
            var radText:TextField = new TextField();
            radText.mouseEnabled = false;
            radText.selectable = false;
            radText.defaultTextFormat = format;
            radText.autoSize = TextFieldAutoSize.LEFT;
            radText.antiAliasType = AntiAliasType.ADVANCED;
            radText.text = "halftone radius:";
            radText.x = 10;
            radText.y = 5;
            _controlHolder.addChild(radText);
            
            _radiusControl = new OBO_ValueSlider(80, 1, 50, 5);
            _radiusControl.x = 10;
            _radiusControl.y = int(radText.y + radText.height + 5);
            _radiusControl.addEventListener(ValueSliderEvent.DRAG, radiuschangeHandler);
            _controlHolder.addChild(_radiusControl);
            
            var offText:TextField = new TextField();
            offText.mouseEnabled = false;
            offText.selectable = false;
            offText.defaultTextFormat = format;
            offText.autoSize = TextFieldAutoSize.LEFT;
            offText.antiAliasType = AntiAliasType.ADVANCED;
            offText.text = "halftone offset:";
            offText.x = 10;
            offText.y = 50;
            _controlHolder.addChild(offText);
            
            _offsetControl = new OBO_ValueSlider(80, .1, 2, .5);
            _offsetControl.x = 10;
            _offsetControl.y = int(offText.y + offText.height + 5);
            _offsetControl.addEventListener(ValueSliderEvent.DRAG, offsetchangeHandler);
            _controlHolder.addChild(_offsetControl);
            
            var bgcolorText:TextField = new TextField();
            bgcolorText.mouseEnabled = false;
            bgcolorText.selectable = false;
            bgcolorText.defaultTextFormat = format;
            bgcolorText.autoSize = TextFieldAutoSize.LEFT;
            bgcolorText.antiAliasType = AntiAliasType.ADVANCED;
            bgcolorText.text = "background color:";
            bgcolorText.x = 120;
            bgcolorText.y = 5;
            _controlHolder.addChild(bgcolorText);
            
            _bgcolorControl = new OBO_ColorPicker(20, 20, 0xFFFFCC);
            _bgcolorControl.x = bgcolorText.x + bgcolorText.width + 15;
            _bgcolorControl.y = 5;
            _bgcolorControl.addEventListener(ColorEvent.COLOR_SELECT, bgColorChange);
            _bgcolorControl.filters = [new DropShadowFilter(1, 90, 0x000000, .8, 2, 2)];
            _controlHolder.addChild(_bgcolorControl);
            
            var loadBtn:EasyButton = new EasyButton("Load Image");
            loadBtn.x = 120;
            loadBtn.y = 60;
            loadBtn.addEventListener(MouseEvent.CLICK, browseforImage);
            _controlHolder.addChild(loadBtn);
            
            var saveBtn:EasyButton = new EasyButton("Save Image");
            saveBtn.x = 210;
            saveBtn.y = 60;
            saveBtn.addEventListener(MouseEvent.CLICK, saveImage);
            _controlHolder.addChild(saveBtn);
        }
        
        private function browseforImage(event:MouseEvent):void {
            _fileRef.addEventListener(Event.SELECT, loadImage);
            _fileRef.addEventListener(Event.COMPLETE, onUpload);
            var filter:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
            _fileRef.browse([filter]);
        }
        
        private function loadImage(event:Event):void {
            _fileRef.removeEventListener(Event.SELECT, loadImage);
            // 51200 = 50k  -  61440 = 60k
            // over 50k fails silently
            if (_fileRef.size < 51200)
                _fileRef.load();
        }
        
        private function onUpload(event:Event):void {
            _fileRef.removeEventListener(Event.COMPLETE, onUpload);
            var l:Loader = new Loader();
            l.contentLoaderInfo.addEventListener(Event.COMPLETE, imageloadHandler);
            l.loadBytes(_fileRef.data);
        }
        
        private function imageloadHandler(event:Event):void {
            
            if (_image.willTrigger(MouseEvent.MOUSE_DOWN))
                _image.removeEventListener(MouseEvent.MOUSE_DOWN, dragImage);
            event.currentTarget.removeEventListener(Event.COMPLETE, imageloadHandler);
            
            try {
                _image.removeChild(_image.getChildAt(0));
            }
            catch (e:Error){ }
            
            _imgWidth = event.currentTarget.content.width;
            _imgHeight = event.currentTarget.content.height;
            _image.mask = _imageMask;
            
            _dragBounds.left = (_imgWidth < _imageHolder.width) ? 0 : _imageHolder.width - _imgWidth;
            _dragBounds.top = (_imgHeight < _imageHolder.height) ? 0 : _imageHolder.height - _imgHeight;
            _dragBounds.width = (_imgWidth < _imageHolder.width) ? _imageHolder.width - _imgWidth : _imgWidth - _imageHolder.width;
            _dragBounds.height = (_imgHeight < _imageHolder.height) ? _imageHolder.height - _imgHeight : _imgHeight - _imageHolder.height;
            _image.addChild(event.currentTarget.content);
            
            _image.addEventListener(MouseEvent.MOUSE_DOWN, dragImage);
            
            _image.x = _image.y = 0;
            _imageHolder.addChild(_image);
            
            applyFilter();
        }
        
        private function dragImage(event:MouseEvent):void {
            stage.addEventListener(MouseEvent.MOUSE_UP, stopdragImage);
            _image.startDrag(false, _dragBounds);
        }
        
        private function stopdragImage(event:MouseEvent):void {
            stage.removeEventListener(MouseEvent.MOUSE_UP, stopdragImage);
            _image.stopDrag();
        }
        
        private function applyFilter():void {
            _shader.data.radius.value = [_radiusControl.value];
            _shader.data.offset.value = [_offsetControl.value];
            _image.filters = [_shaderFilter];
        }
        
        private function radiuschangeHandler(event:ValueSliderEvent):void {
            _shader.data.radius.value = [int(event.value)];
            _image.filters = [_shaderFilter];
        }
        
        private function offsetchangeHandler(event:ValueSliderEvent):void {
            _shader.data.offset.value = [event.value];
            _image.filters = [_shaderFilter];
        }
        
        private function bgColorChange(event:ColorEvent):void {
            _imageHolder.graphics.clear();
            _imageHolder.graphics.beginFill(event.color);
            _imageHolder.graphics.drawRect(0, 0, 350, 350);
            _imageHolder.graphics.endFill();
        }
        
        private function saveImage(event:MouseEvent):void {
            var bmp:BitmapData = new BitmapData(_imgWidth, _imgHeight, true, uint("0xFF" + _bgcolorControl.currentColorString));
            _image.mask = null;
            bmp.draw(_image);
            _image.mask = _imageMask;
            var encoder:JPGEncoder = new JPGEncoder(100);
            var saveBytes:ByteArray = encoder.encode(bmp);
            _fileRef.save(saveBytes, "saved_from_onebyonedesign.jpg");
        }
    }
}
 
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.text.AntiAliasType;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
 
internal class EasyButton extends SimpleButton {
    
    private static const UP_COLOR    :uint = 0xCCCCCC;
    private static const OVER_COLOR    :uint = 0x999999;
    private static const DOWN_COLOR    :uint = 0x869CA7;
    
    private var _label                :String;
        
    public function EasyButton(label:String):void {
        _label = label;
        makeButton();
    }
        
    private function makeButton():void {
        upState = null;
        overState = null;
        downState = null;
        hitTestState = null;
        upState = createState(UP_COLOR);
        overState = createState(OVER_COLOR);
        downState = createState(DOWN_COLOR);
        hitTestState = upState;
    }
        
    private function createState(color:uint):Sprite {
        var s:Sprite = new Sprite();
        s.graphics.beginFill(color);
        s.graphics.drawRect(0, 0, 80, 17);
        s.graphics.endFill();
        var labelText:TextField = createLabel();
        labelText.x = 5;
        s.addChild(labelText);
        
        return s;
    }
        
    private function createLabel():TextField {
        var tf:TextField = new TextField();
        tf.antiAliasType = AntiAliasType.ADVANCED;
        tf.autoSize = TextFieldAutoSize.LEFT;
        tf.selectable = false;
        tf.mouseEnabled = false;
        tf.defaultTextFormat = new TextFormat("_sans", 11);
        tf.text = _label;
        tf.antiAliasType = AntiAliasType.ADVANCED;
        
        return tf;
    }
    
    public function set label(s:String):void {
        _label = s;
        makeButton();
    }
}
  Facebook   Pinterest   Twitter   Google+
  • Singularity?
    January 13, 2008 · 1 comments
    1797
    2
    Read more
  • FIS Athlone && Warbler Update
    April 05, 2009 · 0 comments
    2179
    2
    Read more
  • FlashDevelop Update
    February 15, 2008 · 0 comments
    1661
    2
    Read more
3 Comments:
  1. That’s a cool example, but do you have any idea why it crashes with files larger than 50k? Is this a potential bug in FileReference?

    Regards!

    bLaf · June 25, 2008
  2. hey bLaf, I deliberately set the limit to 50k with this line:
    if (_fileRef.size < 51200)
    _fileRef.load();
    just to limit bandwidth consumption..

    Devon O. · June 26, 2008
  3. Can`t try this sample on your site cause of ReferenceError: Error #1056: Eigenschaft http://www.adobe.com/2008/actionscript/Flash10/::rotationZ in flash.text.TextField kann nicht erstellt werden.
    at main()

    The link to Halftone hydra filter no more exists!?

    I try to setup your sample code but i missing libraries.

    import com.onebyonedesign.ui.events.ColorEvent;
    import com.onebyonedesign.ui.events.ValueSliderEvent;
    import com.onebyonedesign.ui.OBO_ColorPicker;
    import com.onebyonedesign.ui.OBO_ValueSlider;

    Is there a way to download?
    Best regards Michael

    Michael M · September 21, 2011

Leave a Comment! Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Devon O. Wolfgang

AIR | Unity3D | AR/VR

Unity Certified Developer

Technical Reviewer of “The Essential Guide to Flash CS4 AIR Development” and “Starling Game Development Essentials”

Reviewer of “The Starling Handbook”

Unity Engineer at Touch Press.

Categories
  • Actionscript (95)
  • AIR (16)
  • Flash (99)
  • Games (7)
  • Liberty (13)
  • Life (53)
  • Shaders (20)
  • Unity3D (21)
Recent Comments
  • MainDepth on Unity Ripple or Shock Wave Effect
  • Devon O. on Unity Ripple or Shock Wave Effect
  • Feral_Pug on Unity Ripple or Shock Wave Effect
  • bavvireal on Unity3D Endless Runner Part I – Curved Worlds
  • Danielius Vargonas on Custom Post Processing with the LWRP
Archives
  • December 2020 (1)
  • December 2019 (1)
  • September 2019 (1)
  • February 2019 (2)
  • December 2018 (1)
  • July 2018 (1)
  • June 2018 (1)
  • May 2018 (2)
  • January 2018 (1)
  • December 2017 (2)
  • October 2017 (1)
  • September 2017 (2)
  • January 2017 (1)
  • July 2016 (1)
  • December 2015 (2)
  • March 2015 (1)
  • September 2014 (1)
  • January 2014 (1)
  • August 2013 (1)
  • July 2013 (1)
  • May 2013 (1)
  • March 2013 (2)
  • December 2012 (1)
  • November 2012 (1)
  • September 2012 (3)
  • June 2012 (2)
  • May 2012 (1)
  • April 2012 (1)
  • December 2011 (2)
  • October 2011 (3)
  • September 2011 (1)
  • August 2011 (1)
  • July 2011 (1)
  • May 2011 (2)
  • April 2011 (2)
  • March 2011 (1)
  • February 2011 (1)
  • January 2011 (2)
  • December 2010 (3)
  • October 2010 (5)
  • September 2010 (1)
  • July 2010 (2)
  • May 2010 (5)
  • April 2010 (2)
  • March 2010 (7)
  • February 2010 (5)
  • January 2010 (5)
  • December 2009 (3)
  • November 2009 (1)
  • October 2009 (5)
  • September 2009 (5)
  • August 2009 (1)
  • July 2009 (1)
  • June 2009 (2)
  • May 2009 (6)
  • April 2009 (4)
  • March 2009 (2)
  • February 2009 (4)
  • January 2009 (1)
  • December 2008 (5)
  • November 2008 (2)
  • September 2008 (1)
  • August 2008 (6)
  • July 2008 (6)
  • June 2008 (9)
  • May 2008 (4)
  • April 2008 (3)
  • March 2008 (4)
  • February 2008 (9)
  • January 2008 (7)
  • December 2007 (6)
Copyright © 2021 Devon O. Wolfgang