Skip to content

Instantly share code, notes, and snippets.

@granoeste
Created March 15, 2011 08:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save granoeste/870445 to your computer and use it in GitHub Desktop.
Save granoeste/870445 to your computer and use it in GitHub Desktop.
[Android] RGB to YUV
// from http://d.hatena.ne.jp/Superdry/20110130/1296405341
private int[] convertRGB2YUV(int color) {
ColorMatrix cm = new ColorMatrix();
cm.setRGB2YUV();
final float[] yuvArray = cm.getArray();
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
int[] result = new int[3];
// Adding a 127 U and V.
result[0] = floatToByte(yuvArray[0] * r + yuvArray[1] * g + yuvArray[2] * b);
result[1] = floatToByte(yuvArray[5] * r + yuvArray[6] * g + yuvArray[7] * b) + 127;
result[2] = floatToByte(yuvArray[10] * r + yuvArray[11] * g + yuvArray[12] * b) + 127;
return result;
}
private int floatToByte(float x) {
int n = java.lang.Math.round(x);
return n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment