如何截图某个框架并将其发送到特定的电子邮件地址?

时间:2014-07-27 01:19:05

标签: actionscript-3 flash

如何在ActionScript 3 Flash中截取某个帧并将其发送到特定的电子邮件地址?

另外,当我研究样本项目时,它提供外部的,然后..我将如何从框架中调用它?

我正在创建一个Cake Customizer。蛋糕完成后,我想屏幕捕获并直接发送到电子邮件。

2 个答案:

答案 0 :(得分:1)

我在大约一年前做过一个项目,在那里我创建了一个多点触控桌面应用程序,进入一个新的公寓大楼的销售中心。其中一项功能是客户能够自定义具有小家具图形的平面图并通过电子邮件发送给自己。我使用Adobe的PNGEncoder(https://github.com/mikechambers/as3corelib)和Coltware的airxmail(http://flex.coltware.com/as3-flex-air/airxmail/)完成了这项工作。

对于在框架中使用外部AS,您可以使用(如MasterRoro答案所述)

include 'external_file.as';

您还可以将代码包含在类中,并将其包含在FLA的源路径中。要执行此操作,请转到编辑>喜好。点击" Actionscript"类别。然后单击" Actionscript 3.0设置"。在" Source Path"框,使用加号按钮添加单个AS文件(或文件夹按钮,以使Actionscript搜索源AS文件的整个文件夹)。然后,您将能够在框架内创建该类的新实例。

例如,要使用下面的MailMain.as类,您可以将其保存到名为MailMan.as的AS文件中,并将其添加到源路径,如上所述。然后在框架上,您只需创建一个新的实例:

var mail:MailMan = new MailMan();
mail.sendImageFile(...)

这是我用来保存平面图图像的代码。当您提供BitmapData时,其构造函数会询问数据的宽度和高度。在这里你可以提供cake.width和cake.height或类似的东西。

        var theBitmap:Bitmap = new Bitmap(new BitmapData(1620, 1080));
        log("Drawing floorplan");
        theBitmap.bitmapData.draw(this);

        log("Saving floorplan image to desktop");
        var ba:ByteArray = PNGEncoder.encode(theBitmap.bitmapData);
        var savePoint:File = File.desktopDirectory.resolvePath(floorplan.model + "_floorplan" + Math.random()*10000 + ".png");
        var fs:FileStream = new FileStream();
        try
        {
            fs.open(savePoint, FileMode.WRITE);
            fs.writeBytes(ba);
            fs.close();
            log("Floorplan saved!");

            var mailman:MailMan = new MailMan();
            mailman.addEventListener(SMTPEvent.SMTP_SENT_OK, fpSent);
            mailman.sendImageFile("", fpEmailDial.getTo(), savePoint);
        }
        catch(e:Error)
        {
            log("Error occurred while trying to save image file");
        }

这是我为发送电子邮件而创建的课程。它包括发送图像和纯文本电子邮件的示例。请注意,SQLHelper和ResourceManager是我的专有类,但它们的功能很明显。

public class MailMan extends EventDispatcher
{
    private var sender:SMTPSender;
    private var sql:SQLHelper;

    public function MailMan()
    {
        sql = new SQLHelper(File.applicationStorageDirectory.resolvePath("mtt-persistent.db"));

        initSender();
    }

    public function initSender():void
    {
        log("Initializing Sender");
        sender = new SMTPSender();

        //this is the info for the host email from which the email is sent.
        //you might want to create an email such as "noreply@example.com" to act as the host

        sender.setParameter(SMTPSender.HOST,"smtp.gmail.com");
        sender.setParameter(SMTPSender.PORT,465);
        sender.setParameter(SMTPSender.CONNECTION_TIMEOUT,5000);
        sender.setParameter(SMTPSender.ENABLE_BUFFER,true);

        sender.setParameter(SMTPSender.AUTH,true);
        sender.setParameter(SMTPSender.USERNAME,"noreply.salescenter@gmail.com");
        sender.setParameter(SMTPSender.PASSWORD,"salescenterpassword");

        sender.setParameter(SMTPSender.SOCKET_OBJECT, new TLSSocket());

        log("Sender parameters set");

        //  STARTTLS Handler
        //sender.addEventListener(SMTPEvent.SMTP_START_TLS,startTlsHandler);
        sender.addEventListener(SMTPEvent.SMTP_CONNECTION_FAILED, onConnFail);
        sender.addEventListener(SMTPEvent.SMTP_AUTH_NG, authFail);
        sender.addEventListener(SMTPEvent.SMTP_AUTH_OK, onAuthOK);
        sender.addEventListener(SMTPEvent.SMTP_ACCEPT_DATA, onAccData);
        sender.addEventListener(SMTPEvent.SMTP_NOT_SUPPORT_ESMTP, onNotSuppESMTP);
        sender.addEventListener(SMTPEvent.SMTP_SENT_OK, onSent);
        sender.addEventListener(SMTPEvent.SMTP_NOOP_OK, noopOK);
        sender.addEventListener(SMTPEvent.SMTP_COMMAND_ERROR, onCommandError);

        function onAccData(e:SMTPEvent):void
        {
            log("Data Accepted");
        }
        function onAuthOK(e:SMTPEvent):void
        {
            log("Account authorized successfully");
        }
        function onConnFail(e:SMTPEvent):void
        {
            log("SMTP Connection FAIL");
        }
        function authFail(e:SMTPEvent):void
        {
            log("SMTP Auth NG");
        }


    }

    public function sendPic(salesEmail:String, toEmail:String, picID:String)
    {
        log("Sending image:" + picID + ":");
        var contentType:ContentType = ContentType.MULTIPART_MIXED;
        var mimeMsg:MimeMessage = new MimeMessage(contentType);

        var salespersonReq:Array = sql.select("salespeople", new Array(new SQLParam("email", salesEmail)));

        if (salespersonReq != null)
        {
            var salespersonInfo:Object = salespersonReq[0];
            var salesFirstname:String = salespersonInfo.firstname as String;
            var salesLastname:String = salespersonInfo.lastname as String;

            var from:INetAddress = new INetAddress();
            from.personal = salesFirstname + " " + salesLastname;
            from.address = salesEmail;
        }
        else
        {
            var from:INetAddress = new INetAddress();
            from.personal = "Sales Center";
            from.address = "salescenteremail@gmail.com";
        }
        mimeMsg.setFrom(from);
        mimeMsg.setReplyTo(from);

        var to:INetAddress = new INetAddress();
        to.address = toEmail;
        mimeMsg.addRcpt(RecipientType.TO, to);

        mimeMsg.setSubject("Your image");
        mimeMsg.setTextBody("Your image");

        var textPart:MimeTextPart = new MimeTextPart();
        textPart.contentType.setParameter("charset", "UTF-8");
        textPart.transferEncoding = "8bit";
        textPart.setText("Please find attached the image you sent yourself.");
        mimeMsg.addChildPart(textPart);

        var filePart:MimeImagePart = new MimeImagePart();
        filePart.contentType.setMainType("image");
        filePart.contentType.setSubType("png");

        //here I'm grabbing the image that was encoded into a png and saved to the filesystem
        var imageFile:File = (ResourceManager.getInstance().getResourceByID("email_" + picID) as AssetLoader).origFile;
        filePart.setAttachementFile(imageFile, imageFile.name);
        mimeMsg.addChildPart(filePart);

        sender.send(mimeMsg);
        //sender.close();
    }

    public function sendImageFile(salesEmail:String, toEmail:String, imageFile:File)
    {
        log("Sending image:" + imageFile.name);
        var contentType:ContentType = ContentType.MULTIPART_MIXED;
        var mimeMsg:MimeMessage = new MimeMessage(contentType);

        var salespersonReq:Array = sql.select("salespeople", new Array(new SQLParam("email", salesEmail)));
        var salespersonInfo:Object = salespersonReq[0];

        var salesFirstname:String = salespersonInfo.firstname as String;
        var salesLastname:String = salespersonInfo.lastname as String;

        var from:INetAddress = new INetAddress;
        from.personal = salesFirstname + " " + salesLastname;
        from.address = salesEmail;
        mimeMsg.setFrom(from);
        mimeMsg.setReplyTo(from);

        var to:INetAddress = new INetAddress();
        to.address = toEmail;
        mimeMsg.addRcpt(RecipientType.TO, to);

        mimeMsg.setSubject("Your floorplan");
        mimeMsg.setTextBody("Your floorplan");

        var textPart:MimeTextPart = new MimeTextPart();
        textPart.contentType.setParameter("charset", "UTF-8");
        textPart.transferEncoding = "8bit";
        textPart.setText("Please find attached the floorplan you sent yourself.");
        mimeMsg.addChildPart(textPart);

        var filePart:MimeImagePart = new MimeImagePart();
        filePart.contentType.setMainType("image");
        filePart.contentType.setSubType("png");
        filePart.setAttachementFile(imageFile, imageFile.name);
        mimeMsg.addChildPart(filePart);

        sender.send(mimeMsg);
        //sender.close();
    }

    public function sendPlainText():void
    {
        log("Sending plain text message");
        var contentType:ContentType = ContentType.MULTIPART_MIXED;
        var mimeMsg:MimeMessage = new MimeMessage(contentType);

        var from:INetAddress = new INetAddress();
        from.personal = "Ima Sample";
        from.address = "youremail@gmail.com";

        mimeMsg.setFrom(from);
        mimeMsg.setReplyTo(from);
        var toAddr:INetAddress = new INetAddress("toemail@gmail.com","Kiks");
        mimeMsg.addRcpt(RecipientType.TO,toAddr);
        // set mail subject
        mimeMsg.setSubject("Hello Airxmail");
        mimeMsg.setTextBody("this is another test, but with attachments");

        var textPart:MimeTextPart = new MimeTextPart();
        textPart.contentType.setParameter("charset", "UTF-8");
        textPart.transferEncoding = "8bit";
        textPart.setText("This is the text of the message");
        mimeMsg.addChildPart(textPart);


        var filePart:MimeImagePart = new MimeImagePart();
        filePart.contentType.setMainType("image");
        filePart.contentType.setSubType("png");
        filePart.setAttachementFile(File.desktopDirectory.resolvePath("testMail.png"), "testMail.png");
        mimeMsg.addChildPart(filePart);


        sender.send(mimeMsg);
        sender.close();
    }

    protected function onCommandError(event:SMTPEvent):void
    {
        log("A Command error has occurred");
    }

    protected function noopOK(event:Event):void
    {
        log("NOOP OK");
    }

    protected function onSent(event:SMTPEvent):void
    {
        log("MESSAGE SENT!");
        dispatchEvent(new SMTPEvent(SMTPEvent.SMTP_SENT_OK));
    }

    protected function onNotSuppESMTP(event:SMTPEvent):void
    {
        log("ESMTP Not Supported"); 
    }

    public function startTlsHandler(event:SMTPEvent):void
    {
        log("TLS Handler Started");
        var sock:Socket = event.socket as Socket;

        var tls:TLSSocket = new TLSSocket();
        sender.setParameter(SMTPSender.SOCKET_OBJECT,tls);
        tls.startTLS(sock,"smtp.gmail.com");
        sendPlainText();
    }


    private function log(msg:String):void
    {
        WOWLogger.logMsg("MailMan", msg);
        trace("[MailMan] " + msg);
    }

}

答案 1 :(得分:0)

您绝对可以捕获图像并将其保存到文件系统中。但是要将它邮寄到某个地方你必须使用PHP或其他一些服务器端脚本才能管理它。

首先,要使用外部文件中的actionscript,请使用 include 'filename.as'

要保存图像,您需要Adobe的JPG或PNG编码器,您可以在此处找到包含这些类的代码库:

https://github.com/mikechambers/as3corelib

然后,一旦它们在您的构建路径中,根据您要保存的格式导入这些类(PNGEncoder或JPGEncoder)。

import com.adobe.images.PNGEncoder;

接下来,您需要将蛋糕的图像绘制到BitmapData个对象。你可以绘制整个舞台,但如果你只想绘制带有蛋糕的容器,那也没关系。

//draw the cake into a bitmap data
var bmd:BitmapData = new BitmapData(cake.width, cake.height);
bmd.draw(cake);


var imageFile:ByteArray = PNGEncoder.encode( bmd);

//This opens a save prompt allowing you to save your image to the file system
var fileRef:FileReference = new FileReference();
fileRef.save(imageFile);

至于邮寄它......有人纠正我,如果我错了,但是单独使用动作脚本是不可能的。