NatCam Tutorial Series 6: BlurCam App
Hello guys. This is more or less the second part of the tutorial on NatCam’s Preview Data. Before we begin, make sure you check out the tutorials on Starting Off, Controls, Photos, Videos, and Goodies. In this tutorial, we will be getting much more hands on. We will build BlurCam, an iOS application that uses the NatCam preview data pipeline and the iOS Accelerate framework to create a realtime blur of the camera preview. Let’s begin.
Our project will consist of
C# sources for using NatCam in Unity and
C sources for the native code on iOS. As usual, we start off with a blank project:
Now let us create our BlurCam script:
Let’s break down what’s going on here. First, in
OnStart, we ask NatCam for the preview buffer so that we can get its dimensions (width, height, size). We need this information so that we can create a blur texture up front. Now, you might wonder why we can’t just use
NatCam.Preview.width/height. This is because the size of the preview buffer is not guaranteed to be the same size as the preview texture. This discrepancy is due to padding on some devices.
OnFrame, we will get the handle to the preview buffer, send that handle to our native
C code for blurring, then load our blurred data into the
blurPreview texture. Let’s start writing some C code. Create a
BlurCam.c file in
Assets>Plugins>iOS (create the directories if not present).
Our native code uses
Accelerate.framework to perform a very fast blur. Now, let’s go back to
C# and add the managed entry point for the function:
Now, build and run! You should see that the camera preview is blurred. If you reduce the preview resolution, the blur effect will be more pronounced.
We’re not done :D We’re close though. There’s just one last thing we need to pay attention to: Memory. If you noticed, we allocate the native
blurFrame buffer while we’re running, and we never free it when we’re done with it. So let’s do that:
That’s it. We’re all done!
In the next tutorial, we’ll be going over using OpenCV with NatCam. After that, we’ll start working together on an exciting tutorial project: MoodCam VR. Here’s the BlurCam project on GitHub. Till next time.
— Lanre Olokoba.