我目前正在查看zip文件中的面部检测示例:
该示例加载图像,然后用黑色矩形突出显示该图像。 我想保存生成的图像,换句话说,用正方形注释图像而不是其他任何东西(只是图像和正方形,而不是整个舞台)。
到目前为止,我可以通过将图像处理为位图数据并重新保存来保存图像,但是我无法保存图像' square。我已经尝试复制绘制正方形的线并将其绘制到另一个位图对象,尝试在精灵上绘制它,并且我尝试使用矩阵计算它被绘制出边界。没有运气。
下面是原始代码,然后是徒劳的尝试(我意识到它保存了一张空白的白色图像,我试图看看我是否可以画一个正方形):
package
{
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.text.TextField;
import jp.maaash.ObjectDetection.ObjectDetector;
import jp.maaash.ObjectDetection.ObjectDetectorOptions;
import jp.maaash.ObjectDetection.ObjectDetectorEvent;
import flash.utils.getTimer;
public class FaceDetector extends Sprite{
private var debug :Boolean = true;
private var detector :ObjectDetector;
private var options :ObjectDetectorOptions;
private var faceImage :Loader;
private var bmpTarget :Bitmap;
private var view :Sprite;
private var faceRectContainer :Sprite;
private var tf :TextField;
private var lastTimer:int = 0;
public function FaceDetector() {
initUI();
initDetector();
faceImage.load( new URLRequest("013.jpg") );
}
private function initUI():void{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
view = new Sprite;
addChild(view);
faceImage = new Loader;
faceImage.contentLoaderInfo.addEventListener( Event.COMPLETE, function(e :Event) :void {
startDetection();
});
view.addChild( faceImage );
faceRectContainer = new Sprite;
view.addChild( faceRectContainer );
tf = new TextField;
tf.x = 256;
tf.width = 600;
tf.height = 300;
tf.textColor = 0x000000;
tf.multiline = true;
view.addChild( tf );
}
private function initDetector():void{
detector = new ObjectDetector;
detector.options = getDetectorOptions();
detector.addEventListener(ObjectDetectorEvent.DETECTION_COMPLETE,function( e :ObjectDetectorEvent ):void{
logger("[ObjectDetectorEvent.COMPLETE]");
tf.appendText( "\ntime: "+(new Date)+" "+e.type );
detector.removeEventListener( ObjectDetectorEvent.DETECTION_COMPLETE, arguments.callee );
if( e.rects ){
var g :Graphics = faceRectContainer.graphics;
g.clear();
g.lineStyle( 2 ); // black 2pix
e.rects.forEach( function( r :Rectangle, idx :int, arr :Array ) :void {
g.drawRect( r.x, r.y, r.width, r.height );
});
}
});
detector.addEventListener( ObjectDetectorEvent.DETECTION_START, function(e :ObjectDetectorEvent) :void {
tf.appendText( "\ntime: "+(new Date)+" "+e.type );
});
}
private function startDetection():void{
logger("[startDetection]");
bmpTarget = new Bitmap( new BitmapData( faceImage.width, faceImage.height, false ) )
bmpTarget.bitmapData.draw( faceImage );
detector.detect( bmpTarget.bitmapData );
}
private function getDetectorOptions() :ObjectDetectorOptions {
options = new ObjectDetectorOptions();
options.min_size = 50;
return options;
}
private function logger(... args):void{
if(!debug){ return; }
trace( args, getTimer(), getTimer() - lastTimer);
lastTimer = getTimer();
}
}
}
我的代码:
package
{
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.events.Event;
import flash.geom.*;//Rectangle;
import flash.net.URLRequest;
import flash.text.TextField;
import jp.maaash.ObjectDetection.ObjectDetector;
import jp.maaash.ObjectDetection.ObjectDetectorOptions;
import jp.maaash.ObjectDetection.ObjectDetectorEvent;
import com.adobe.images.*;
import flash.utils.getTimer;
////
import flash.filesystem.*;
import flash.utils.ByteArray;
////
public class FaceDetector extends Sprite{
private var debug :Boolean = true;
private var detector :ObjectDetector;
private var options :ObjectDetectorOptions;
private var faceImage :Loader;
private var bmpTarget :Bitmap;
private var view :Sprite;
private var faceRectContainer :Sprite;
private var tf :TextField;
private var lastTimer:int = 0;
public function FaceDetector() {
initUI();
initDetector();
faceImage.load( new URLRequest("013.jpg") );
}
private function initUI():void{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
view = new Sprite;
addChild(view);
faceImage = new Loader;
faceImage.contentLoaderInfo.addEventListener( Event.COMPLETE, function(e :Event) :void {
startDetection();
});
view.addChild( faceImage );
faceRectContainer = new Sprite;
view.addChild( faceRectContainer );
tf = new TextField;
tf.x = 256;
tf.width = 600;
tf.height = 300;
tf.textColor = 0x000000;
tf.multiline = true;
view.addChild( tf );
}
private function initDetector():void{
detector = new ObjectDetector;
detector.options = getDetectorOptions();
detector.addEventListener(ObjectDetectorEvent.DETECTION_COMPLETE,function( e :ObjectDetectorEvent ):void{
logger("[ObjectDetectorEvent.COMPLETE]");
tf.appendText( "\ntime: "+(new Date)+" "+e.type );
detector.removeEventListener( ObjectDetectorEvent.DETECTION_COMPLETE, arguments.callee );
var rt:Sprite = new Sprite();
var bmprt:Bitmap = new Bitmap( new BitmapData( faceImage.width, faceImage.height, false ) );
var matrix:Matrix = new Matrix();
var rect:Rectangle = new Rectangle();
if( e.rects ){
var g :Graphics = faceRectContainer.graphics;
g.clear();
g.lineStyle( 2 ); // black 2pix
e.rects.forEach( function( r :Rectangle, idx :int, arr :Array ) :void {
g.drawRect( r.x, r.y, r.width, r.height );
matrix.translate(-r.x, -r.y);
rect = r;
rt.graphics.drawRect(r.x, r.y, r.width, r.height );
});
}
var file:File = File.applicationStorageDirectory.resolvePath("testoutput.jpg");
var fs:FileStream = new FileStream();
var byteArray:ByteArray;
var jpgEncoder:JPGEncoder = new JPGEncoder( 90 );
//rt.graphics.draw(g);
bmpTarget.bitmapData.draw(rt);
bmprt.bitmapData.draw(rt, matrix);
byteArray = jpgEncoder.encode( bmprt.bitmapData );
fs.open(file, FileMode.WRITE);
fs.writeBytes(byteArray);
fs.close();
});
detector.addEventListener( ObjectDetectorEvent.DETECTION_START, function(e :ObjectDetectorEvent) :void {
tf.appendText( "\ntime: "+(new Date)+" "+e.type );
});
}
private function startDetection():void{
logger("[startDetection]");
bmpTarget = new Bitmap( new BitmapData( faceImage.width, faceImage.height, false ) )
bmpTarget.bitmapData.draw( faceImage );
detector.detect( bmpTarget.bitmapData );
}
private function getDetectorOptions() :ObjectDetectorOptions {
options = new ObjectDetectorOptions();
options.min_size = 50;
return options;
}
private function logger(... args):void{
if(!debug){ return; }
trace( args, getTimer(), getTimer() - lastTimer);
lastTimer = getTimer();
}
}
}
TL; DR:
假设您已经知道如何加载和保存图像,如何加载图像,在其上绘制正方形,然后保存结果?
提前感谢任何帮助。
修改的
以下是感兴趣的人的最终代码:
package
{
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.events.Event;
import flash.geom.*;//Rectangle;
import flash.net.URLRequest;
import flash.text.TextField;
import jp.maaash.ObjectDetection.ObjectDetector;
import jp.maaash.ObjectDetection.ObjectDetectorOptions;
import jp.maaash.ObjectDetection.ObjectDetectorEvent;
import com.adobe.images.*;
import flash.utils.getTimer;
////
import flash.filesystem.*;
import flash.utils.ByteArray;
////
public class FaceDetector extends Sprite{
private var debug :Boolean = true;
private var detector :ObjectDetector;
private var options :ObjectDetectorOptions;
private var faceImage :Loader;
private var bmpTarget :Bitmap;
private var view :Sprite;
private var faceRectContainer :Sprite;
private var tf :TextField;
private var lastTimer:int = 0;
public function FaceDetector() {
initUI();
initDetector();
faceImage.load( new URLRequest("013.jpg") );
}
private function initUI():void{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
view = new Sprite;
addChild(view);
faceImage = new Loader;
faceImage.contentLoaderInfo.addEventListener( Event.COMPLETE, function(e :Event) :void {
startDetection();
});
view.addChild( faceImage );
faceRectContainer = new Sprite;
view.addChild( faceRectContainer );
tf = new TextField;
tf.x = 256;
tf.width = 600;
tf.height = 300;
tf.textColor = 0x000000;
tf.multiline = true;
view.addChild( tf );
}
private function initDetector():void{
detector = new ObjectDetector;
detector.options = getDetectorOptions();
detector.addEventListener(ObjectDetectorEvent.DETECTION_COMPLETE,function( e :ObjectDetectorEvent ):void{
logger("[ObjectDetectorEvent.COMPLETE]");
tf.appendText( "\ntime: "+(new Date)+" "+e.type );
detector.removeEventListener( ObjectDetectorEvent.DETECTION_COMPLETE, arguments.callee );
var bmprt:Bitmap = new Bitmap( new BitmapData( faceImage.width, faceImage.height, false ) );
if( e.rects ){
var g :Graphics = faceRectContainer.graphics;
g.clear();
g.lineStyle(3, 0xFF0000, 1); // black 2pix--- parameters(thicknes in pixels, color in hex, alpha (0-1))
e.rects.forEach( function( r :Rectangle, idx :int, arr :Array ) :void {
g.drawRect( r.x, r.y, r.width, r.height );
});
}
var file:File = File.applicationStorageDirectory.resolvePath("testoutput.jpg");
var fs:FileStream = new FileStream();
var byteArray:ByteArray;
var jpgEncoder:JPGEncoder = new JPGEncoder( 90 );
bmprt.bitmapData.draw(faceImage);
bmprt.bitmapData.draw(faceRectContainer);
byteArray = jpgEncoder.encode( bmprt.bitmapData );
fs.open(file, FileMode.WRITE);
fs.writeBytes(byteArray);
fs.close();
});
detector.addEventListener( ObjectDetectorEvent.DETECTION_START, function(e :ObjectDetectorEvent) :void {
tf.appendText( "\ntime: "+(new Date)+" "+e.type );
});
}
private function startDetection():void{
logger("[startDetection]");
bmpTarget = new Bitmap( new BitmapData( faceImage.width, faceImage.height, false ) )
bmpTarget.bitmapData.draw( faceImage );
detector.detect( bmpTarget.bitmapData );
}
private function getDetectorOptions() :ObjectDetectorOptions {
options = new ObjectDetectorOptions();
options.min_size = 50;
return options;
}
private function logger(... args):void{
if(!debug){ return; }
trace( args, getTimer(), getTimer() - lastTimer);
lastTimer = getTimer();
}
}
}
感谢Vesper的帮助。
答案 0 :(得分:0)
好吧,您将图片加载为Bitmap
,然后确定绘制矩形的位置,创建Shape
并在graphics
上绘制矩形,然后调用bitmap.bitmapData.draw(shape)
在加载的位图上绘制矩形,然后保存位图。在您的代码中,在准备好矩形后,您可以执行以下操作:
bmprt.bitmapData.copyPixels(faceImage.bitmapData,faceImage.bitmapData.rect,new Point());
bmprt.bitmapData.draw(faceRectContainer);
然后像你一样保存bmprt
。