이번에 요것까지 도입해서 멋지게 해볼까나?ㅎㅎ
출처 : http://blog.naver.com/nooreestyle/150004802567
플래시 플레이어 8에 내장된 필터를 이용하여, 간단하게 화상캠으로 사진을 찍고 이를 PHP의 GD라이브러리를 이용하여, JPG파일로 출력하는 예제를 만들어 보겠습니다. (화상카메라가 있어야 합니다.)
먼저 첨부파일의 cam_app.fla파일을 열어 내용을 살펴봅니다. 간단하게 스테이지에는 output_vid 무비클립과 버튼 1개 만 존재합니다. output_vid를 더블클릭하여 무비클립 편집모드로 들어가면 my_video라는 인스턴스명을 가진 외부비디오 무비클립이 존재하며, 첫번째 타임라인에는 다음과 같은 액션이 기술되어 있어 자동으로 화상캠 카메라의 영상을 가져옵니다.
stop();
// 카메라 생성
myCam = Camera.get();
// 적용
this.my_video.attachVideo(myCam);
스테이지로 다시 돌아오면, 역시 첫번째 타임라인에 다음과 같은 액션이 기술되어 있습니다.
Stage.scaleMode = "noScale";
Stage.align="CM";
stop();
//내장 필터임포트
import flash.display.BitmapData;
import flash.filters.*;
import flash.geom.Matrix;
//찍기
function makePic() { capture(0) }
pic_btn.onPress = mx.utils.Delegate.create(this,makePic);
//캡쳐하기
function capture(nr){
this["snapshot"+nr] = new BitmapData(output_vid._width,output_vid._height);
this["snapshot"+nr].draw(output_vid,new Matrix());
var t:MovieClip = createEmptyMovieClip("pictured_mc"+nr,nr);
t._x = 345; t._y = 20; //찍은 화면의 출력위치
t._xscale = t._yscale = 50 //새 무비클립의 사이즈를 50%줄여주기
t.attachBitmap(this["snapshot"+nr],1);
attachMovie("print_but", "bot"+nr, 100+nr, {_x:345, _y:165});
}
// PHP로 넘겨줄 비트맵 데이터 출력하기
function output(nr){
var pixels:Array = new Array()
var snap = new BitmapData(this["snapshot"+nr].width, this["snapshot"+nr].height);
var myMatrix = new Matrix();
myMatrix.scale(0.5, 0.5); //PHP에서 출력될 이미지의 사이즈
snap.draw(this["snapshot"+nr], myMatrix);
var w:Number = snap.width, tmp
var h:Number = snap.height
for(var a=0; a<=w; a++){
for(var b=0; b<=h; b++){
tmp = snap.getPixel32(a, b).toString(16)
pixels.push(tmp.substr(1))
}
}
//PHP에 데이터 넘겨주기
var output:LoadVars = new LoadVars()
output.img = pixels.toString()
output.height = h
output.width = w
//PHP의 경로지정
output.send("show.php", "output", "POST")
}
SWF파일을 퍼블리싱하고, SWF가 포함된 index.php파일과 show.php파일을 PHP가 구동되는 서버에 업로드 합니다. show.php파일의 내용은 다음과 같습니다.
<?
if(!function_exists("imagecreate")) die("GD라이브러리를 설치해야 합니다.");
//Capture Post data
$data = explode(",", $_POST['img']);
$width = $_POST['width'];
$height = $_POST['height'];
//Allocate image
$image=(function_exists("imagecreatetruecolor"))?imagecreatetruecolor( $width ,$height ):imagecreate( $width ,$height );
$background = imagecolorallocate( $image ,0 , 0 , 0 );
//Copy pixels
$i = 0;
for($x=0; $x<=$width; $x++){
for($y=0; $y<=$height; $y++){
$r = 255-hexdec("0X".substr( $data[$i] , 0 , 2 ));
$g = 255-hexdec("0x".substr( $data[$i] , 2 , 2 ));
$b = 255-hexdec("0x".substr( $data[$i++] , 4 , 2 ));
$color = ($r << 16) | ($g << 8) | $b;
imagesetpixel ( $image , $x , $y , $color );
}
}
//Output image and clean
header( "Content-type: image/jpeg" );
ImageJPEG( $image );
imagedestroy( $image );
?>
간단한 예제이지만, 유용한 소스가 될 수 있을 것입니다. 예를 들어 회원가입시에 사진을 업로드 해야하는 경우 이미 찍어 놓은 JPG파일이 없는 경우 가입 시 화상캠으로 바로 찍어 업로드 하는 어플리케이션을 만들때나, 미니홈피 같은 곳에서 사진 바로 찍어 올리기 같은 기능을 만들때 유용할 듯 합니다.
TRACKBACK :: http://je.yomybaby.com/trackback/312
댓글을 달아 주세요
test
2008/10/06 14:00