2012-05-02 86 views
3

對於Android Flash移動應用程序,我嘗試從相機中抓取預覽幀。FREBitmapData無法編輯

不幸的是,我不能使用AIR內置攝像頭類,因爲除非我調用setMode()(這是因爲它凍結UI而不可接受的),否則攝像頭不會聚焦。

所以我決定爲此寫一個本地擴展。該擴展本身正在工作(我在NativeCamera.pixels中獲得適當的圖像數據),但我無法使用FREBitmapData傳輸幀。在ActionScript端,BitmapData具有正確的維度,但它仍然是黑色的。

我的java代碼

package com.jumptomorrow.nativecamera; 

import java.nio.ByteBuffer; 

import com.adobe.fre.FREBitmapData; 
import com.adobe.fre.FREContext; 
import com.adobe.fre.FREFunction; 
import com.adobe.fre.FREObject; 

public class NativeCameraGrabFrameFunction implements FREFunction { 

    @Override 
    public FREObject call(FREContext context, FREObject[] object) { 
     try { 
      FREBitmapData out = null; 

      try { 
       out = FREBitmapData.newBitmapData(NativeCamera.size.width, NativeCamera.size.height, false, new Byte[]{0xf,0xf,0x0,0x0}); 
       out.acquire(); 
       ByteBuffer bb = out.getBits(); 
       bb = ByteBuffer.wrap(NativeCamera.pixels); 
       out.invalidateRect(0, 0, NativeCamera.size.width, NativeCamera.size.height); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      try { 
       out.release(); 
      } catch(Exception e) { 
       e.printStackTrace(); 
      } 

      return out; 

     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

} 

我的ActionScript代碼

package 
{ 
    import flash.display.Bitmap; 
    import flash.display.BitmapData; 
    import flash.display.Sprite; 
    import flash.display.StageAlign; 
    import flash.display.StageScaleMode; 
    import flash.events.Event; 
    import flash.utils.ByteArray; 

    import net.hires.debug.Stats; 

    public class camera_test extends Sprite 
    { 
     private var nc:NativeCameraInterface; 
     private var bitmap:Bitmap; 

     public function camera_test() 
     { 
      super(); 

      // support autoOrients 
      stage.align = StageAlign.TOP_LEFT; 
      stage.scaleMode = StageScaleMode.NO_SCALE; 

      nc = new NativeCameraInterface(); 
      try { 
       nc.startCamera(); 
      } 
      catch(e:Error) { 
       trace(e.getStackTrace()); 
      } 

      addEventListener(Event.ENTER_FRAME, onEnterFrame); 
     } 

     protected function onEnterFrame(event:Event):void 
     { 
      if(nc) { 
       var bmpd:BitmapData = nc.getFrame() as BitmapData; 

       try { 
        var bmp:Bitmap = new Bitmap(bmpd); 

        addChild(bmp); 
        bmp.scaleX = bmp.scaleY = 2; 

       } catch(e:Error) { 
        trace("failed"); 
       } 
      } 
     } 
    } 
} 

有沒有人一個想法,這可能是什麼?

回答

1

不該這些行...

ByteBuffer bb = out.getBits(); 
bb = ByteBuffer.wrap(NativeCamera.pixels); 

...是這樣的?

ByteBuffer bb = out.getBits(); // (no change) 
bb.position(0); 
bb.put(ByteBuffer.wrap(NativeCamera.pixels)); 

這個修改,而不是通過bb = ByteBuffer.wrap(...)創建一個新的ByteBuffer比如通過out.getBits()獲得ByteBuffer實例。

+0

是。這使它。謝謝 – flecki89