Created
March 15, 2011 08:07
-
-
Save granoeste/870445 to your computer and use it in GitHub Desktop.
[Android] RGB to YUV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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