FlashCom学习例子: 在线拍大头照(项目使用Flash8+VS2005)

目前初步实现了Flash在线拍照的功能,由于没有服务器做进一步的演示,故把代码贴出,如下:
Flash 8中(Actions)

btn2.enabled = false;
//FLASH大头贴原程序
System.useCodepage = true;
//程序编写:Benmouse 2005-12-01
import flash.display.BitmapData;
if (System.capabilities.version.charAt(4)>=8) {
 myCrame = Camera.get();
 myCrame.setMode(160, 120, 15);
 myVideo.attachVideo(myCrame);
 //显示视频
 var imger:BitmapData = new BitmapData(myVideo._width, myVideo._height, false);
 imgShow.attachBitmap(imger, this.getNextHighestDepth(), "auto", true);
 var thehang:Number = 0;
 var ID:Number;
 var PX:Array = new Array();
 btn1.onRelease = function() {
  imger.draw(myVideo);
  btn2.enabled = true;
  //imger是场景中一个空的组件,用来显示myVideo的图象
  //thehang = 0;
  //_root.ID = setInterval(makeImg, 5);
 };
 if (myCrame.name != undefined) {
  if (myCrame.muted) {
   mx.controls.Alert.yesLabel = "确定";
   mx.controls.Alert.show("摄像头被禁止使用了!", "提示");
  }
  else
  {
   mx.controls.Alert.yesLabel = "确定";
   mx.controls.Alert.show("您有摄像头,可以正常使用!", "提示");
  }
  // end if        
 } else {
  mx.controls.Alert.noLabel = "确定";
  mx.controls.Alert.show("您没有安装摄像头!", "提示");
 }
 // end if
} else {
 mx.controls.Alert.cancelLabel = "确定";
 mx.controls.Alert.show("您的FLASHPLAY版本不是8.0                                   请先安装FLASHPLAY8.0", "版本错误");
}
// end if
btn2.onRelease = function() {
 thehang = 0;
 _root.ID = setInterval(makeImg, 5);
};
function makeImg() {
 var pix:Number;
 var pixer:String;
 PX[thehang] = new Array();
 for (w=0; w<imgShow._width; w++) {
  pix = imger.getPixel(w, thehang);
  pixer = pix.toString();
  //pixer = pix.toString();
  //if (pix == 0xFFFFFF) {
  //pixer = "";
  //}
  // don’t send blank pixel 
  //trace(pix);
  PX[thehang].push(pixer);
 }
 thehang += 1;
 if (thehang>=imgShow._height) {
  clearInterval(_root.ID);
  //imger.dispose();
  sendVars();
 }
}
function sendVars() {
 sender = new LoadVars();
 sender.w = myVideo._width;
 sender.h = myVideo._height;
 for (i=0; i<sender.h; i++) {
  sender["PX"+i] = _root.PX[i];
 }
 sender.send("SavePhoto.aspx?View=Com", "compa", "POST");
}

AspX的代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.IO;

public partial class SavePhoto : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string View = (Request.QueryString["View"] != null) ? Request.QueryString["View"].ToString() : null;
            if (View.Equals("Com"))
            {
                int w = int.Parse(Request.Form["w"].ToString());
                int h = int.Parse(Request.Form["h"].ToString());

                string[] pixelColor = new string[w - 1];
                for (int i = 0; i < h; i++)
                {
                    pixelColor[i] = Request.Form["PX" + i].ToString();
                }
                SaveJpg(w, h, pixelColor);
            }
        }
    }

    private void SaveJpg(int width, int height, string[] rowColor)
    {
        Bitmap image = new Bitmap(width, height);
        string delimStr = ",";
        char[] delimiter = delimStr.ToCharArray();

        for (int i = 0; i < height; i++)
        {
            string[] colDot = rowColor[i].ToString().Split(delimiter);
            for (int j = 0; j < colDot.Length; j++)
            {
                int rgb = Convert.ToInt32(colDot[j].ToString());
                image.SetPixel(j, i, Color.FromArgb(rgb));
            }
        }
        string text2 = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
        string SavePath = base.Server.MapPath("UpLoadFile/") + text2;

        image.Save(SavePath, ImageFormat.Jpeg);
        image.Dispose();
    }
}

本例子代码来源与FlashCom,在学习中,经过一步一步的调试,目前还不是很明白什么道理,效果是已经达到了,接下去就是需要给大头贴合成一个背景上去了。







源代码下载

posted on 2006-06-07 14:21  北极熊,我来了!  阅读(3339)  评论(3编辑  收藏  举报