AS3 WebCam Still Shot

Introducción

Hay varios tutoriales por ahí de como guardar imágenes de una webcam en flash cuando se detecta movimiento, sin embargo, no hay ninguno (que yo haya encontrado) con el cual tomar webcam shots únicamente cuando se deja de detectar movimiento.

El Problema

Recientemente ocupaba guardar imágenes de una cámara conectada a flash (el como hacerla pasar como webcam sera tema de otro post). Mas específicamente la cámara era una cámara intraoral, la cual es una cámara para poder inspeccionar el interior de una boca humana, inspeccionar dientes, etc. La cámara contaba con un botón de freeze, sin embargo, la cámara únicamente manda vídeo constante, tal y lo que se vería en una pantalla es lo que manda a la computadora, y querer guardar imagen por imagen resultaría tedioso ya que habría que ir hasta la computadora y guardar.

La solución

Aprovechar el botón de freeze.
La idea es simple, si la imagen se mantiene congelada guardas la imagen automáticamente solo una vez, si esta en movimiento la ignoras.

Código

El código dice mas que mil palabras así que vamos al grano, se ocupan 2 partes, ActionScript y PHP.

webcam.as

package {

	import flash.media.*;
	import flash.display.*;
	import flash.text.*;
	import flash.events.*;
	import flash.utils.*;
	import com.adobe.images.JPGEncoder;
	import flash.net.*;

	public class webcam extends Sprite{

		var cam:Camera = null;
		var vid:Video = null;
		var activity = false;
		var sound:camera_wav = new camera_wav();
		var intervalId:uint;
		var upload_url:String;
		var iwidth:int = 640;
		var iheight:int = 480;

		var sumBmpData:uint = 0;

		private function getFlashVars():Object {
			return Object( LoaderInfo( this.loaderInfo ).parameters );
		}

		public function webcam(){

			upload_url = "save.php";//getFlashVars().upload_url;
			cam = Camera.getCamera();

			if(cam){
				cam.setMode(iwidth,iheight,30,true);
				vid = new Video(iwidth,iheight);
				vid.attachCamera(cam);

				addChild(vid);
				//frecuencia con la que checa si hay cambios
				intervalId = setInterval(checkBitmap,500);

			}else{

				//

			}

		}

		private function checkBitmap():void{

			var newSumBmpData:uint = 0;
			var newBmpData:BitmapData = new BitmapData(iwidth,iheight);
			newBmpData.draw(vid);
			//sumar colores cada 80 pixeles, para tener un "resumen" de la imagen
			for(var x:int = 0; x < iwidth; x+=80){
				for(var y:int = 0; y < iheight; y+=80){
					newSumBmpData += newBmpData.getPixel(x,y);
				}
			}

			if(sumBmpData == 0) activity = false;
			else
			if(newSumBmpData == sumBmpData){
				//ignorar el negro
				if(sumBmpData <= 1) activity = false; 

				if(activity == true){

					sound.play();
					sendData(newBmpData);

				}

				activity = false;

			}else{

				activity = true;

			}

			sumBmpData = newSumBmpData;

		}

		private function sendData(bmpData:BitmapData){

			var encoded_jpg:JPGEncoder = new JPGEncoder(100);
			var jpg_binary:ByteArray = encoded_jpg.encode(bmpData);

			var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");
			var request:URLRequest = new URLRequest(upload_url);
			request.requestHeaders.push(header);
			request.method = URLRequestMethod.POST;
			request.data = jpg_binary;

			var loader:URLLoader = new URLLoader();
			loader.load(request);

			//navigateToURL(request, "_blank"); //usar en caso de debuggeo  (en vez de loader.load)

		}

	}

}

save.php

		$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];

		$folder_url = 'fotos/';
		if(!is_dir($folder_url)) {
			mkdir($folder_url);
		}
		$filename = '/foto_'.time().'.jpg';
		file_put_contents($filename, $jpg);

Las imagenes son encodeadas en JPG y despues enviadas al servidor php, para poder encodearlas ocupas la siguiente libreria:
as3corelib

