我是Flash(AS3)的新手,这只是我的一个小实验。
我正在尝试变形通过网络摄像头拍摄的图像。网络摄像头快照部分已经在工作,但我仍然不知道如何进行变形部分。任何人都可以指点我如何使用flash actionscript 3实现图像变形的教程(或亲切的帖子代码示例)?
可以找到我想要实现的变形示例 here。谢谢。
答案 0 :(得分:3)
几年前我需要类似的东西。幸运的是格兰特斯金纳已经开发了camgoo。
从那时起我仍然拥有快速而又脏的端口。 注意:您需要在库中添加一个MovieClip,其中包含您想要变形的图像,链接为 Img 和一个链接为 Brush 的画笔MovieClip。最简单的入门方法是从Grant Skinner的网站上获取原始as2源代码中的符号,将它们放在as3文档中并使用Morph类,只需尝试类似 addChild(new Morph()); < /强>
代码根本没有优化,它是一个非常快速和文字的端口。 actionscript-3功能并非最佳使用:
package
{
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.filters.*;
/**
* @author Grant Skiner - original code: http://incomplet.gskinner.com/index2.html#camgoo
* @author port - George Profenza
*/
public class Morph extends Sprite
{
private var rect:Rectangle;
private var mapBmp:BitmapData;
private var blurredMapBmp:BitmapData;
private var blurF:BlurFilter;
private var pt:Point;
private var dispMapF:DisplacementMapFilter;
// holder for transient values (ex. during drag, animation)
private var tmp:Object;
private var mapHolder:MovieClip;
private var debugBitmap:Bitmap;
private var img:MovieClip;
private var brush:MovieClip;
private var isAnimating:Boolean = false;
private var btn:Sprite;
private var btn2:Sprite;
public function Morph()
{
init();
}
private function init():void
{
trace('init');
img = new Img();
addChild(img);
brush = new Brush();
brush.scaleX = brush.scaleY = .75;
addChild(brush);
mapHolder = new MovieClip();
addChild(mapHolder);
debugBitmap = new Bitmap(new BitmapData(img.width, img.height, false, 0x808080));
debugBitmap.alpha = 0;
mapHolder.addChild(debugBitmap);
rect = new Rectangle(0,0,Math.floor(img.width),Math.floor(img.height));
pt = new Point(0,0);
// set up bitmaps:
mapBmp = new BitmapData(rect.width,rect.height,false,0x808080);
blurredMapBmp = mapBmp.clone();
// set up filters:
blurF = new BlurFilter(8,8,2);
dispMapF = new DisplacementMapFilter(blurredMapBmp, pt,BitmapDataChannel.RED, BitmapDataChannel.GREEN, 100, 100, DisplacementMapFilterMode.CLAMP);
brush.visible = false;
this.addEventListener(MouseEvent.MOUSE_DOWN, startGoo);
btn = new Sprite();
btn.graphics.beginFill(0);
btn.graphics.drawRect(0, 0, 50, 50);
btn.graphics.endFill();
btn.visible = false;
addChild(btn);
btn.addEventListener(MouseEvent.CLICK, onAnimClick);
btn2 = new Sprite();
btn2.graphics.beginFill(0);
btn2.graphics.drawRect(0, 0, 50, 50);
btn2.graphics.endFill();
btn2.y = btn.height + 5;
btn2.visible = false;
addChild(btn2);
btn2.addEventListener(MouseEvent.CLICK, endAnimate);
}
private function onAnimClick(e:MouseEvent):void
{
isAnimating = true;
animateGoo();
}
private function startGoo(e:MouseEvent):void{
tmp = { oldx:mouseX, oldy:mouseY };
this.addEventListener(MouseEvent.MOUSE_UP, endGoo);
this.addEventListener(MouseEvent.MOUSE_MOVE, gooify);
}
private function endGoo(e:MouseEvent):void{
tmp = null;
this.removeEventListener(MouseEvent.MOUSE_UP, endGoo);
this.removeEventListener(MouseEvent.MOUSE_MOVE, gooify);
}
private function clearGoo():void {
mapBmp.fillRect(rect,0x808080);
blurredMapBmp.fillRect(rect,0x808080);
applyMap();
}
private function gooify(e:MouseEvent):void{
var dx:Number = mouseX-tmp.oldx;
var dy:Number = mouseY-tmp.oldy;
tmp = {oldx:mouseX,oldy:mouseY};
brush.rotation = (Math.atan2(dy, dx)) * 180 / Math.PI;
brush.x = mouseX;
brush.y = mouseY;
var g:Number = 0x80+Math.min(0x79,Math.max(-0x80, -dx*2 ));
var b:Number = 0x80+Math.min(0x79,Math.max(-0x80, -dy*2 ));
var ct:ColorTransform = new ColorTransform(0,0,0,1,0x80,g,b,0);
mapBmp.draw(brush,brush.transform.matrix,ct,BlendMode.HARDLIGHT);
applyMap();
}
private function applyMap() {
blurredMapBmp.applyFilter(mapBmp, rect, pt, blurF);
img.filters = [dispMapF];
}
private function animateGoo():void {
removeEventListener(MouseEvent.MOUSE_DOWN, startGoo);
addEventListener(MouseEvent.MOUSE_DOWN, endAnimate);
addEventListener(Event.ENTER_FRAME, animate);
tmp = {count:100,dir:-4,scale:dispMapF.scaleX}
}
private function animate(e:Event):void {
tmp.count+=tmp.dir;
if (tmp.count >= 100 || tmp.count <= 0) { tmp.dir *= -1; }
dispMapF.scaleX = dispMapF.scaleY = tmp.count / 100 * tmp.scale;
applyMap();
}
private function endAnimate(e:MouseEvent):void {
isAnimating = false;
removeEventListener(MouseEvent.MOUSE_DOWN, endAnimate);
removeEventListener(Event.ENTER_FRAME, animate);
addEventListener(MouseEvent.MOUSE_DOWN, startGoo);
tmp = null;
dispMapF = new DisplacementMapFilter(blurredMapBmp, pt,BitmapDataChannel.RED, BitmapDataChannel.GREEN, 100, 100, DisplacementMapFilterMode.CLAMP);
applyMap();
}
public function set anim(value:Boolean):void {
value ? btn.dispatchEvent(new MouseEvent(MouseEvent.CLICK)) : btn2.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
isAnimating = value;
}
public function get anim():Boolean {
return isAnimating;
}
}
}
答案 1 :(得分:2)
答案 2 :(得分:2)
答案 3 :(得分:0)
嗯,很少有开源项目可以做这件事。 javamorph听起来很有趣。它是开源的,所以你可以查看源代码并试一试。
答案 4 :(得分:0)
在几个免费周末(和工作日)之后,我很高兴分享到目前为止我所得到的。我知道需要更多改进。年轻人也变得懒惰:P。您可以下载源here。加入它仍然在那里,因为该域将很快到期哈哈。但我会尽量保持网站的建立,我希望在接下来的几个月内能够获得更多的项目:D。
谢谢你们的帮助。