我有一个Android USB Camera应用程序,它会将Camera帧泵入FrameAvaliable()方法。从此方法检索的数据是有效的(通过将原始文件保存为图像来确认)。我尝试在SurfaceView创建的FrameAvaliable上渲染帧。
public class MainActivity extends Activity implements USBCameraListener{
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
private Thread rendererThread;
private boolean isStarted;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
isStarted = false;
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(final SurfaceHolder holder) {
surfaceHolderORG = holder;
Log.d(TAG, "Surface Created");
rendererThread = new Thread(){
@Override
public void run() {
while(true) {
if (isStarted== true) {
//Added code
byte[] bytes = new byte[frameBuffer.remaining()];
int offset = 0, n = 0;
InputStream in = new ByteArrayInputStream(bytes);
try {
while (offset < bytes.length
&& (n = in.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += n;
}
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// getting byte array
Bitmap bmp;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
Canvas canvas = surfaceHolderORG.lockCanvas();
if (canvas != null) {
canvas.drawBitmap(bmp, 0, 0, null);
surfaceHolderORG.unlockCanvasAndPost(canvas);
}
}
}
}
};
rendererThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
// Frame Listener
@Override
public void FrameAvailable(ByteBuffer byteBuffer) {
frameBuffer = byteBuffer;
if(isStarted == false){
startedProducer = true;
}
}
}
}
当FrameAvailable()开始生成帧时,条件检查会在rendererThread中输入渲染器代码,并且应用程序会抛出以下异常,并且SurfaceView中没有任何预览。
12-26 03:05:20.590 12023-12104/com.sample.USBAPP D/skia: --- SkImageDecoder::Factory returned null
12-26 03:05:20.590 3613-5139/? E/BufferQueueProducer: [SurfaceView] connect(P): already connected (cur=1 req=2)
12-26 03:05:20.590 12023-12104/com.sample.USBAPP E/SurfaceHolder: Exception locking surface
12-26 03:05:20.590 12023-12104/com.sample.USBAPP E/SurfaceHolder: java.lang.IllegalArgumentException
12-26 03:05:20.590 12023-12104/com.sample.USBAPP E/SurfaceHolder: at android.view.Surface.nativeLockCanvas(Native Method)
我不知道我哪里出错了。