视频采集,后远程存储视频实现->spcaview代码的修改
如何在spcaclient中实现存储功能,现在可以显示,但是存储不行,在从 socket处读出数据后,如何转换存储到一个打开的文件outputfile中????
//---------------------------------------------------------------------------------------------------------------------------------------------------------
int readjpeg(int sock, unsigned char **buf,struct frame_t *headerframe,struct client_t *message,int statOn, avi_t *outfile)
{
int byteread,bytewrite;
bytewrite = write_sock(sock,(unsigned char*)message,sizeof(struct client_t));
// is sleeping ?
if ((byteread= read_sock(sock,(unsigned char*)headerframe,sizeof(struct frame_t))) < 0){
printf("Seem server is gone !! try later \n");
goto error;
}
//memcpy( tmp, headerframe, sizeof( struct frame_t ));
if(statOn)
printf (" key %s nb %d width %d height %d times %dms size %d \n",headerframe->header,
headerframe->nbframe,headerframe->w,headerframe->h,headerframe->deltatimes,headerframe->size);
if(headerframe->size && !headerframe->wakeup){
//if(headerframe->size){
*buf=(unsigned char*) realloc(*buf,headerframe->size);
if((byteread = read_sock(sock,*buf,headerframe->size)) < 0){
printf("Seem server is gone !! try later \n");
goto error;
}
printf( "headerframe->size = %d\n", headerframe->size );
}
//tmp = headerframe;
//if (AVI_write_frame(outfile,(unsigned char *)tmp,400*300*1.5) < 0)
//{
//printf ("[2] > write error on avi out \n");
//free (jpegData);
//free(dpshDest);
//free(buf);
//}
//printf("buf read %d \n",byteread);
if(headerframe->acknowledge)
reset_callbackmessage(message);
usleep(5000);
return ((headerframe->wakeup)?0:(headerframe->size));
//return (headerframe->size);
error:
return -1;
}
//---------------------------------------------------------------------------------------------------------
int spcaClient (char *Ip, short port,int owidth, int oheight, int statOn)
{
struct frame_t *headerframe;
struct client_t *messcallback;
unsigned char *buf = NULL;
int width,height;
int jpegsize;
int sock_client;
int run = 1;
int quit =1;
int keypressed =0;
int bpp = 3;
SDL_Surface *pscreen;
SDL_Event sdlevent;
unsigned char* p= NULL;
unsigned char *picture = NULL;
struct tm *tdate;
time_t curdate;
char titre[21];
avi_t *out_fd;
char *outputfile = "testavistore.avi";
//char *outputfile = "testjpeg.jpg";
int image_width = 320;
int image_height = 480;
char fourcc[4];
int framecount = 0;
Uint32 time = 0;
/*----------------------------------*/
//compressor=="RGB2"
/*format = VIDEO_PALETTE_RGB565;*/
//int bpp = 2;
//time = SDL_GetTicks ();
if (outputfile)
{
if ((out_fd = AVI_open_output_file (outputfile)) == NULL)
{
printf ("cannot open write file ? \n");
exit (1);
}
}
//format = VIDEO_PALETTE_YUV420P;
//bpp = 3;
snprintf (fourcc, 5, "I420");
//snprintf (fourcc, 5, "RGB2");
AVI_set_video (out_fd, image_width, image_height, 20,fourcc);
init_sdlall();
sock_client = open_clientsock(Ip,port);
headerframe=(struct frame_t*)malloc(sizeof(struct frame_t));
printf( "******headerframe->size = %d\n", headerframe->size );
messcallback=(struct client_t*)malloc(sizeof(struct client_t));
init_callbackmessage(messcallback);
//if ((jpegsize = readjpeg(sock_client,&buf,headerframe,messcallback,statOn)) < 0)
if ((jpegsize = readjpeg(sock_client,&buf,headerframe,messcallback,statOn, out_fd)) < 0)
{
printf("got size = 0 \n");
goto error;
}
// else
// {
// framecount++;
// printf("the first frame write ok !");
// }
width = headerframe->w;
height = headerframe->h;
if(!owidth || !oheight){
owidth = width;
oheight = height;
}
if(videoOk) {
pscreen = SDL_SetVideoMode (owidth, oheight, bpp * 8,
SDL_DOUBLEBUF | SDL_SWSURFACE);
p=pscreen->pixels;
}
do{
//if((jpegsize = readjpeg(sock_client,&buf,headerframe,messcallback,statOn)) < 0)
if ((jpegsize = readjpeg(sock_client,&buf,headerframe,messcallback,statOn, out_fd)) < 0)
{
// printf(" No size !!! exit fatal \n");
goto error;
}
// else
// {
// framecount++;
// printf("the %d (th) framecount frame write ok !",framecount);
// }
/* mode sleep off */
if(!jpegsize && videoOk)
close_sdlvideo();
if( !videoOk && jpegsize){
init_sdlvideo();
pscreen = SDL_SetVideoMode (owidth, oheight, bpp * 8,
SDL_DOUBLEBUF | SDL_SWSURFACE);
p=pscreen->pixels;
}
curdate = (time_t) (headerframe->seqtimes / 1000);
tdate = localtime(&curdate);
snprintf (titre,21,"%02d/%02d/%04d-%02d:%02d:%02d\0",
tdate->tm_mday, tdate->tm_mon + 1, tdate->tm_year + 1900,
tdate->tm_hour, tdate->tm_min, tdate->tm_sec);
if(jpegsize && videoOk){
jpeg_decode(&picture,buf,&width,&height);
resize (p,picture,owidth,oheight,width,height) ;
SDL_WM_SetCaption (titre, NULL);
SDL_Flip (pscreen);
}
printf("bright %05d contrast %05d \r",headerframe->bright,headerframe->contrast);
fflush (stdout);
switch (run){
case 1:
if (SDL_PollEvent (&sdlevent) == 1) {
switch (sdlevent.type) {
case SDL_KEYDOWN:
switch (sdlevent.key.keysym.sym) {
case SDLK_s:
//getPicture(buf,jpegsize);
getJpegPicture(buf,width,height,
VIDEO_PALETTE_JPEG,jpegsize,PICTURE,NULL);
break;
case SDLK_w:
messcallback->updocontrast =2;
break;
case SDLK_x:
messcallback->updocontrast =1;
break;
case SDLK_b:
messcallback->updobright=1;
break;
case SDLK_n:
messcallback->updobright=2;
break;
case SDLK_l:
messcallback->sleepon=1;
break;
case SDLK_c:
messcallback->sleepon=2;
break;
case SDLK_j:
messcallback->updoexposure=1;
break;
case SDLK_d:
messcallback->updosize=1; //workaround change quality index
break;
case SDLK_f:
messcallback->updosize=2; //workaround change quality index
break;
case SDLK_g:
messcallback->fps=1; // decrease time_interval
break;
case SDLK_h:
messcallback->fps=2; // increase time interval
break;
case SDLK_UP:
if(messcallback->y -1 > 0)
messcallback->y--;
keypressed = SDLK_UP ; run = 2;
break;
case SDLK_DOWN:
if(messcallback->y +1 < 256)
messcallback->y++;
keypressed = SDLK_DOWN; run = 2;
break;
case SDLK_RIGHT:
if(messcallback->x -1 > 0)
messcallback->x--;
keypressed = SDLK_RIGHT; run = 2;
break;
case SDLK_LEFT:
if(messcallback->x +1 < 256)
messcallback->x++;
keypressed = SDLK_LEFT; run = 2;
break;
case SDLK_SPACE:
run = 0;
break;
case SDLK_q:
quit =0;
break;
}
break;
case SDL_QUIT:
quit = 0;
break;
}
} //end Poll event
break;
case 0:
if (SDL_WaitEvent (&sdlevent) == 1) {
switch (sdlevent.type) {
case SDL_KEYDOWN:
switch (sdlevent.key.keysym.sym) {
case SDLK_s:
//getPicture(buf,jpegsize);
break;
case SDLK_SPACE:
run = 1;
break;
case SDLK_q:
quit =0;
break;
}
break;
case SDL_QUIT:
quit = 0;
break;
}
} //end wait event
break;
case 2:{
if (SDL_PollEvent (&sdlevent) == 1) {
switch (sdlevent.type) {
case SDL_KEYDOWN:
keypressed = 0;
run = 1;
break;
} //end event type poll 2
}
switch (keypressed){
case SDLK_UP:
if(messcallback->y -1 > 0)
messcallback->y--;
break;
case SDLK_DOWN:
if(messcallback->y +1 < 256)
messcallback->y++;
break;
case SDLK_RIGHT:
if(messcallback->x -1 > 0)
messcallback->x--;
break;
case SDLK_LEFT:
if(messcallback->x +1 < 256)
messcallback->x++;
break;
default:
break;
}
} // end case 2
break;
}
}while(quit);
error:
if(picture){
free(picture);
picture = NULL;
}
close_sock(sock_client);
free(buf);
free(messcallback);
free(headerframe);
SDL_Quit ();
//AVI_close (out_fd);
if (outputfile)
{
//if (!interval)
// out_fd->fps = (double) framecount *1000 / time;
AVI_close (out_fd);
printf ("close avi\n");
//free (jpegData);
//free(dpshDest);
}
return 0;
}
[ 本帖最后由 dreamice 于 2008-11-13 22:36 编辑 ]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只要收到数据了,把它存到一个文件里,应该是没有问题的
能不能通过网络 存储在网络上的另一台机器上?
spcaclient可以从arm端接收到图像数据,也能显示,但现在的问题的存储问题,我写入一个test.avi文件的数据,不清楚它的格式,不清楚如何处理再写入进去,spcaclient的代码如下:如何存储呢?
int spcaClient (char *Ip, short port,int owidth, int oheight, int statOn)
{
struct frame_t *headerframe;
struct client_t *messcallback;
unsigned char *buf = NULL;
int width,height;
int jpegsize;
int sock_client;
int run = 1;
int quit =1;
int keypressed =0;
int bpp = 3;
SDL_Surface *pscreen;
SDL_Event sdlevent;
unsigned char* p= NULL;
unsigned char *picture = NULL;
struct tm *tdate;
time_t curdate;
char titre[21];
int frame_size = 0;
char fourcc[4];
char *outputfile = "testavistore.avi";
avi_t *out_fd;
if (outputfile)
{
if ((out_fd = AVI_open_output_file (outputfile)) == NULL)
{
printf ("cannot open write file ? \n");
exit (1);
}
}
snprintf (fourcc, 5, "I420");
AVI_set_video (out_fd, owidth, oheight, 20,fourcc);
init_sdlall();
sock_client = open_clientsock(Ip,port);
headerframe=(struct frame_t*)malloc(sizeof(struct frame_t));
messcallback=(struct client_t*)malloc(sizeof(struct client_t));
init_callbackmessage(messcallback);
if ((jpegsize = readjpeg(sock_client,&buf,headerframe,messcallback,statOn)) < 0){
printf("got size = 0 \n");
goto error;
}
width = headerframe->w;
height = headerframe->h;
if(!owidth || !oheight){
owidth = width;
oheight = height;
}
if(videoOk) {
pscreen = SDL_SetVideoMode (owidth, oheight, bpp * 8,
SDL_DOUBLEBUF | SDL_SWSURFACE);
p=pscreen->pixels;
}
do{
if((jpegsize = readjpeg(sock_client,&buf,headerframe,messcallback,statOn)) < 0){
// printf(" No size !!! exit fatal \n");
goto error;
}
/* mode sleep off */
if(!jpegsize && videoOk)
close_sdlvideo();
if( !videoOk && jpegsize){
init_sdlvideo();
pscreen = SDL_SetVideoMode (owidth, oheight, bpp * 8,
SDL_DOUBLEBUF | SDL_SWSURFACE);
p=pscreen->pixels;
}
curdate = (time_t) (headerframe->seqtimes / 1000);
tdate = localtime(&curdate);
snprintf (titre,21,"%02d/%02d/%04d-%02d:%02d:%02d\0",
tdate->tm_mday, tdate->tm_mon + 1, tdate->tm_year + 1900,
tdate->tm_hour, tdate->tm_min, tdate->tm_sec);
if(jpegsize && videoOk){
jpeg_decode(&picture,buf,&width,&height);
resize (p,picture,owidth,oheight,width,height) ;
SDL_WM_SetCaption (titre, NULL);
SDL_Flip (pscreen);
}
//-----------------------------store jpeg into outputfile-------------------------------------
// frame_size =width *height;
// int fs = get_jpegsize (buf , frame_size);
// if (AVI_write_frame (out_fd, buf,fs) < 0)
// {
// printf ("write error on avi out \n");
// }
// else
// {
// printf("====store jpe ok! \n");
// }
if (AVI_write_frame (out_fd, picture,owidth *oheight *1.5) < 0)
// if (AVI_write_frame (out_fd, buf,width *height *1.5) < 0)
{
printf ("write error on avi out \n");
}
else
{
printf(">>>>>>>>store ok !\n");
}
//-------------------------------------------------------------------------------------------------
printf("bright %05d contrast %05d \r",headerframe->bright,headerframe->contrast);
fflush (stdout);
switch (run){
case 1:
if (SDL_PollEvent (&sdlevent) == 1) {
switch (sdlevent.type) {
case SDL_KEYDOWN:
switch (sdlevent.key.keysym.sym) {
case SDLK_s:
//getPicture(buf,jpegsize);
getJpegPicture(buf,width,height,
VIDEO_PALETTE_JPEG,jpegsize,PICTURE,NULL);
break;
case SDLK_w:
messcallback->updocontrast =2;
break;
case SDLK_x:
messcallback->updocontrast =1;
break;
case SDLK_b:
messcallback->updobright=1;
break;
case SDLK_n:
messcallback->updobright=2;
break;
case SDLK_l:
messcallback->sleepon=1;
break;
case SDLK_c:
messcallback->sleepon=2;
break;
case SDLK_j:
messcallback->updoexposure=1;
break;
case SDLK_d:
messcallback->updosize=1; //workaround change quality index
break;
case SDLK_f:
messcallback->updosize=2; //workaround change quality index
break;
case SDLK_g:
messcallback->fps=1; // decrease time_interval
break;
case SDLK_h:
messcallback->fps=2; // increase time interval
break;
case SDLK_UP:
if(messcallback->y -1 > 0)
messcallback->y--;
keypressed = SDLK_UP ; run = 2;
break;
case SDLK_DOWN:
if(messcallback->y +1 < 256)
messcallback->y++;
keypressed = SDLK_DOWN; run = 2;
break;
case SDLK_RIGHT:
if(messcallback->x -1 > 0)
messcallback->x--;
keypressed = SDLK_RIGHT; run = 2;
break;
case SDLK_LEFT:
if(messcallback->x +1 < 256)
messcallback->x++;
keypressed = SDLK_LEFT; run = 2;
break;
case SDLK_SPACE:
run = 0;
break;
case SDLK_q:
quit =0;
break;
}
break;
case SDL_QUIT:
quit = 0;
break;
}
} //end Poll event
break;
case 0:
if (SDL_WaitEvent (&sdlevent) == 1) {
switch (sdlevent.type) {
case SDL_KEYDOWN:
switch (sdlevent.key.keysym.sym) {
case SDLK_s:
//getPicture(buf,jpegsize);
break;
case SDLK_SPACE:
run = 1;
break;
case SDLK_q:
quit =0;
break;
}
break;
case SDL_QUIT:
quit = 0;
break;
}
} //end wait event
break;
case 2:{
if (SDL_PollEvent (&sdlevent) == 1) {
switch (sdlevent.type) {
case SDL_KEYDOWN:
keypressed = 0;
run = 1;
break;
} //end event type poll 2
}
switch (keypressed){
case SDLK_UP:
if(messcallback->y -1 > 0)
messcallback->y--;
break;
case SDLK_DOWN:
if(messcallback->y +1 < 256)
messcallback->y++;
break;
case SDLK_RIGHT:
if(messcallback->x -1 > 0)
messcallback->x--;
break;
case SDLK_LEFT:
if(messcallback->x +1 < 256)
messcallback->x++;
break;
default:
break;
}
} // end case 2
break;
}
}while(quit);
error:
if(picture){
free(picture);
picture = NULL;
}
close_sock(sock_client);
free(buf);
free(messcallback);
free(headerframe);
SDL_Quit ();
return 0;
}
兄弟,你可以只保存一帧,看看是不是一张图片,如果是jpeg的,打开看看就知道了。