YUV转RGB 颜色失真问题

yqiang5152 2012-08-07 11:10:26
最近一直在研究YUV420转RGB565,纠结了N天,转换出来的图片一直是屏幕分成了4块上面两块有明显的红色块,下面两块有明显的绿色块,屏幕颜色失真。求大神指正:代码如下

fis = new FileInputStream(file);
in = new BufferedInputStream(fis);

byte[] buf = new byte[width*heigth*3/2];//视频的宽高乘以1.5 yuv420 一帧数据长度
int size = 0;
while ((size = in.read(buf)) != -1) {
byte[] content = null;
out = new ByteArrayOutputStream();
out.write(buf, 0, size);
content = out.toByteArray();


decodeYUV420SP(rgb,content,width,height);//decodeYUV420SP 在下面
Bitmap bmp = Bitmap.createBitmap(rgb,width, height,

Bitmap.Config.RGB_565);



Matrix matrix = new Matrix();
matrix.postScale(f1,f1); //长和宽放大缩小的比例
Bitmap resizeBmp =Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,true);

Config.bits[i]=resizeBmp;//存入bitmap数组中

}
static void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) {

final int frameSize = width * height;

for (int j = 0, yp = 0; j < height; j++) {
int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
for (int i = 0; i < width; i++, yp++) {
int y = (0xff & ((int) yuv420sp[yp])) - 16;
if (y < 0)
y = 0;
if ((i & 1) == 0) {
v = (0xff & yuv420sp[uvp++]) - 128;
u = (0xff & yuv420sp[uvp++]) - 128;
}

int y1192 = 1192 * y;
int r = (y1192 + 1634 * v);
int g = (y1192 - 833 * v - 400 * u);
int b = (y1192 + 2066 * u);

if (r < 0)
r = 0;
else if (r > 262143)
r = 262143;
if (g < 0)
g = 0;
else if (g > 262143)
g = 262143;
if (b < 0)
b = 0;
else if (b > 262143)
b = 262143;

rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
}
}
}
...全文
1545 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
顾小林 2013-07-18
  • 打赏
  • 举报
回复
YuvImage yuvImage = new YuvImage(yuv,ImageFormat.NV21, pictureSize.width, pictureSize.height, null); //picturesize 是YUV 是尺寸 ByteArrayOutputStream baos = new ByteArrayOutputStream(); yuvImage.compressToJpeg(new Rect(0,0,pictureSize.width,pictureSize.height), 100, baos); out = new FileOutputStream(path); byte[] data = baos.toByteArray(); 这个data 是RGB的数据。 可能和你想要的差距比较大
Snlole 2013-07-18
  • 打赏
  • 举报
回复
v = (0xff & yuv420sp[uvp++]) - 128;//抛java.lang.arrayindexoutofboundsexception异常
yqiang5152 2012-08-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

上面decodeYUV420SP的方法代码多加了字符,换成以下的:
Java code

static int[] decodeYUV420SP(byte[] yuv420sp, int width, int height)
{
final int frameSize = width * height;
int rgb[] = new int[frameSiz……
[/Quote]
感谢楼上的作答,我试了你给我的方法,creatbitmap 的时候也改用了您推荐的方法,但是问题还是没有解决,是不是算法某个地方溢出,导致颜色的失真
AMinfo 2012-08-07
  • 打赏
  • 举报
回复
上面decodeYUV420SP的方法代码多加了字符,换成以下的:

static int[] decodeYUV420SP(byte[] yuv420sp, int width, int height)
{
final int frameSize = width * height;
int rgb[] = new int[frameSize];
for (int j = 0, yp = 0; j < height; j++) {
int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
for (int i = 0; i < width; i++, yp++) {
int y = (0xff & ((int) yuv420sp[yp])) - 16;
if (y < 0)
y = 0;
if ((i & 1) == 0) {
v = (0xff & yuv420sp[uvp++]) - 128;
u = (0xff & yuv420sp[uvp++]) - 128;
}
int y1192 = 1192 * y;
int r = (y1192 + 1634 * v);
int g = (y1192 - 833 * v - 400 * u);
int b = (y1192 + 2066 * u);
if (r < 0)
r = 0;
else if (r > 262143)
r = 262143;
if (g < 0)
g = 0;
else if (g > 262143)
g = 262143;
if (b < 0)
b = 0;
else if (b > 262143)
b = 262143;
rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000)
| ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
}
}
return rgb;
}

AMinfo 2012-08-07
  • 打赏
  • 举报
回复
尝试将

Bitmap bmp = Bitmap.createBitmap(rgb,width, height,

Bitmap.Config.RGB_565);
改为
Bitmap bmp = Bitmap.createBitmap(rgb, 0, width, width, height,
android.graphics.Bitmap.Config.RGB_565);


或者也有可能是你这个rgb值处理不正确,将static void decodeYUV420SP整个方法改一下

static int[] decodeYUV420SP[](byte[] yuv420sp, int width, int height)
{
final int frameSize = width * height;
int rgb[] = new int[frameSize];
for (int j = 0, yp = 0; j < height; j++) {
int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
for (int i = 0; i < width; i++, yp++) {
int y = (0xff & ((int) yuv420sp[yp])) - 16;
if (y < 0)
y = 0;
if ((i & 1) == 0) {
v = (0xff & yuv420sp[uvp++]) - 128;
u = (0xff & yuv420sp[uvp++]) - 128;
}
int y1192 = 1192 * y;
int r = (y1192 + 1634 * v);
int g = (y1192 - 833 * v - 400 * u);
int b = (y1192 + 2066 * u);
if (r < 0)
r = 0;
else if (r > 262143)
r = 262143;
if (g < 0)
g = 0;
else if (g > 262143)
g = 262143;
if (b < 0)
b = 0;
else if (b > 262143)
b = 262143;
rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000)
| ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
}
}
return rgb;
}


