Converting YUV- RGB(Image processing)- YUV during onPreviewFrame in android?
Why not specify that camera preview should provide RGB images?
i.e. Camera.Parameters.setPreviewFormat(ImageFormat.RGB_565);
Converting YUV to YUV420 Android onPreview
I found a very straight forward way to convert from NV12 to YV12/YUV 420. Two lines of code did the job for me:
public void surfaceCreated(SurfaceHolder holder) {
try {
// set view of surface in cameraview
Camera.Parameters p = mCamera.getParameters();
p.setPreviewFormat(ImageFormat.YV12);
mCamera.setParameters(p);
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
}
}
YUV image processing
I'm assuming you're using the Camera API's preview callbacks, where you get a byte[] array of data for each frame.
First, you need to select which YUV format you want to use. NV21 is required to be supported, and YV12 is required since Android 3.0. None of the other formats are guaranteed to be available. So NV21 is the safest choice, and also the default.
Both of these formats are YUV 4:2:0 formats; the color information is subsampled by 2x in both dimensions, and the layout of the image data is fairly different from the standard interleaved RGB format. FourCC.org's NV21 description, as one source, has the layout information you want - first the Y plane, then the UV data interleaved. But since the two color planes are only 1/4 of the size of the Y plane, you'll have to decide how you want to upsample them - the simplest is nearest neighbor. So if you want pixel (x,y) from the image of size (w, h), the nearest neighbor approach is:
Y = image[ y * w + x];
U = image[ w * h + floor(y/2) * (w/2) + floor(x/2) + 1]
V = image[ w * h + floor(y/2) * (w/2) + floor(x/2) + 0]
More sophisticated upsampling (bilinear, cubic, etc) for the chroma channels can be used as well, but what's suitable depends on the application.
Once you have the YUV pixel, you'll need to interpret it. If you're more comfortable operating in RGB, you can use these JPEG conversion equations at Wikipedia to get the RGB values.
Or, you can just use large positive values of V (Cr) to indicate red, especially if U (Cb) is small.
Related Topics
Webview Link Click Open Default Browser
Stopping & Starting Music on Incoming Calls
What Is Meaning of Boolean Value Returned from an Event-Handling Method in Android
Actionbar - Custom View with Centered Imageview, Action Items on Sides
Android Fade in and Fade Out with Imageview
How to Add Footer to Navigationview - Android Support Design Library
Android - Volume Buttons Used in My Application
Detecting When User Has Dismissed the Soft Keyboard
How to Make Circle Custom Progress Bar in Android
Taking Pictures with Camera on Android Programmatically
How to Add Window -- Token Android.Os.Binderproxy Is Not Valid; Is Your Activity Running