Image shows up corrupted on localhost

不问归期 提交于 2019-12-24 07:41:54

问题


I'm horribly new to c++ and programming in general. I'm simply trying to read a picture using opencv and display it on web server using boost asio. This is an initial step before I do this for all frames from a video. The following is my code -

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <thread>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>

using boost::asio::ip::tcp;
using namespace std;
using namespace cv;

int main(){
    try{
        boost::asio::io_service io_service;
        tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1112));

        for (;;){
            tcp::socket socket(io_service);
            acceptor.accept(socket);
            boost::system::error_code ignored_error;

            cv::Mat frame = cv::imread("x.jpg");
            vector<uchar> buff;
            vector<int> param = vector<int>(2);
            param[0]=cv::IMWRITE_JPEG_QUALITY;
            param[1]=95;
            imencode(".jpg",frame,buff,param);

            const char mess[] = "axaxaxaxasaaaaaaaaaaxax";

            std::string content(buff.begin(), buff.end());

            boost::asio::write(socket, boost::asio::buffer(content), boost::asio::transfer_all(), ignored_error);
            // boost::asio::write(socket, boost::asio::buffer(mess), boost::asio::transfer_all(), ignored_error);
        }
    }
    catch(std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
    return 0;
}

now sending the message works just fine but when i try to send the image via content or buff, it shows up as gibberish. I feel like it's because I'm not sending any information about the picture prior to sending the picture. But I can't figure out how to do that. Or maybe I'm entirely wrong. Any help/advice would be appreciated. Cheers!


回答1:


I've simplified the code a little:

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <opencv2/opencv.hpp>

using boost::asio::ip::tcp;

int main(){
    try{
        boost::asio::io_service io_service;
        tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1112));

        for (;;){
            tcp::socket socket(io_service);
            acceptor.accept(socket);

            cv::Mat frame = cv::imread("x.jpg");
            std::vector<uchar> buff;
            imencode(".jpg", frame, buff, std::vector<int> { cv::IMWRITE_JPEG_QUALITY, 95 });

            boost::system::error_code err;
            auto bytes_transferred = boost::asio::write(socket, boost::asio::buffer(buff), boost::asio::transfer_all(), err);

            std::cout << "Written: " << bytes_transferred << " (" << err.message() << ")\n";
        }
    }
    catch(std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
}

(specifically, don't use using namespace unneccessarily, not copying into a std::string unneccessarily, and not ignoring the error code unnecessarily)

Compiled it with

g++ test.cpp -L/usr/local/lib -pedantic -Wall -Wextra -pthread -lope^Cv_{core,imgproc,imgcodecs} -lboost_{system,thread} -o test.exe

Copied a sampe jpeg as x.jpg, running it in a terminal:

./test.exe

Then using netcat to read the result:

netcat localhost 1112 > verify.jpg

The server process will print the same message each time:

Written: 6130 (Success)

(6130 bytes happens to be the 95% re-encoded size of the test image I chose). The resulting image (verify.jpg) looks fine in my image viewer.

Conclusion

I think the code is probably fine (but check with the improvements above) and you might have been testing the result incorrectly.



来源:https://stackoverflow.com/questions/58337527/image-shows-up-corrupted-on-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!