For an Android Flash Mobile App I try to grab a Preview-frame from the camera.
Unfortunately I can't use the AIR Built-in Camera class because the camera isn't focusing unless I call setMode() (which is unacceptable because it freezes the UI).
So I decided to write a Native Extension for this. The extension itself is working (I get the proper Image data in NativeCamera.pixels) but I can't transmit the Frame using FREBitmapData. On ActionScript side the BitmapData has the right dimension, but it remains black.
My Java-Code
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;
}
}
My ActionScript Code
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");
}
}
}
}
}
Has anyone an idea what this could be?
Shouldn't these lines...
ByteBuffer bb = out.getBits();
bb = ByteBuffer.wrap(NativeCamera.pixels);
... be something like this?
ByteBuffer bb = out.getBits(); // (no change)
bb.position(0);
bb.put(ByteBuffer.wrap(NativeCamera.pixels));
This modifies the ByteBuffer
instance obtained through out.getBits()
, instead of creating a new ByteBuffer
instance through bb = ByteBuffer.wrap(...)
.