无法编辑FREBitmapData

时间:2012-05-02 17:16:29

标签: android actionscript-3 flash air-native-extension

对于Android Flash Mobile App,我尝试从相机中抓取预览帧。

不幸的是我不能使用AIR内置摄像头类,因为除非我调用setMode(),因此相机不会聚焦(这是因为它冻结了UI而不可接受)。

所以我决定为此编写一个Native Extension。扩展本身正在工作(我在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 个答案:

答案 0 :(得分: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));

这会修改通过ByteBuffer获得的out.getBits()实例,而不是通过ByteBuffer创建新的bb = ByteBuffer.wrap(...)实例。