一般的視頻采集設備輸出的數據都是YUV格式早敬,
1.什么是YUV
YUV是一種圖像編碼方式 ,其中Y表示明亮度(Luminance大脉、Luma)搞监,也就是灰階值桶雀。
U磨淌、V 表示色度(Chrominance 或 Chroma),描述的是色調和飽和度嘀韧。
2.為什么要用YUV格式
相比大家熟悉的RGB編碼方式秤标,YUV格式將圖片的亮度和色度信息分開存儲棍矛。為什么做?有什么好處呢抛杨?
由于人眼的視網膜桿細胞多于視網膜錐細胞,其中視網膜桿細胞的作用就是識別亮度荐类,視網膜錐細胞的作用就是識別色度怖现,所以人眼對亮度比較敏感,而對色度沒那么敏感玉罐。利用這個特性屈嗤,我們在采樣的時候可以盡量保留亮度信息(Y),適當減少色度信息(UV)吊输,人眼也很難辨別出來饶号。因此相比RGB編碼方式,YUV能節(jié)省不少存儲空間季蚂。
YUV格式運用十分廣泛茫船,從電視機到各種視頻采集和顯示設備,都是用的YUV格式扭屁。
3.YUV采樣格式
YUV 圖像的主流采樣方式有如下三種:
- YUV 4:4:4
- YUV 4:2:2
- YUV 4:2:0
YUV 4:4:4 每個像素都采樣一個Y算谈、U、V分量料滥,占用 3 個字節(jié)然眼。
YUV 4:2:2 每個像素采樣一個Y分量,兩個像素共用一個UV分量葵腹,整體算下來每個像素點占用2個字節(jié)高每。
YUV 4:2:0 每個像素采樣一個Y分量, 4個像素共用一個UV分量屿岂,整體算下來每個像素點占用1.5個字節(jié)。
4.YUV存儲
YUV420P是基于 planar 平面模式進行存儲鲸匿,先存儲所有的 Y 分量爷怀,然后存儲所有的 U 分量,最后存儲所有的 V 分量晒骇。
YUV420SP 先存儲所有的 Y 分量霉撵,然后 UV 交替存儲
5.YUV操作
<center><font color=gray size=2>原圖</font></center>
1)提取Y分量
public static byte[] splitY(byte[] yuv420p, int w, int h) {
int frame = w * h;
byte[] y = new byte[frame];
System.arraycopy(yuv420p, 0, y, 0, y.length);
return y;
}
2)提取U分量
public static byte[] splitU(byte[] yuv420p, int w, int h) {
int frame = w * h;
byte[] u = new byte[frame / 4];
System.arraycopy(yuv420p, frame, u, 0, u.length);
return u;
}
3)提取V分量
public static byte[] splitV(byte[] yuv420p, int w, int h) {
int frame = w * h;
byte[] v = new byte[frame / 4];
System.arraycopy(yuv420p, frame + frame / 4, v, 0, v.length);
return v;
}
4)圖片亮度減半
public static void halfLuminance(byte[] yuv420p, int w, int h) {
int frame = w * h;
for (int i = 0; i < frame; i++) {
int luminance = NumberUtils.byteToInt(yuv420p[i]);
yuv420p[i] = NumberUtils.intToByte(luminance / 2);
}
}
項目地址:
Gitee:https://gitee.com/huaisu2020/Android-Live
Github:https://github.com/xh2009cn/Android-Live