您所在的位置: 首页 >> 查看信息 >> flash摄像头拍照实现原理分析 分享家:Addthis中文版

flash摄像头拍照实现原理分析

    最近一段时间,由于网站的需要,做了一个在线拍照的flash,刚开始的时候也是什么都不懂,对flash也是了解点皮毛,经过不断的在网上查阅资料。经过一周的努力,终于被我搞定了。
   
   主要用到Camera类,MovieClip 类,BitmapData类,LoadVars类,如果要实现这个功能,必须先去了解这几个类
   
   实现的过程如下。
   
   1,先在库中新建一个视频元件,把视频元件拖放到场景中,取名为my_video,新建一个按纽元件,取名为btn_print.
   
   2,加入以下代码实现视频的播放
   myCam = Camera.get();
   myCam.setMode(266,200,10);
   my_video.attachVideo(myCam);
   
   3 ,以下代码实现图片的获得和上传
   import flash.display.BitmapData;
   import flash.geom.Rectangle;
   import flash.geom.ColorTransform;
   
   class PrintScreen {
   
    public var addListener:Function
    public var broadcastMessage:Function
   
    private var id: Number;
    public var record:LoadVars;
   
    function PrintScreen(){
    AsBroadcaster.initialize( this );
    }
    public function print(mc:MovieClip, w:Number, h:Number){
    broadcastMessage("onStart", mc);
    w = Math.floor(w);
    h = Math.floor(h);
    var bmp:BitmapData = new BitmapData(w, h, false);
    record = new LoadVars();
    record.width = w
    record.height = h
    record.cols = 0
    record.rows = 0
    bmp.draw(mc);
    id = setInterval(copysource, 1, this, mc, bmp);
    }
   
    private function copysource(scope, movie, bit){
    var pixel:Number
    var str_pixel:String
    scope.record["px" + scope.record.rows] = new Array();
    for(var a = 0; a < bit.width; a++){
    pixel = bit.getPixel(a, scope.record.rows )
    str_pixel = pixel.toString(16)
    scope.record["px" + scope.record.rows].push(str_pixel)
    }
   
    scope.record.rows += 1
    if(scope.record.rows >= bit.height){
    clearInterval(scope.id)
    bit.dispose();
    }
    }
   }
   
   4 以下代码在服务器段接受数据,生成图片(jsp版)
   String width = request.getParameter("width");
    String height = request.getParameter("height");
   int w = Integer.parseInt(width);
    int h = Integer.parseInt(height);
   try {
    OutputStream fos = response.getOutputStream();
    BufferedImage bf = new BufferedImage(w, h,
    BufferedImage.TYPE_INT_RGB);
   
    for (int i = 0; i < bf.getHeight(); i++) {
    String data = request.getParameter("px" + i);
    String[] ds = data.split(",");
    for (int j = 0; j < bf.getWidth(); j++) {
    int d = Integer.parseInt(ds[j], 16);
    bf.setRGB(j, i, d);
    }
    }
   
    ImageWriter writer = null;
    ImageTypeSpecifier type = ImageTypeSpecifier
    .createFromRenderedImage(bf);
    Iterator iter = ImageIO.getImageWriters(type, "jpg");
    if (iter.hasNext()) {
    writer = (ImageWriter) iter.next();
    }
    if (writer == null) {
    return null;
    }
    IIOImage iioImage = new IIOImage(bf, null, null);
    ImageWriteParam param = writer.getDefaultWriteParam();
   
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
   
    param.setCompressionQuality(0.2f);
   
    // save to file
    ImageOutputStream outputStream2 = ImageIO
    .createImageOutputStream(response.getOutputStream());
    writer.setOutput(outputStream2);
    writer.write(null, iioImage, param);
   
    fos.close();
   
    } catch (Exception e) {
    e.printStackTrace();
    }
   System.out.println("w = " + w + ",h=" + h);
    response.setContentType("image/jpeg");
    return null;
来源: 互联网
作者:Administrator
浏览:3318

2009/8/11 14:25:12
搜索文章
 

最新文章列表

热门文章列表