交叉编译工具链
直接用正点原子提供的,或者通过sdk编译得到
脚本安装到http://opt/atk-dlrk356x-toolchain/
工具链里有opencv库
目录
工具链的cmake写法
#@author Deng Zhimao
#@email dengzhimao@alientek.com
#http://www.openedv.com/forum.php
cmake_minimum_required(VERSION 3.8)
message(STATUS "cmake version ${CMAKE_VERSION}")
set(TOOLCHAIN_DIR /opt/atk-dlrk356x-toolchain)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_DIR}/bin/aarch64-buildroot-linux-gnu-g++)
set(CMAKE_C_COMPILER ${TOOLCHAIN_DIR}/bin/aarch64-buildroot-linux-gnu-gcc)
set(SYSROOT ${TOOLCHAIN_DIR}/aarch64-buildroot-linux-gnu/sysroot/usr/include)
set(CMAKE_SYSROOT ${TOOLCHAIN_DIR}/aarch64-buildroot-linux-gnu/sysroot)
set(CMAKE_CXX_STANDARD 11)
set(OPENCV_LIBS opencv_core opencv_videoio opencv_video opencv_highgui
opencv_imgcodecs opencv_imgproc)
include_directories(${SYSROOT})
include_directories(${SYSROOT}/opencv4)
project(opencv)
#生成 opencv_test 执行程序
add_executable(opencv_test opencv_test.cpp)
target_link_libraries(opencv_test ${OPENCV_LIBS})
SYSROOT
和 CMAKE_SYSROOT
都是 CMake 中用于指定交叉编译工具链的 sysroot 目录的变量。
sysroot 是一个目录,包含了交叉编译工具链所需要的系统文件和库。它模拟了目标设备上的文件系统结构,包括标准库、头文件和其他系统资源。在交叉编译过程中,编译器会使用 sysroot 目录下的文件和库来生成针对目标平台的可执行文件。
SYSROOT
变量指定了交叉编译工具链的 sysroot 目录中的头文件所在路径。在本例中,include_directories(${SYSROOT})
将该路径添加到 CMake 的头文件搜索路径中。
CMAKE_SYSROOT
变量指定了整个交叉编译工具链的 sysroot 目录路径。在本例中,set(CMAKE_SYSROOT ${TOOLCHAIN_DIR}/aarch64-buildroot-linux-gnu/sysroot)
设置了 CMake 的 sysroot 为交叉编译工具链的 sysroot 目录。
通过设置这两个变量,CMake 在构建项目时能够正确地找到所需的系统文件和库,以便进行交叉编译。
压缩图片案例
jpg读入到char*,然后压缩照片的案例(可以直接读取jpg,不用经过fopen,只是为了测试char*转图片)
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
if (argc != 3)
{
cout << "Usage: " << argv[0] << " <input_image_path> <output_image_path>" << endl;
return -1;
}
// 从文件中读取图像数据到 char*
FILE* file = fopen(argv[1], "rb");
if (file == nullptr)
{
cout << "Could not open the image file: " << argv[1] << endl;
return -1;
}
// 获取文件大小
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
rewind(file);
// 分配内存来存储图像数据
char* img_data = new char[file_size];
// 将文件数据读入到内存中
size_t read_result = fread(img_data, 1, file_size, file);
fclose(file);
if (read_result != file_size)
{
cout << "Error reading the image file: " << argv[1] << endl;
delete[] img_data;
return -1;
}
// 将内存中的图像数据转换为 Mat 对象
Mat img = imdecode(Mat(1, file_size, CV_8UC1, img_data), IMREAD_COLOR);
delete[] img_data;
if (img.empty())
{
cout << "Could not decode the image: " << argv[1] << endl;
return -1;
}
// 压缩图像
vector<int> compression_params;
compression_params.push_back(IMWRITE_JPEG_QUALITY);
compression_params.push_back(50);
// 写入输出图像
imwrite(argv[2], img, compression_params);
return 0;
}