Webcams

  • Hello! baackea interesting baackea site! I’m really like it! Very, very baackea good!

  • Very nice site! cheap viagra

  • Very nice site! [url=http://apxoiey.com/qovsyt/2.html]cheap cialis[/url]

  • Very nice site! cheap cialis http://apxoiey.com/qovsyt/4.html

  • Very nice site!

  • It’s amazing in support of me to have a site, which is beneficial in favor
    of my know-how. thanks admin

    Here is my weblog … generic cialis in canada

  • [url=http://sildalis.trade/]sildalis[/url] [url=http://zoloft.pro/]setraline purchase[/url] [url=http://buy-furosemide.faith/]furosemide[/url]

  • [url=http://buydiflucan.in.net/]diflucan[/url] [url=http://buynexium.eu/]where to buy nexium[/url] [url=http://genericsynthroid.science/]synthroid[/url] [url=http://buy-clomid.trade/]clomid[/url] [url=http://buymotilium.loan/]motilium[/url] [url=http://buylasix.review/]buy lasix without prescription[/url] [url=http://abilify-cost.nu/]abilify[/url]

  • [url=http://buymotrin.pro/]motrin ib[/url] [url=http://vpxl.bid/]vpxl[/url] [url=http://voltaren-gel-1.ru/]purchase diclofenac[/url]

  • [url=http://buy-motilium.eu/]buy motilium[/url] [url=http://clindamycin-300-mg.ru/]clindamycin[/url] [url=http://bupropiononline.top/]bupropion hcl 150mg[/url] [url=http://augmentin875.cricket/]augmentin 875[/url] [url=http://buy-clomid-online.eu/]clomid[/url]

  • [url=http://buy-trazodone.bid/]trazodone[/url] [url=http://buyprozac.in.net/]buy prozac online uk[/url] [url=http://buytretinoin.eu/]tretinoin cream purchase[/url] [url=http://buy-glucophage.party/]glucophage xr 500[/url] [url=http://synthroid.cricket/]synthroid[/url] [url=http://buyalbuterol.download/]albuterol[/url] [url=http://amoxicillin-500.ru/]amoxicillin 500[/url]

  • [url=http://prednisolone.mom/]prednisolone[/url] [url=http://cleocin-gel.mom/]cleocin gel[/url] [url=http://buy-vermox.xyz/]where can i buy vermox[/url] [url=http://acyclovir.download/]aciclovir 5% cream[/url] [url=http://buy-azithromycin.trade/]where can i buy azithromycin[/url] [url=http://doxycyclinehyclate100mg.top/]doxycycline[/url] [url=http://glucophage.click/]glucophage[/url]

  • [url=http://buy-benicar.eu/]benicar hct cost[/url] [url=http://buyvardenafil.eu/]buy vardenafil[/url] [url=http://wellbutrin-online.ru/]wellbutrin online[/url]

  • [url=http://buy-cafergot.accountant/]buy cafergot[/url] [url=http://buy-doxycycline.party/]doxycycline medicine[/url] [url=http://sildenafil.mom/]sildenafil products[/url]

  • [url=http://motrin.accountant/]motrin[/url] [url=http://buy-amoxicillin.xyz/]buy amoxicillin[/url] [url=http://clindamycinhcl.top/]clindamycin hcl 300 mg buy[/url] [url=http://buy-neurontin.top/]neurontin pfizer[/url] [url=http://buy-abilify.party/]abilify[/url]

  • [url=http://buy-viagra-soft.date/]viagra soft[/url] [url=http://kamagra-online.gdn/]kamagra gel[/url] [url=http://crestorgeneric.webcam/]crestor[/url] [url=http://buysildenafil.gdn/]buy sildenafil online[/url]

  • [url=http://buy-zithromax.stream/]buy zithromax[/url] [url=http://generic-seroquel.cricket/]seroquel[/url] [url=http://buy-singulair.party/]buy singulair online[/url]

  • [url=http://albendazole.stream/]albendazole[/url] [url=http://cytoxan.top/]cytoxan for lupus[/url] [url=http://buy-singulair.party/]buy singulair[/url]

  • [url=http://nexiummedication.webcam/]otc nexium[/url] [url=http://lexaprogeneric.in.net/]lexapro[/url] [url=http://celexa.pro/]cost of celexa[/url]

  • [url=http://buy-augmentin.com/]click for source[/url]

  • [url=http://amoxilonline.cricket/]amoxil 500mg capsule[/url] [url=http://cialiscostperpill.top/]cialis[/url] [url=http://buy-prednisolone.kim/]buy prednisolone[/url]

  • [url=http://provera-10mg.top/]provera[/url] [url=http://buy-torsemide.bid/]torsemide brand name[/url] [url=http://buy-celexa.cricket/]celexa[/url]

  • [url=http://buysildalis.kim/]buy sildalis[/url] [url=http://buycleocingel.gdn/]buy cleocin gel[/url] [url=http://torsemideonline.gdn/]torsemide online[/url] [url=http://generic-for-crestor.cricket/]crestor[/url] [url=http://tetracyclinecost.bid/]tetracycline[/url] [url=http://buy-cleocin-gel.cricket/]generic cleocin gel[/url] [url=http://motrin800.webcam/]motrin 800[/url]

  • [url=http://buyxeloda.trade/]xeloda[/url] [url=http://lasixmedication.top/]lasix[/url] [url=http://genericpropecia.date/]where to buy generic propecia[/url] [url=http://hydrochlorothiazide-12-5-mg.cricket/]hydrochlorothiazide[/url] [url=http://buyneurontin.kim/]neurontin[/url]

  • [url=http://celebrex.pro/]celebrex[/url] [url=http://furosemideonline.trade/]furosemide online no prescription[/url] [url=http://rogaine-for-women.xyz/]rogaine[/url] [url=http://generic-crestor.cricket/]generic crestor canada[/url] [url=http://azithromycinonline.cricket/]azithromycin online[/url] [url=http://levaquinonline.gdn/]levaquin 500[/url]

  • [url=http://cephalexin500mgcapsules.webcam/]cephalexin[/url] [url=http://bupropion-sr.bid/]bupropion[/url] [url=http://amitriptyline-25mg.bid/]amitriptyline 25 mg[/url] [url=http://buy-citalopram.faith/]citalopram sleep[/url] [url=http://furosemide40mg.gdn/]furosemide 40 mg[/url] [url=http://synthroid.pro/]levothyroxine 50 mcg[/url] [url=http://bupropionxl.trade/]bupropion xl 150 mg[/url]

  • [url=http://furosemide20mgtab.date/]furosemide 20 mg tab[/url] [url=http://acyclovir400mg.trade/]acyclovir 800 mg tablets[/url] [url=http://buywellbutrin.link/]buy wellbutrin[/url]

  • [url=http://lisinopril10mg.xyz/]lisinopril 10 mg[/url] [url=http://methylprednisolone.science/]prednisolone[/url] [url=http://alavert.top/]alavert d-12 hour allergy and sinus[/url] [url=http://buy-voltaren.top/]diclofenac[/url]

  • LfgYhf xwpeycxegcqo, [url=http://axllgmpcamar.com/]axllgmpcamar[/url], [link=http://zyjjlgxlkgjp.com/]zyjjlgxlkgjp[/link], http://heogulswbwdw.com/

  • [url=http://singulaironline.party/]singulair for post nasal drip[/url] [url=http://vigara.webcam/]viagra[/url] [url=http://permethrin-cream.trade/]permethrin cream[/url] [url=http://bupropion.press/]bupropion 150mg sr[/url] [url=http://tretinoin.tech/]tretinoin cream[/url] [url=http://doxycyline.xyz/]doxycycline[/url]

  • [url=http://blackjacklive.top/]blackjack live[/url] [url=http://roullette.top/]play blackjack[/url] [url=http://makemoneyfromhomeonline.top/]make money from home online for free[/url] [url=http://pornshows.top/]porn show[/url]

You can follow any responses to this entry through the RSS 2.0 feed.