Vitaly
Vitaly

Reputation: 30

Working with raw imageData in onPictureTaken

I've implemented functionality to use camera to take pictures. Instead of storing it I want to do some simple picture processing (very simple OCR - mostly to try own ability to handle such tasks), for which I need to convert imageData in public void onPictureTaken(byte[] imageData, Camera c) into greyscale - 2d byte array. The only code sample I found was to convert image to bitmap using bitmapFactory ( Image Processing on Android ). My question is - can someone advise on all the raw formats and how to extract data out of them? Or maybe my best guess is to get bitmap and scale it down to 2d byte array?

Any links on the topic are very appreciated.

Upvotes: 1

Views: 1833

Answers (3)

Vyacheslav
Vyacheslav

Reputation: 27221

May be this answers is not useful for author of the topic but it may be interesting for the other people how wanna use camera raw data for his/her own purposes. It is ffmpeg. It can be compiled for android. for instance there is a project which can give a way how to do it. https://github.com/guardianproject/android-ffmpeg-java https://www.ffmpeg.org/ Android camera can feed ffmpeg by this command ffmpeg -f rawvideo -pixel_format nv21 -video_size 320x240 -framerate 15

Upvotes: 0

gadget
gadget

Reputation: 2112

I realize this answer is a bit late, but here's what I have learned over the past month, while working with the camera.

Whether you receiver raw image data via onPictureTaken or onPreviewFrame it can come encoded in sereral different formats (android.graphics.ImageFormat). The only one that is guaranteed universal support (according to the docs) is NV21, YV12 is universally supported from honeycomb and up. Besides being universally supported, NV21 might fit very well to what you're trying to do (convert into black and white). NV21 is a YUV format, where the Y channel carries luminosity information, while U and V channels carry color information. See a more formal description here:

http://en.wikipedia.org/wiki/YUV
http://www.fourcc.org/fccyvrgb.php
http://www.fourcc.org/yuv.php#NV21

The wiki link has a sample source that achieves NV21->RGB conversion (not very efficient as is, but gives a good idea of the general approach). Should you be interested in doing this conversion in C or assembly, this was a good source of info (warning: there is no code for NV21, and my quick modifications to handle NV21 led to nothing good). Nevertheless, it shows how to incorporate .S files into ndk projects, which is actually really easy.

http://www.roman10.net/effective-color-conversion-yuv-rgb-in-android-in-assembly/

Back to your question - conversion into B/W. Simply take the Y channel and form RGB triples as [Y,Y,Y] and that will give you a black and white image. Or maybe simply make an L8 image out of it (eg, Y->L).

PS. There is a great deal of confusion regarding range/scaling of YUV values during transformation to RGB. I found that camera data comes in full-swing (0-255, not 16-235 or 16-240), and that clamping the result to 0-255 after matrix multiplication gives a visually pleasing result, while scaling to 0-255 distorts the colors (needless to say, this is still true if you scale to some other range, such as 16-240). These observations appear to go against some of the finer aspects of the full transformation procedure described in the links above, but that's what I found to work.

Upvotes: 1

Konstantin Pribluda
Konstantin Pribluda

Reputation: 12367

Come over to the dark side - we have cookies:

http://sourceforge.net/projects/javaocr/

... and also working OCR on android. There is no release of actual geatures, so look in the source repository. Feel free to join developers list and ask questions there ( and of cource to contribute to project)

Upvotes: 0

Related Questions