然后将这一句
decodeYUV420SP(rgb,content,width,height);
改为
int rgb[] = decodeYUV420SP(content,width,height);
多媒体计算机技术19春在线作业2-0003 要把一台普通的计算机变成多媒体计算机要解决的关键技术是: (1)视频音频信号的获取 (2)多媒体数据压编码和解码技术 (3)视频音频数据的实时处理和特技 (4)视频音频数据的输出技术 选项【A】:(1)(2)(3) 选项【B】:(1)(2)(4) 选项【C】:(1)(3)(4) 选项【D】:全部 正确选项:D 下列数组声明语句中,正确的是 选项【A】:Dim A [3,4] As Integer 选项【B】:Dim A (3,4) As Integer 选项【C】:Dim A [3;4] As Integer 奥鹏作业多媒体计算机技术在线作业2-0003全文共19页,当前为第1页。选项【D】:Dim A (3;4) As Integer 奥鹏作业多媒体计算机技术在线作业2-0003全文共19页,当前为第1页。 正确选项:B 下列程数据类型中,不属于VBA的是 选项【A】:长整型 选项【B】:布尔型 选项【C】:变体型 选项【D】:指针型 正确选项:D Authorwre是基于()的多媒体集成软件。 选项【A】:幻灯片 选项【B】:页面和卡片 选项【C】:时间线 选项【D】:图标 正确选项:D 奥鹏作业多媒体计算机技术在线作业2-0003全文共19页,当前为第2页。 奥鹏作业多媒体计算机技术在线作业2-0003全文共19页,当前为第2页。 数字视频编码的方式有哪些: (1)RGB视频 (2)YUV视频 (3)Y/C(S)视频 (4)复合视频 选项【A】:仅(1) 选项【B】:(1)(2) 选项【C】:(1)(2)(3) 选项【D】:全部 正确选项:D 在视频加工中对过度效果的设置中,要把相应的效果拖到()。 选项【A】:两视频之间 选项【B】:第一个视频的开头 选项【C】:第二个视频的结尾 选项【D】:任何地方 正确选项:A 奥鹏作业多媒体计算机技术在线作业2-0003全文共19页,当前为第3页。 奥鹏作业多媒体计算机技术在线作业2-0003全文共19页,当前为第3页。 要录制声音,除了要具备声卡、麦克风等硬件设备外,还要具备录音软件,下列不属于录音软件的是() 选项【A】:Windows的"录音机" 选项【B】:Goldware 选项【C】:Sound Farge 选项【D】:Media Player 正确选项:D 在Authorwre中,要制作背景音乐,需要在声音图标的属性的Timing选项中选择() 选项【A】:等待(Wait Until Done) 选项【B】:插入(Insert) 选项【C】:导入(Import) 选项【D】:同步(Concurrent) 正确选项:D 奥鹏作业多媒体计算机技术在线作业2-0003全文共19页,当前为第4页。关于文件的压缩,以下说法正确的是() 奥鹏作业多媒体计算机技术在线作业2-0003全文共19页,当前为第4页。 选项【A】:文本文件与图形图像都可以采用有损压缩 选项【B】:文本文件与图形图像都不可以采用有损压缩 选项【C】:文本文件可以采用有损压缩,图形图像不可以 选项【D】:图形图像可以采用有损压缩,文本文件不可以 正确选项:D 下列文件格式存储的图像,在缩放过程中不易失真的是() 选项【A】:.bmp 选项【B】:.psd 选项【C】:.jpg 选项【D】:.cdr 正确选项:D 下列方法中,不能从CD上获取声音的是() 奥鹏作业多媒体计算机技术在线作业2-0003全文共19页,当前为第5页。 选项【A】:直接从CD上复制并粘贴到硬盘上 奥鹏作业多媒体计算机技术在线作业2-0003全文共19页,当前为第5页。 选项【B】:利用超级解霸工具把整个音轨抓取下来 选项【C】:边播放CD上的声音,边用Total Recorder录制 选项【D】:利用Windows Media Player的"从CD复制"功能 正确选项:A MI##I音频文件是() 选项【A】:一种波形文件 选项【B】:一种采用P 选项【C】:M压缩的波形文件 选项【D】:是MP3的一种格式 选项【E】:是一种符号化的音频信号,记录的是一种指令序列。 正确选项:D 下列选项中,不属于Access数据类型的是 选项【A】:数字 奥鹏作业多媒体计算机技术在线作业2-0003全文共19页,当前为第6页。选项【B】:文本 奥鹏作业多媒体计算机技术在线作业2-0003全文共19页,当前为第6页。 选项【C】:报表 选项【D】:时间/日期 正确选项:C ( )是将声音变换为数字化信息,又将数字化信息变换为声音的设备。 选项【A】:音箱 选项【B】:音响 选项【C】:声卡 选项【D】:PCI卡 正确选项:C 如果按三个色差信号B-Y,R-Y,G-Y来传输彩色全电视信号,会造成( )失真

80,348

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