Language: ChineseGermanSpanishFrenchDutchItalianRussian
123 Flash Chat Forums

Go Back   TOPCMM Community > SWFKit > SWFKit Support

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-06-2008, 04:05 AM
Junior Member
 
Join Date: Jun 2008
Posts: 4
Default AS3: BitmapData->ByteArray->JPGEncode->SWFKit=>Paint and Save JPG

Hi,here!

I wish this sample code will help somebody!

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//useable: Use SWFKit to direct save your own BitmapData to JPG and without the server's or other out's supports like C++,VB.VC
//collecter: dottob
//reference: http://www.123flashchat.com/forum/index.php?topic=1169
//reference: http://dynamicflash.com/goodies/base64/
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

usage:

1.In CS3,build a new scene 500x500,Paster the as3 code in first frame,make a mc with a jpg file in it in the main scene,then delete it ,and don't forget to change the mc's LinkedID like"mc" in the library.

2.drag a button from component into the library.Save the fla and CTRL+Enter it!

3.In SWFKit,Paster the FFish script code in Scripts window,Scripts->Items->Initialize

4.In SWFKit,In Resources window ,->Add Movies ,and seleted the swf,and F7 it!

5.Just press and move mouse to draw on the pic,and release mouse ,press button("Save JPG"),then u will save the pic with your paint!

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Code in As3 first frame:

Code:
import flash.display.Sprite;
import fl.controls.Button;
import flash.events.MouseEvent;

import mx.graphics.codec.JPEGEncoder;
import Base64;
import SWFKit.*;

var P_Btn1: Button;
var P_Btn2: Button;
var P_line: Sprite;
var P_Box : Sprite;
var P_Pic = new mc();

function Paint_it() {
	P_Btn1 = new Button();
	P_Btn1.y = 350;
	P_Btn1.x = 5;
	P_Btn1.label = "Clear Paint";

	P_Btn2 = new Button();
	P_Btn2.y = 390;
	P_Btn2.x = 5;
	P_Btn2.label = "Save JPG";
	addChild(P_Btn1);
	addChild(P_Btn2);
	
	P_Btn1.addEventListener(MouseEvent.MOUSE_DOWN, P_Clear);
	P_Btn2.addEventListener(MouseEvent.MOUSE_DOWN, P_Save);

	addEventListener(MouseEvent.MOUSE_DOWN, P_Down);
	addEventListener(MouseEvent.MOUSE_UP, P_Up);

	P_Box = new Sprite();
	P_line = new Sprite();
	P_line.graphics.lineStyle(5,0xffcc00);

	addChild(P_Box);
	P_Box.addChild(P_Pic);
	P_Pic.addChild(P_line);
	P_Pic.x = 110;
	P_Pic.y = 5;
}

function P_Clear(event:MouseEvent):void {
	event.stopPropagation();//See help。
	
	P_line.graphics.clear();
	P_line.graphics.lineStyle(5,0xffcc00);
}
function P_Save(event:MouseEvent):void {
	event.stopPropagation();
	
	var P_Bmp:BitmapData = new BitmapData(P_Pic.width, P_Pic.height);
	P_Bmp.draw(P_Pic);
	
	var P_Jpg:JPEGEncoder = new JPEGEncoder(75);
	var P_Byte:ByteArray = P_Jpg.encode(P_Bmp);
	
	var encoded:String = Base64.encodeByteArray(P_Byte);

	ExternalInterface.call("saveByte", encoded);//pass data between AS3 and FFish Script

}

function P_Down(event:MouseEvent):void {
	addEventListener(MouseEvent.MOUSE_MOVE, P_Move);
	P_line.graphics.moveTo(mouseX-110, mouseY);
}

function P_Move(event:MouseEvent):void {
	P_line.graphics.lineTo(mouseX-110, mouseY);
}

function P_Up(event:MouseEvent):void {
	removeEventListener(MouseEvent.MOUSE_MOVE, P_Move);
}

Paint_it();
Code in FFish script:Scripts->Items->Initialize
Code:
//Initialize
//getAdditionalFile();
//return true;
function saveByte(file) {
  var converter = new Base64();
  var newStream = converter.decode(file);
	var dlg = new Dialogs;
	var output = dlg.fileSave("JPG Files(*.jpg)|*.jpg|All Files(*.*)|*.*|", "jpg","bitmap_jpg.jpg");
  newStream.saveToFile(output);
}
function Base64()
{
 this.keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
 function readByte(input)
 {
   var ch = input.get();
   if (input.eof) return NaN;   
   return ch;
 }
 function encode(input) 
   {
   var output = new StringStream;
   var chr1, chr2, chr3 ;
   var enc1, enc2, enc3, enc4;
   do 
   {
    chr1 = this.readByte(input);
    chr2 = this.readByte(input);
    chr3 = this.readByte(input);

    enc1 = chr1 >> 2;
    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
    enc4 = chr3 & 63;

    if (isNaN(chr2)) 
    {
      enc3 = enc4 = 64;
    } 
    else if (isNaN(chr3)) 
    {
      enc4 = 64;
    }
    output.write(this.keyStr.charAt(enc1) + this.keyStr.charAt(enc2) + 
       this.keyStr.charAt(enc3) + this.keyStr.charAt(enc4));       
    } while (!input.eof);
    return output;
  }
  function decode(input) 
   {
    var output = new StringStream;
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;
    
    var base64test = new RegExp("[^A-Za-z0-9+/=]", "g");
    if (base64test.exec(input)) 
    {
      trace("Base64.decode: ", "invalid input string");
      return null;
    } 
    do 
    {
      enc1 = this.keyStr.indexOf(input.charAt(i++));
      enc2 = this.keyStr.indexOf(input.charAt(i++));
      enc3 = this.keyStr.indexOf(input.charAt(i++));
      enc4 = this.keyStr.indexOf(input.charAt(i++));
      
      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;
      
      output.put(String.fromCharCode(chr1));
      
      if (enc3 != 64) 
      {
       output.put(String.fromCharCode(chr2));
      }      
      if (enc4 != 64) 
      {
       output.put(String.fromCharCode(chr3));
      }
    } while (i < input.length);
    return output;
 }
}
I like SWFKit Pro,and Thank you for your great works!

Here is the EXE,but no sources in it,I think if you like this sample,you can do it youself use the code here:

http://www.dao28.com/temp/swfkit.rar
Reply With Quote
  #2 (permalink)  
Old 08-06-2008, 01:04 PM
Senior Member
 
Join Date: Dec 2002
Posts: 2,015
Default Re: AS3: BitmapData->ByteArray->JPGEncode->SWFKit=>Paint and Save JPG

Nice, thank you very much!
Reply With Quote
Reply

Was this information helpful?    Yes No



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 05:19 AM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.