OpenCV wasm编译

Use Ubuntu 20.04

https://docs.opencv.org/4.x/d4/da1/tutorial_js_setup.html

apt-get install cmake

cd emsdk
./emsdk update
#./emsdk install 1.39.4
./emsdk install latest
#./emsdk activate 1.39.4
./emsdk activate latest
source ./emsdk_env.sh

vi ./platforms/js/build_js.py
-DWITH_JPEG=ON
-DWITH_PNG=ON
-DBUILD_opencv_imgcodecs=ON
-DCMAKE_INSTALL_PREFIX=/home/li/opencv-4.5.5/build_wasm/install

cd opencv
emcmake python ./platforms/js/build_js.py build_wasm --build_wasm --build_loader --build_test
cd ./build_wasm/
make install

npx http-server build_wasm/bin
firefox http://localhost:8081/tests.html
192.168.146.128:8081

em++ opencv_version.cpp -std=c++14 /home/li/opencv-4.5.5/build_wasm/install/lib/libopencv_core.a /home/li/opencv-4.5.5/build_wasm/install/lib/libopencv_imgproc.a /home/li/opencv-4.5.5/build_wasm/install/lib/libopencv_imgcodecs.a /home/li/opencv-4.5.5/build_wasm/install/lib/opencv4/3rdparty/liblibjpeg-turbo.a /home/li/opencv-4.5.5/build_wasm/install/lib/opencv4/3rdparty/liblibpng.a /home/li/opencv-4.5.5/build_wasm/install/lib/opencv4/3rdparty/libzlib.a /home/li/opencv-4.5.5/build_wasm/install/lib/opencv4/3rdparty/liblibopenjp2.a -I/home/li/opencv-4.5.5/build_wasm/install/include/opencv4 -Os -s WASM=1 -o index.html -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' -s ALLOW_MEMORY_GROWTH=1 -s TOTAL_MEMORY=16777216 -s ASSERTIONS=1 --preload-file ./img.jpg --use-preload-plugins -s FORCE_FILESYSTEM
opencv_version.cpp

#include 
#include 
#include 
#include 
#include 

static const std::string keys = "{ b build | | print complete build info }"
                                "{ h help  | | print this help           }";


using namespace std;
using namespace cv;

void SetFileData(uchar* buff, const int buffLength)
{
	Mat img_decode;
	std::vector data(buffLength);
	for (int i = 0; i < buffLength; i++) {
		data[i] = buff[i];
	}
	img_decode = imdecode(data, cv::IMREAD_COLOR);

	std::cout << "Image size:" << img_decode.cols << "x" << img_decode.rows << std::endl;
}

int main(int argc, const char* argv[])
{
    cv::CommandLineParser parser(argc, argv, keys);
    parser.about("This sample outputs OpenCV version and build configuration.");
    if (parser.has("help"))
    {
        parser.printMessage();
    }
    else if (!parser.check())
    {
        parser.printErrors();
    }
    else if (parser.has("build"))
    {
        std::cout << cv::getBuildInformation() << std::endl;
    }
    else
    {
        std::cout << "Welcome to OpenCV " << CV_VERSION << std::endl;
    }

    std::string p = "./img.jpg";
	FILE* pFile;
	long lSize;
	char* buffer;
	size_t result;
	pFile = fopen(p.c_str(), "rb");
	if (pFile == NULL) { fputs("File error", stderr); exit(1); }
	// obtain file size:
	fseek(pFile, 0, SEEK_END);
	lSize = ftell(pFile);
	rewind(pFile);
	
	// allocate memory to contain the whole file:
	buffer = (char*)malloc(sizeof(char) * lSize);
	if (buffer == NULL) { fputs("Memory error", stderr); exit(2); }

	// copy the file into the buffer:
	result = fread(buffer, 1, lSize, pFile);
	if (result != lSize) { fputs("Reading error", stderr); exit(3); }

	SetFileData((uchar*)buffer, lSize);

	// terminate
	fclose(pFile);
	free(buffer);
	
    return 0;
}



    
    
    opencv
    


请选择

WASM下载解析中