【问题标题】:Bitmap Data .draw() with transform matrix带有变换矩阵的位图数据 .draw()
【发布时间】:2011-06-29 10:55:02
【问题描述】:

我正在进行一个项目,用户可以使用他们的网络摄像头拍摄自己的快照,然后通过转换比例和旋转来编辑此图像并保存结果。

我已经完成了大部分工作,用户可以拍摄自己的快照,使用 Senocular 的变换工具变换对象,然后我使用 .draw() 来保存这个变换后的对象。问题是 .draw() 只是从舞台的左上角抓取数据。它正在绘制转换后的对象,但仅从左上角绘制。

为什么它只从左上角绘制,我怎样才能将坐标设置为只从设置捕获图像的区域绘制?

谢谢。

您可以在以下位置查看文件:http://s46264.gridserver.com/dev/dave/pb-photo/index.html

我已将 FLA 和相关课程压缩到 http://s46264.gridserver.com/dev/dave/pb-photo/pb-photo.zip

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.geom.Matrix;
import flash.net.FileReference;
import com.adobe.images.JPGEncoder;
import com.senocular.display.transform.*;

// create container for captured image to apply Transform Tool to
var box:Sprite = new Sprite();
addChild(box);
box.graphics.beginFill(0xAACCDD);
box.graphics.drawRect(-160, -120, 320, 240); // xreg, yreg, width, height (x-y = width-height / 2 to set centered registration point)
box.x = 520;
box.y = 140;

// create the Transform Tool
var tool:TransformTool = new TransformTool(new ControlSetStandard());
addChild(tool);

// select the box with the transform tool when clicked. 
// deselect when clicking on the stage
box.addEventListener(MouseEvent.MOUSE_DOWN, tool.select);
stage.addEventListener(MouseEvent.MOUSE_DOWN, tool.deselect);

var snd:Sound = new camerasound(); //new sound instance for the "capture" button click

var bandwidth:int = 0; // Maximum amount of bandwidth that the current outgoing video feed can use, in bytes per second.
var quality:int = 100; // This value is 0-100 with 1 being the lowest quality. 

var cam:Camera = Camera.getCamera();
cam.setQuality(bandwidth, quality);
cam.setMode(320,240,30,false); // setMode(videoWidth, videoHeight, video fps, favor area)
var video:Video = new Video();
video.attachCamera(cam);
video.x = 20;
video.y = 20;
addChild(video);

// create bitmap to hold initial capture and addChild to transform box
var bitmapData:BitmapData = new BitmapData(video.width,video.height);

var bitmap:Bitmap = new Bitmap(bitmapData);
bitmap.x = -160;
bitmap.y = -120;
box.addChild(bitmap);

capture_mc.buttonMode = true;
capture_mc.addEventListener(MouseEvent.CLICK,captureImage);

function captureImage(e:MouseEvent):void {
    snd.play();
    bitmapData.draw(video);
    save_mc.buttonMode = true;
    save_mc.addEventListener(MouseEvent.CLICK, onSaveJPG);
    save_mc.alpha = 1;
}

save_mc.alpha = .5;

// save transformed bmp to new object and addChild to holder_mc
function onSaveJPG(e:Event):void{

    var m:Matrix = box.transform.matrix;
    trace(m);

    var bmp:BitmapData = new BitmapData(320, 240, true, 0xCCCCCCCC);
    bmp.draw(box, m);

    var newbmp:Bitmap = new Bitmap(bmp);
    holder_mc.addChild(newbmp);

}

【问题讨论】:

    标签: flash actionscript-3 matrix bitmapdata


    【解决方案1】:

    有趣的是,我目前正在做一些非常相似的事情。您可以通过将 x 和 y 偏移作为矩阵的一部分传递给 draw 来绘制目标的某个区域:

    var bmd:BitmapData = new BitmapData(target.width,target.height,true,0);
    var mat:Matrix = new Matrix(1,0,0,1,-target.x,-target.y);
    bmd.draw(this,mat);
    

    特别注意传入矩阵的 x 和 y 设置为负数。

    【讨论】:

      【解决方案2】:

      您可以绘制整个舞台,然后使用 copyPixels() 选择所需的像素。方法如下:)

      private function drawImage():void
      {
          var bmp:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight)
          bmp.draw(stage);
      
          var _newBmp:BitmapData = new bitmapData(WidthOfTheImage, HeightOfTheImage, TransParancy, FillColor);
          _newBmp.copyPixels(bmp, new Rectangle(XofTheImage, YOftheImage, WidthOfTheImage, HeightOfTheImage), new point());
      }
      

      _newBmp 是你想要的位图数据 :)

      【讨论】:

        【解决方案3】:

        获取剪辑的边界将为您提供要复制的确切区域。

        var bounds:Rectangle = mc.getBounds(mc);
        var bitmapData:BitmapData = new BitmapData(int(bounds.width + 0.5), int(bounds.height + 0.5), true, 0);
        bitmapData.draw(mc, new Matrix(1, 0, 0, 1, -bounds.x, -bounds.y));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多