How to convert RGB -> YUV -> RGB (both ways)

Yes, invertible transformations exist. equasys GmbH posted invertible transformations from RGB to YUV, YCbCr, and YPbPr, along with explanations of which situation each is appropriate for, what this clamping is really about, and links to references. (Like a good SO answer.) For my own application (jpg images, not analog voltages) YCbCr was appropriate, so I … Read more

Converting preview frame to bitmap

Alternatively, if you DO need a bitmap for some reason, and/or want to do this without creating a YUVImage and compressing to JPEG, you can use the handy RenderScript ‘ScriptIntrinsicYuvToRGB’ (API 17+): @Override public void onPreviewFrame(byte[] data, Camera camera) { Bitmap bitmap = Bitmap.createBitmap(r.width(), r.height(), Bitmap.Config.ARGB_8888); Allocation bmData = renderScriptNV21ToRGBA8888( mContext, r.width(), r.height(), data); bmData.copyTo(bitmap); … Read more

How to convert RGB from YUV420p for ffmpeg encoder?

You can use libswscale from FFmpeg like this: #include <libswscale/swscale.h> SwsContext * ctx = sws_getContext(imgWidth, imgHeight, AV_PIX_FMT_RGB24, imgWidth, imgHeight, AV_PIX_FMT_YUV420P, 0, 0, 0, 0); uint8_t * inData[1] = { rgb24Data }; // RGB24 have one plane int inLinesize[1] = { 3*imgWidth }; // RGB stride sws_scale(ctx, inData, inLinesize, 0, imgHeight, dst_picture.data, dst_picture.linesize); Note that you … Read more

Displaying YUV Image in Android

Following code solve your problem and it may take less time on Yuv Format data because YuvImage class is provided with Android-SDK. You can try this, ByteArrayOutputStream out = new ByteArrayOutputStream(); YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, width, height, null); yuvImage.compressToJpeg(new Rect(0, 0, width, height), 50, out); byte[] imageBytes = out.toByteArray(); Bitmap image = BitmapFactory.decodeByteArray(imageBytes, … Read more

Rotate an YUV byte array on Android

The following method can rotate a YUV420 byte array by 90 degree. private byte[] rotateYUV420Degree90(byte[] data, int imageWidth, int imageHeight) { byte [] yuv = new byte[imageWidth*imageHeight*3/2]; // Rotate the Y luma int i = 0; for(int x = 0;x < imageWidth;x++) { for(int y = imageHeight-1;y >= 0;y–) { yuv[i] = data[y*imageWidth+x]; i++; } … Read more

Convert bitmap array to YUV (YCbCr NV21)

Here is some code that actually works: // untested function byte [] getNV21(int inputWidth, int inputHeight, Bitmap scaled) { int [] argb = new int[inputWidth * inputHeight]; scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight); byte [] yuv = new byte[inputWidth*inputHeight*3/2]; encodeYUV420SP(yuv, argb, inputWidth, inputHeight); scaled.recycle(); return yuv; } void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, … Read more

Converting YUV->RGB(Image processing)->YUV during onPreviewFrame in android?

Although the documentation suggests that you can set which format the image data should arrive from the camera in, in practice you often have a choice of one: NV21, a YUV format. For lots of information on this format see http://www.fourcc.org/yuv.php#NV21 and for information on the theory behind converting it to RGB see http://www.fourcc.org/fccyvrgb.php. There … Read more