Hello, hello.
A little background. It all started with a regular Flash site, the maximum functionality of which was to open a window with text by pressing a button. Then there was the first game in the form of a tank rolling around the field with a turning barrel, then the first MMORPG. After that, where I got a job, they realized that the MMORPG was not exactly what was needed, and everyone decided to release PC games. On disks, in beautiful boxes, everything is as it should be.
What programming language to choose? All together they chanted: “Xi, Xi, Xi”. Everything would be fine if it were not for one “BUT” (this “but” became known just before the start of work). The same version of the game was supposed to be online, on the site, embedded in HTML with a bunch of visitors (remember how I talked about abandoning the MMORPG? Aha, right!). How to make a file with the .EXE extension with HTML or PHP did not occur to anyone. It was decided to do this: for the PC, write everything in ActionScript (then still 2.0), wrap it with a component for Builder, and for the web, wrap the SWF file with HTML code. And everything turned out. We did not need the web version, and the full-fledged PC version of the game was written in ActionScript 2.0. And it is convenient for artists to draw, and we, as flash-programmers, are happy.
Get to the point. This game has evolved. To date, 9 versions have been released, which are successfully sold and, accordingly, purchased. With each new version of the game, its functionality increased, and at this moment we came together to a situation where you need to take the current image of the character and save it to disk. Here we are. What can save flash to disk? - pathetic SharedObject with text data. And here we have no time for jokes. Here we are dealing with images. I need a JPEG and whatever you like, whatever you want, do it, but just save it to me. At first they formed byte arrays, sent fscommand to the shell, did the shell destroy it, form an image and save it to disk? Conveniently? - Of course not. To reconfigure something, it was necessary to completely change the entire code.
')
And here the case pushed me to MDM Zinc from Multidmedia. A great tool for creating desktop applications for both Windows and Linux and MacOS. We load into it the USB stick and the desktop application is ready. EXE-shnichek, with integrated Flash Player'om and great functionality to work with all the delights of Windows. Although the registry climb, even in the file system.
Zinc is installed elementary. You practically do not need to do anything (except how to install an extension using the extension manager). And the scope for thought opens up huge. So I began to slowly do ImageSaver (as I called it). Decided to do so first. I open the application, select the file with the picture, load it into the shell, photograph the desired part of the movie, saving the image to disk. First, create a movie (movie).
See what should be in the Flash movieAn empty image loader clip is given by (instance name) -
mc_loader . Clip scanner (blue transparent rectangle) give the name -
imageScanner . As the Document class parameter in the video (the properties tab), we specify the Saver. The class should be in the same folder as the movie and application. In the movie library throw component Zinc.
Next, write the Saver code:
/**
* @author DaFive
* mailto, gtalk: dafive.get@gmail.com
* icq: 163-092-469
*
* ImageSaver class. Interaction with MDM.Zinc.
*
* */
package {
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.net.URLRequest;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
import mdm.*;
public class Saver extends Sprite
{
//
private var TIMER:Timer;
//
private var TIME: int = 0;
//
var myAppPath:String;
public function Saver(): void
{
//!! init mdm-.
mdm.Application.init( this , onInit);
}
// Zinc
public function onInit(): void
{
myAppPath = mdm.Application.path;
//
mdm.Application.bringToFront();
//
var myFilePath:String = mdm.Dialogs.BrowseFile.show();
mdm.Dialogs.prompt(myFilePath);
//
var myLoader:Loader = new Loader();
var requestPhoto:URLRequest = new URLRequest(myFilePath);
myLoader.load(requestPhoto);
mc_loader.addChild(myLoader);
myLoader.contentLoaderInfo.addEventListener(Event.INIT, initPhoto, false , 0, true );
imageScanner.addEventListener(MouseEvent.MOUSE_DOWN, onScannerPress, false , 0, true );
imageScanner.addEventListener(MouseEvent.MOUSE_UP, onScannerRelease, false , 0, true );
imageScanner.buttonMode = true ;
save_btn.addEventListener(MouseEvent.CLICK, onSave, false , 0, true );
}
//
private function initPhoto(e:Event): void
{
mdm.Dialogs.prompt ( "" );
}
//
private function onSave(e:MouseEvent): void
{
save_btn.removeEventListener(MouseEvent.CLICK, onSave);
imageScanner.alpha = 0;
TIMER = new Timer(1000, 0);
TIMER.start();
TIMER.addEventListener(TimerEvent.TIMER, onTimer, false , 0, true );
}
// , ,
private function onTimer(e:TimerEvent): void
{
TIME ++;
if (TIME == 1)
{
var rnd_val:Number = Math.random();
mdm.Image.ScreenCapture.movieAreaToBmp(imageScanner.x, imageScanner.y, imageScanner.width, imageScanner.height, myAppPath+ "\\shot_" + rnd_val + ".bmp" );
mdm.Image.bmpToJpg(myAppPath+ "\\shot_" + rnd_val + ".bmp" );
mdm.FileSystem.deleteFile(myAppPath+ "\\shot_" + rnd_val + ".bmp" );
TIMER.stop();
TIMER.reset();
TIMER.removeEventListener(TimerEvent.TIMER, onTimer);
TIME = 0;
save_btn.addEventListener(MouseEvent.CLICK, onSave, false , 0, true );
imageScanner.alpha = 1;
}
}
//
private function onScannerPress(e:MouseEvent): void
{
e.currentTarget.startDrag( false );
}
//
private function onScannerRelease(e:MouseEvent): void
{
e.currentTarget.stopDrag();
}
}
}
* This source code was highlighted with Source Code Highlighter .
The most important thing is to call the initialization application function MDM in the constructor of the Saver class. Without it, nothing will work in the third version of the ActionScript language. In the second works, but here - no.
As you can see, our scanner is assigned the drag property (by pressing the LMB) and the drop property (when the LMB is released). That is, we can carry it on the desired piece of the image. Scanner size is fixed, but you can easily change it.
By pressing the "Save" button, the timer starts in one second. For if we do without a timer, the transparent rectangle will also be saved. We hide it. The thing is this. Zinc is not able to save the desired piece of the image with the desired coordinates immediately in JPG. But save to BMP and convert it to JPG. This is what we do after if (TIME == 1).
The file is assigned a name randomly (specially made so that you can save several versions of the image and not rub them). The file will be saved in the folder where the application itself lies (myAppPath will help us). Then there is a conversion from BMP to JPG (mdm.Image.bmpToJpg) and the subsequent deletion of the BMP file as unnecessary.
What Zinc extensions need to be connected?What we get with the project build?How do we direct the scanner?Saved ImageI hope that someone will come in handy. Do not scold, if that. My first topic.