-1

所以我了解套接字编程的基础知识,但是当我转到 localhost:8888 时得到的是不可读的字符串,而不是视频或图像帧。我想知道是否有人对如何在我的端口上显示图像而不是字符串有任何指示。我的代码如下:

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void* streamServer(void* arg);
void  quit(char* msg, int retval);

int main(int argc, char** argv)
{
    pthread_t   thread_s;
    int         key;

    if (argc == 2) {
        capture = cvCaptureFromFile(argv[1]);
    } else {
        capture = cvCaptureFromCAM(0);
    }

    if (!capture) {
        quit("cvCapture failed", 1);
    }

    img0 = cvQueryFrame(capture);
    img1 = cvCreateImage(cvGetSize(img0), IPL_DEPTH_8U, 1);

    cvZero(img1);
    cvNamedWindow("stream_server", CV_WINDOW_AUTOSIZE);

    /* print the width and height of the frame, needed by the client */
    fprintf(stdout, "width:  %d\nheight: %d\n\n", img0->width, img0->height);
    fprintf(stdout, "Press 'q' to quit.\n\n");

    /* run the streaming server as a separate thread */
    if (pthread_create(&thread_s, NULL, streamServer, NULL)) {
        quit("pthread_create failed.", 1);
    }

    while(key != 'q') {
        /* get a frame from camera */
        img0 = cvQueryFrame(capture);
        if (!img0) break;

        img0->origin = 0;
        cvFlip(img0, img0, 1); //-1);

        /**
         * convert to grayscale
         * note that the grayscaled image is the image to be sent to the client
         * so we enclose it with pthread_mutex_lock to make it thread safe
         */
        pthread_mutex_lock(&mutex);
        cvCvtColor(img0, img1, CV_BGR2GRAY);
        is_data_ready = 1;
        pthread_mutex_unlock(&mutex);

        /* also display the video here on server */
        cvShowImage("stream_server", img0);
        key = cvWaitKey(30);
    }

    /* user has pressed 'q', terminate the streaming server */
    if (pthread_cancel(thread_s)) {
        quit("pthread_cancel failed.", 1);
    }

    /* free memory */
    cvDestroyWindow("stream_server");
    quit(NULL, 0);
}

/**
 * This is the streaming server, run as a separate thread
 * This function waits for a client to connect, and send the grayscaled images
 */
void* streamServer(void* arg)
{
    struct sockaddr_in server;

    /* make this thread cancellable using pthread_cancel() */
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

    /* open socket */
    if ((serversock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
        quit("socket() failed", 1);
    }

    /* setup server's IP and port */
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(PORT);
    server.sin_addr.s_addr = INADDR_ANY;

    /* bind the socket */
    if (bind(serversock, (const void*)&server, sizeof(server)) == -1) {
        quit("bind() failed", 1);
    }

    /* wait for connection */
    if (listen(serversock, 10) == -1) {
        quit("listen() failed.", 1);
    }

    /* accept a client */
    if ((clientsock = accept(serversock, NULL, NULL)) == -1) {
        quit("accept() failed", 1);
    }

    /* the size of the data to be sent */
    int imgsize = img1->imageSize;
    int bytes, i;

    /* start sending images */
    while(1)
    {
        /* send the grayscaled frame, thread safe */
        pthread_mutex_lock(&mutex);
        if (is_data_ready) {
            bytes = send(clientsock, img1->imageData, imgsize, 0);
            is_data_ready = 0;
        }
        pthread_mutex_unlock(&mutex);

        /* if something went wrong, restart the connection */
        if (bytes != imgsize) {
            fprintf(stderr, "Connection closed.\n");
            close(clientsock);

            if ((clientsock = accept(serversock, NULL, NULL)) == -1) {
                quit("accept() failed", 1);
            }
        }

整个代码: http: //pastebin.com/jnuZhx8C

4

2 回答 2

1

除非 img1->imageData 是浏览器应该识别的格式(png、jpg、gif),否则您应该期待无法识别的文本。

于 2012-10-01T22:21:31.967 回答
1

在我们进入技术细节之前,问自己一个问题:什么是standard web image format

在您看来,会是 PNG 吗?JPG?TIFF?骨肉瘤?动图?所有这些?

像这样的图像格式存储的不仅仅是像素。如果你看一下PNG 的规范,你会发现它除了像素之外还存储了很多关于图像的信息。

问题是,当您通过网络仅发送像素时,就像您现在正在做的那样,无论是谁在连接的另一端接收数据,都无法对它做任何有用的事情,因为它甚至不知道尺寸是多少的图像。

我们需要澄清的另一个误解是:没有display images on my port rather than strings. 什么是端口?

跨网络传输数据是一回事。在连接的另一端对数据做一些有用的事情(比如在屏幕上显示)是一项完全不同的任务。

如果您需要一个关于如何完成这些任务的实际示例,我建议您看看这个很棒的教程:通过网络流式传输 OpenCV 视频。我刚刚注意到您问题中的代码来自那里,因此请特别注意 section 4. Implementation of the Client-side

于 2012-10-02T15:07:33.793 回答