There are several tutorials out there which explain how to get a simple camera preview up and running on an android device. But i couldn’t find any example which explains how to manipulate the image before it’s being rendered.
What I want to do is implementing custom color filters to simulate e.g. red and/or green deficiency.
There are several tutorials out there which explain how to get a simple camera
Share
I did some research on this and put together a working(ish) example. Here’s what I found. It’s pretty easy to get the raw data coming off of the camera. It’s returned as a YUV byte array. You’d need to draw it manually onto a surface in order to be able to modify it. To do that you’d need to have a SurfaceView that you can manually run draw calls with. There are a couple of flags you can set that accomplish that.
In order to do the draw call manually you’d need to convert the byte array into a bitmap of some sort. Bitmaps and the BitmapDecoder don’t seem to handle the YUV byte array very well at this point. There’s been a bug filed for this but don’t don’t what the status is on that. So people have been trying to decode the byte array into an RGB format themselves.
Seems like doing the decoding manually has been kinda slow and people have had various degrees of success with it. Something like this should probably really be done with native code at the NDK level.
Still, it is possible to get it working. Also, my little demo is just me spending a couple of hours hacking stuff together (I guess doing this caught my imagination a little too much ;)). So chances are with some tweaking you could much improve what I’ve managed to get working.
This little code snip contains a couple of other gems I found as well. If all you want is to be able to draw over the surface you can override the surface’s onDraw function – you could potentially analyze the returned camera image and draw an overlay – it’d be much faster than trying to process every frame. Also, I changed the SurfaceHolder.SURFACE_TYPE_NORMAL from what would be needed if you wanted a camera preview to show up. So a couple of changes to the code – the commented out code:
And the:
Should allow you to overlay frames based on the camera preview on top of the real preview.
Anyway, here’s a working piece of code – Should give you something to start with.
Just put a line of code in one of your views like this:
And include this class in your source somewhere: