采集bmp图象时的奇怪问题
日期:2006-07-30 荐:
采集bmp图象时的奇怪问题用vc302视频采集卡,读寄存器,并用CFile创建文件保存像素,bmp图象显示正常,可用ACDSee看图却提示"源数据流被截断",转换成jpg图象下半部分全黑。请问这是图象采集的问题,还是保存文件时出错?部分源码:int CCanontestDlg::CapVideo(){PLAYSOUND;long Y1=0;long Y2=0;long U1=0;long V1=0;float R=0.0;float G=0.0;float B=0.0;long Hcount=0;long Pcount=0;long PixNum1=0;//640*240 彩色BOOL bRet=SetPortVal(CAP_MODE,0x02,2);if(!bRet){AfxMessageBox("IO ERROR");return(0);}//像素数组BYTE Pix[460799];for(int i=1;i<=3;i ){//PLAYSOUND;bRet=SetPortVal(CAP_CTL,0x00,2);if(!bRet){AfxMessageBox("IO ERROR");}int CapSta;CapSta=_inp(CAP_STU);while( (CapSta && 0x1)== 0 )//判断采集状况{CapSta=_inp(CAP_STU);}for(Hcount=0;Hcount<=239;Hcount ){for(Pcount=0;Pcount<=319;Pcount ){Y1=_inp(CAP_YUV);U1=_inp(CAP_YUV);Y2=_inp(CAP_YUV);V1=_inp(CAP_YUV);R = Y1 1.371 * (V1 - 128);G = Y1 - 0.698 * (V1 - 128) - 0.336 * (U1 - 128);B = Y1 1.732 * (U1 - 128);if( R < 0) R = 0;if (G < 0) G = 0;if (B < 0) B = 0;if (R > 255) R = 255;if (G > 255) G = 255;if (B > 255) B = 255;PixNum1 = ((239 - Hcount) * 640 Pcount * 2) * 3;Pix[PixNum1] =B; //注意在BMP文件中每 个像素3个字节依次存放B、G、R值Pix[PixNum1 1] = G;Pix[PixNum1 2] = R;R = Y2 1.371 * (V1 - 128);G = Y2 - 0.698 * (V1 - 128) - 0.336 * (U1 - 128);B = Y2 1.732 * (U1 - 128);if (R < 0 ) R = 0;if (G < 0 ) G = 0;if (B < 0 ) B = 0;if (R > 255 ) R = 255;if (G > 255 ) G = 255;if (B > 255 ) B = 255; Pix[PixNum1 3] = B;Pix[PixNum1 4] = G;Pix[PixNum1 5] = R;}}//位图头BITMAPFILEHEADER bfh1;BITMAPINFOHEADER bih1;bfh1.bfType=0x4D42; bfh1.bfSize= 460854 ;bfh1.bfReserved1=0;bfh1.bfReserved2 =0;bfh1.bfOffBits =54;bih1.biSize =40;bih1.biWidth =640;bih1.biHeight =240;bih1.biPlanes=1;bih1.biBitCount=24;bih1.biCompression=0;bih1.biSizeImage =153600;bih1.biXPelsPerMeter =3780;bih1.biYPelsPerMeter =3780;bih1.biClrUsed=0;bih1.biClrImportant =0;CFile a("C:\\Documents and Settings\\dj\\桌面 \\zsx.bmp",CFile::modeCreate|CFile::modeWrite);a.SeekToBegin();a.Write(&bfh1,sizeof(BITMAPFILEHEADER));a.Write(&bih1,sizeof(BITMAPINFOHEADER));a.Write(Pix,460799);a.Close;}return(0);}a.Write(Pix,460799);这里常量460799应该换成实际的图象数据区字节数,具体怎么计算看BMP文件格式。460799应该改为460800240*320*6=460800"数据源被截断"已经改正但bmp转jpg仍然下半部分还是黑的(用ACDSEE转换却正常)但用我以前使用的JpgDll.dll/JpgVSbmp.dll时却出错请问有什么办法解决呢?在写入BITMAPINFOHEADER后面再写入一个RGBQUAD变量看看。另外,BITMAPFILEHEADER和BITMAPINFOHEADER结构中的bfSize成员都应该是各自的结构大小,用sizeof()获得,而不是位图大?gt;>
标签: