I am actually working on a camera based project. I am adding in a queue, preview frames from onPreviewFrame from the camera that I get back in a running thread to process and draw it in a SurfaceView. (My goal is to send it over network).
My problem is that the BitmapFactory.decodeByteArray() function is very slow on my custom thread, but works really fine on the UI Thread:
this works fine:
private void drawFromThread(final byte[] data) {
size = mCamera.getPreviewSize();
YuvImage yuvimage;
yuvimage = new YuvImage(data, ImageFormat.NV21, size.width,size.height, null);
ByteArrayOutputStream baos;
baos = new ByteArrayOutputStream();
yuvimage.compressToJpeg(new Rect(0, 0, size.width, size.height), 10,baos);
final byte[] jdata = baos.toByteArray();
((Activity)context).runOnUiThread(new Runnable(){
@Override
public void run() {
Bitmap bmp;
bmp = BitmapFactory.decodeByteArray(jdata, 0, jdata.length);
Canvas canvas = contactView.getHolder().lockCanvas();
canvas.drawBitmap(bmp, 0, 0, null);
contactView.getHolder().unlockCanvasAndPost(canvas);
}
});
}
But this, is really slow:
private void drawFromThread(final byte[] data) {
size = mCamera.getPreviewSize();
YuvImage yuvimage;
yuvimage = new YuvImage(data, ImageFormat.NV21, size.width,
size.height, null);
ByteArrayOutputStream baos;
baos = new ByteArrayOutputStream();
yuvimage.compressToJpeg(new Rect(0, 0, size.width, size.height), 10,
baos);
final byte[] jdata = baos.toByteArray();
Bitmap bmp;
bmp = BitmapFactory.decodeByteArray(jdata, 0, jdata.length);
Canvas canvas = contactView.getHolder().lockCanvas();
canvas.drawBitmap(bmp, 0, 0, null);
contactView.getHolder().unlockCanvasAndPost(canvas);
}
I googled a lot but didn't find anyone with the same problem.
I know there are most efficient way to use the camera and a SurfaceView, i have another surfaceView that is used to display my camera frames in the "standard way". But i am trying to simulate frames that i would get from network and drawing it on a SurfaceView.
Thanks for your time.
I found where the problem was coming from. I was pushing in a queue my camera preview frames from the Ui Thread. Frames that i was retrieving from the queue on another thread. The problem was that the Ui Thread was pushing more frames per seconds in the queue than the other thread could handle per second. Doing the decodeByteArray on the Ui Thread, was slowing it down, which could let time to the other thread to draw my bitmap. I changed the kind of queue that i am using to make it faster and still try to find a way to render faster in my thread. If someone has advice, i will take it.