源码如下,实现把一个JPG保存成灰度图格式的BMP
1 //jpg2bmp.cpp 2 #include <stdio.h> 3 #include <inttypes.h> 4 #include <opencv2/opencv.hpp> 5 #include <opencv2/core.hpp> 6 7 #ifndef P 8 #define P(format, ...) do 9 { 10 printf("%s %s %d " format "\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__); 11 fflush(stdout); 12 } 13 while (0); 14 #endif 15 16 int main(int argc, char* argv[]){ 17 if (argc<3){ 18 printf("usage:%s <from> <to>\n", argv[0]); 19 return 0; 20 } 21 cv::Mat image = cv::imread(argv[1], cv::IMREAD_UNCHANGED); 22 cv::UMat u_img = image.getUMat(cv::ACCESS_READ); 23 if (image.empty()){ 24 P("load pic %s fail", argv[1]); 25 return -1; 26 } 27 cv::UMat bmp; 28 cv::cvtColor(u_img, bmp, cv::COLOR_BGR2GRAY); 29 cv::imwrite(argv[2], bmp); 30 P("ok"); 31 return 1; 32 }
编译命令行如下:
1 /Users/ahfu/code/android/android-ndk-r14b/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-g++ 2 -o jpg2bmp_1.o -c jpg2bmp_1.cpp -pie -fPIC 3 -I"/Users/ahfu/code/android/OpenCV-android-sdk/sdk/native/jni/include" 4 --sysroot=/Users/ahfu/code/android/android-ndk-r14b/platforms/android-22/arch-arm/ 5 -I"/Users/ahfu/code/android/android-ndk-r14b/sources/cxx-stl/gnu-libstdc++/4.9/include" 6 -I"/Users/ahfu/code/android/android-ndk-r14b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include/"
链接命令行如下:
1 /Users/ahfu/code/android/android-ndk-r14b/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-g++ 2 -o jpg2bmp_1 jpg2bmp_1.o -pie -fPIC 3 --sysroot=/Users/ahfu/code/android/android-ndk-r14b/platforms/android-22/arch-arm/ 4 -L"/Users/ahfu/code/android/OpenCV-android-sdk/sdk/native/staticlibs/armeabi-v7a" -lopencv_core -lopencv_imgcodecs -lopencv_imgproc -lm -lz -landroid -llog 5 -L"/Users/ahfu/code/android/OpenCV-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/" -ltegra_hal -lcpufeatures -ltbb -llibpng -llibjpeg-turbo -llibtiff -llibjasper -llibwebp -lIlmImf -lopencv_core -lopencv_imgcodecs -lopencv_imgproc -ltegra_hal 6 -L"/Users/ahfu/code/android/android-ndk-r14b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a" -lgnustl_static -lsupc++ 7 -L"/Users/ahfu/code/android/android-ndk-r14b/platforms/android-22/arch-arm/usr/lib" -lstdc++
然后使用adb上传
adb root #手机必要要root
adb push jpg2bmp 1.jpg /data/data/temp
adb shell
cd /data/data/temp
chmod +x jpg2bmp
./jpg2bmp 1.jpg 2.bmp
#执行成功
ndk版本是:android-ndk-r14b
opencv android sdk版本是:opencv-3.4.6-android-sdk.zip
原文地址:https://www.cnblogs.com/ahfuzhang/p/10948967.html
时间: 2024-10-09 18:34:25