1

I need to show a camera preview in the SurfaceView with delay about 5 seconds. So, I think I need somehow to capture Frames from a camera before they go to the SurfaceView and put them to the buffer, and then when buffer will be full, get a stored Frames from the buffer and show them to the SurfaceView.

But I don't know how to get frames before they will be drawn on the SurfaceView.

I only know how to get frames from PreviewCallback, onPreviewFrame(byte[] data, Camera camera) method:

    PreviewCallback previewCb = new PreviewCallback() {
    public void onPreviewFrame(byte[] data, Camera camera) {
    // byte[] data is the Frame

    }
};

But I don't know how to get a Frames from a camera directly, to store them to the buffer, and then restore the Frames from buffer to the SurfaceView.

Any help is very appreciated.

DeniSHow
  • 1,394
  • 1
  • 18
  • 30
  • 1
    Seems like you already know the answer -- capture the frames with a preview callback, then render the frames to the SurfaceView, rather than feeding the preview directly to the SurfaceView. You still need a preview Surface, for which you can use a SurfaceTexture; see e.g. http://stackoverflow.com/questions/22462360/android-use-camera-without-surfaceview-or-textureview/ – fadden Dec 16 '15 at 17:18
  • Thanks a lot! It helped me to understood the problem better and I finally have found some really nice examples, exactly what I was looking for on the GitHub. – DeniSHow Dec 19 '15 at 10:18

1 Answers1

0

Finally, I ended up working with OpenCV. There is a nice example in the samples folder which name is "tutorial-1-camerapreview". There I have method :

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    return inputFrame.rgba();
}

So, I can do what I want with frames inside this method and then just return them.

Also, maybe to someone it can be useful. I have found a nice example of how to get a frames from onPreviewFrame, then convert them from yuv into rgb format using jni (because it faster.. I think so..) and then draw the frames to the custom SurfaceView. Example 1. Example 2.

Hope it will be useful to someone.

DeniSHow
  • 1,394
  • 1
  • 18
  • 30
  • I am afraid there is usually not enough memory to keep 5 sec of high resolution preview frames. – Alex Cohn Dec 23 '15 at 17:39
  • 1
    By the way, it may be much faster to use [renderscript intrinsic converter](http://developer.android.com/reference/android/renderscript/ScriptIntrinsicYuvToRGB.html) – Alex Cohn Dec 23 '15 at 17:46
  • Thanks for comment! I'm actually need less than 5 seconds of delay, just about 1 sec or 30 frames. With OpenCV I did it succesfully, but I had to use the System.gc() method, in order to clear heap after each frame have been stored to the buffer. But with RenderScript I've got some errors http://stackoverflow.com/questions/34453174/renderscript-scriptintrinsicyuvtorgb-and-bitmap-allocation I think I incorrectly work with allocation of a memory... – DeniSHow Dec 24 '15 at 13:03