Add

Monday, May 25, 2009

Capturing the image is an asp.net web application

Introduction:


Capturing the image is an asp.net web application is not a difficult task. If you are ready to use flash with asp.net.

Description:

Create a flash file will be used to capture the image from clientside.That flash file code is given below with explanation.

Import the files whatever necessary.

import flash.display.BitmapData;

import flash.display.DisplayObject;

import com.adobe.images.*;

import flash.utils.ByteArray;

import flash.utils.*;

import flash.events.*;

import flash.display.SimpleButton;

import com.dynamicflash.util.Base64;

import com.bumpslide.util.*;

import flash.external.ExternalInterface;

//Create an instance for camera

var cam:Camera = Camera.getCamera();

//Import video file

//var __video:Video = new Video(80,80)

cam.setQuality(0, 100);

//cam.setMode(1024,768,30);

__video.x=47;

__video.y= 25;

__video.width=80;

__video.height=80;

//Set Border

var square:Sprite = new Sprite();

addChild(square);

square.graphics.lineStyle(1,0x000000);

square.graphics.drawRect(0,0,80,80);

square.x = 47;

square.y = 25;

//Set Capturing image size.

//cam.setQuality(0,80);

var count:int = 0;

var scale:Number = .68;

var m:Matrix = new Matrix();

m.scale(scale,scale);

//Create buttons and add corresponding event listener to that.

//cam.setMode(cam.width,cam.height,cam.currentFPS);

btnStart:SimpleButton;

btnStart.addEventListener(MouseEvent.CLICK, startphoto);

btnCapture.addEventListener(MouseEvent.CLICK, takephoto);

btnStop.addEventListener(MouseEvent.CLICK, stopphoto);

var screenS:BitmapData = new BitmapData(80,80,false);

var screenS_image:Bitmap=new Bitmap(screenS);

//function to start camera

function startphoto(e:MouseEvent):void

{

__video.attachCamera(cam);

__video.visible=true;

}

//function to take photo

function takephoto(e:MouseEvent):void

{

screenS.draw(__video,m);

//screenS_image(screenS);

var jpg: JPGEncoder = new JPGEncoder(80);

var jpgByteArray:ByteArray = new ByteArray();

jpgByteArray = jpg.encode(screenS);

var encoded:String = Base64.encodeByteArray(jpgByteArray);

var myData:URLRequest = new URLRequest("_self")

myData.method = URLRequestMethod.POST;

var variables:URLVariables = new URLVariables();

variables.encoded = encoded;

myData.data = variables;

var loader:URLLoader = new URLLoader();

loader.dataFormat = URLLoaderDataFormat.VARIABLES;

loader.addEventListener(Event.COMPLETE,dataOnLoad);

loader.load(myData);

__video.attachCamera(null);

}

//function to stop camera.

function stopphoto(e:MouseEvent):void

{

__video.visible=false;

__video.attachCamera(null);

}

function dataOnLoad(evt:Event)

{

trace(evt.target.data.writing)

if(evt.target.data.writing=="Ok")

{

gotoAndPlay(2)

}

//else status_txt.text = "Error in saving submitted data"

}

Get data from client side(asp.net) as string and convert that in to byte again. Save that converted data in to database.

string data = Request.Form["encoded"];

if (data != null)

{

byte[] bytes = Convert.FromBase64String(data);

}

Download Sample From Here

No comments: