要求:系统Centos7.x
第一步安装Cmake
yum install -y gcc gcc-c++ wget
wget https://cmake.org/files/v3.14/cmake-3.14.0.tar.gz
tar -zxvf cmake-3.14.0.tar.gz
cd cmake-3.14.0
./bootstrap
gmake
gmake install
第二步编译libz
tar -xvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
mkdir build
cd build
touch build.sh
build.sh
cmake -G "Unix Makefiles" \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=/var/SDK/install/zlib \
-D CMAKE_C_FLAGS="-fPIC" \
-D CMAKE_CXX_FLAGS="-fPIC" ..
chmod -R 777 build.sh
./build.sh
make
make install
第三步编译libpng
tar -xvf libpng-1.5.30.tar.xz
cd libpng-1.5.30
touch build.sh
build.sh
export CFLAGS="-I/var/SDK/install/zlib/include -fPIC"
export CPPFLAGS="-I/var/SDK/install/zlib/include -fPIC"
export LDFLAGS="-L/var/SDK/install/zlib/lib"
./configure --prefix=/var/SDK/install/libpng
chmod -R 777 build.sh
./build.sh
make
make install
第四步编译opencv
unzip opencv-4.1.0.zip
cd opencv-4.1.0
mkdir build
cd build
touch build.sh
build.sh
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/var/SDK/install/opencv \
-D BUILD_SHARED_LIBS=OFF \
-D WITH_FFMPEG=ON \
-D ENABLE_CXX11=1 ..
chmod -R 777 build.sh
./build.sh
make
make install
第四步编译zxing
unzip zxing-cpp-master.zip
cd zxing-cpp-master
mkdir build
touch build.sh
build.sh
cmake -G "Unix Makefiles" \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=/var/SDK/install/zxing \
-D CMAKE_C_FLAGS="-fPIC" \
-D CMAKE_CXX_FLAGS="-fPIC" \
-D OpenCV_DIR=/var/SDK/opencv-4.1.0/build ..
chmod -R 777 build.sh
./build.sh
make
make install
第五步编译c++的二维码识别程序
mkdir /var/SDK/OPENCV_ZXING_QR
cd OPENCV_ZXING_QR
touch CMakeLists.txt
touch opencv_core.h
touch opencv_core.cpp
touch zxing_core.h
touch zxing_core.cpp
touch test.cpp
CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
add_library(libittnotify STATIC IMPORTED)
set_target_properties(libittnotify PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv4/3rdparty/libittnotify.a)
add_library(libquirc STATIC IMPORTED)
set_target_properties(libquirc PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv4/3rdparty/libquirc.a)
add_library(liblibprotobuf STATIC IMPORTED)
set_target_properties(liblibprotobuf PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv4/3rdparty/liblibprotobuf.a)
add_library(libIlmImf STATIC IMPORTED)
set_target_properties(libIlmImf PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv4/3rdparty/libIlmImf.a)
add_library(liblibjasper STATIC IMPORTED)
set_target_properties(liblibjasper PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv4/3rdparty/liblibjasper.a)
add_library(liblibwebp STATIC IMPORTED)
set_target_properties(liblibwebp PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv4/3rdparty/liblibwebp.a)
add_library(liblibtiff STATIC IMPORTED)
set_target_properties(liblibtiff PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv4/3rdparty/liblibtiff.a)
add_library(liblibjpeg-turbo STATIC IMPORTED)
set_target_properties(liblibjpeg-turbo PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv4/3rdparty/liblibjpeg-turbo.a)
add_library(libopencv_video STATIC IMPORTED)
set_target_properties(libopencv_video PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/libopencv_video.a)
add_library(libopencv_stitching STATIC IMPORTED)
set_target_properties(libopencv_stitching PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/libopencv_stitching.a)
add_library(libopencv_objdetect STATIC IMPORTED)
set_target_properties(libopencv_objdetect PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/libopencv_objdetect.a)
add_library(libopencv_calib3d STATIC IMPORTED)
set_target_properties(libopencv_calib3d PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/libopencv_calib3d.a)
add_library(libopencv_features2d STATIC IMPORTED)
set_target_properties(libopencv_features2d PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/libopencv_features2d.a)
add_library(libopencv_dnn STATIC IMPORTED)
set_target_properties(libopencv_dnn PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/libopencv_dnn.a)
add_library(libopencv_photo STATIC IMPORTED)
set_target_properties(libopencv_photo PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/libopencv_photo.a)
add_library(libopencv_ml STATIC IMPORTED)
set_target_properties(libopencv_ml PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/libopencv_ml.a)
add_library(libopencv_flann STATIC IMPORTED)
set_target_properties(libopencv_flann PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/libopencv_flann.a)
add_library(libopencv_highgui STATIC IMPORTED)
set_target_properties(libopencv_highgui PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/libopencv_highgui.a)
add_library(libopencv_videoio STATIC IMPORTED)
set_target_properties(libopencv_videoio PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/libopencv_videoio.a)
add_library(libopencv_imgcodecs STATIC IMPORTED)
set_target_properties(libopencv_imgcodecs PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/libopencv_imgcodecs.a)
add_library(libopencv_imgproc STATIC IMPORTED)
set_target_properties(libopencv_imgproc PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/libopencv_imgproc.a)
add_library(libopencv_core STATIC IMPORTED)
set_target_properties(libopencv_core PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/libopencv_core.a)
add_library(libzxing STATIC IMPORTED)
set_target_properties(libzxing PROPERTIES IMPORTED_LOCATION /var/SDK/install/zxing/lib/libzxing.a)
add_library(libz STATIC IMPORTED)
set_target_properties(libz PROPERTIES IMPORTED_LOCATION /var/SDK/install/zlib/lib/libz.a)
add_library(libpng15 STATIC IMPORTED)
set_target_properties(libpng15 PROPERTIES IMPORTED_LOCATION /var/SDK/install/libpng/lib/libpng15.a)
message("include 头文件目录")
include_directories(/var/SDK/install/opencv/include/opencv4)
include_directories(/var/SDK/install/zxing/include)
include_directories(/usr/java/jdk1.8.0_65/include)
include_directories(/usr/java/jdk1.8.0_65/include/linux)
include_directories(${SRC_ROOT_PATH}/)
message("设置源码根路径,即 cpp 目录")
set(SRC_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
message(${SRC_ROOT_PATH})
message("定义要编译的文件列表变量")
file(GLOB CPP_FILES_IN_UTILS "${SRC_ROOT_PATH}/*.cpp")
message("设置要编译的源文件")
set(SOURCE_FILES ${CPP_FILES_IN_UTILS})
add_library(qrcode SHARED ${SOURCE_FILES})
target_link_libraries(
qrcode
libzxing
libopencv_video
libopencv_stitching
libopencv_objdetect
libopencv_calib3d
libopencv_features2d
libopencv_dnn
libopencv_photo
libopencv_ml
libopencv_flann
libopencv_highgui
libopencv_videoio
libopencv_imgcodecs
libopencv_imgproc
libopencv_core
libittnotify
libquirc
liblibprotobuf
libIlmImf
liblibjasper
liblibwebp
liblibtiff
liblibjpeg-turbo
libpng15
libz
)
zxing_core.h
#pragma once
const char* parse(const char* fromPath);
zxing_core.cpp
#ifndef zxing_parse_h
#define zxing_parse_h
#include <iostream>
#include "opencv2/opencv.hpp"
#include "zxing_core.h"
#include <zxing/LuminanceSource.h>
#include <zxing/common/Counted.h>
#include <zxing/Reader.h>
#include <zxing/aztec/AztecReader.h>
#include <zxing/common/GlobalHistogramBinarizer.h>
#include <zxing/DecodeHints.h>
#include <zxing/datamatrix/DataMatrixReader.h>
#include <zxing/MultiFormatReader.h>
#include <zxing/pdf417/PDF417Reader.h>
#include <zxing/qrcode/QRCodeReader.h>
#include "zxing/MatSource.h"
#endif
using namespace cv;
using namespace std;
using namespace zxing;
const char* parse(const char* fromPath) {
cv::Mat matSrc = cv::imread(fromPath, 1);
if (!matSrc.data) {
return nullptr;
}
try {
cv::Mat matGray;
cv::cvtColor(matSrc, matGray, COLOR_BGR2GRAY);
zxing::Ref<zxing::LuminanceSource> source = MatSource::create(matGray);
zxing::Ref<zxing::Reader> reader;
reader.reset(new zxing::qrcode::QRCodeReader);
zxing::Ref<zxing::Binarizer> binarizer(new zxing::GlobalHistogramBinarizer(source));
zxing::Ref<zxing::BinaryBitmap> bitmap(new zxing::BinaryBitmap(binarizer));
zxing::Ref<zxing::Result> result(reader->decode(bitmap, zxing::DecodeHints(zxing::DecodeHints::QR_CODE_HINT)));
std::string str2 = result->getText()->getText();
return str2.c_str();
}
catch (zxing::Exception e) {
return nullptr;
}
return nullptr;
}
opencv_core.h
#pragma once
int opencv_detail(const char* fromPath,const char* toPath);
opencv_core.cpp
#ifndef opencv_core_h
#define opencv_core_h
#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv_core.h"
#endif
using namespace cv;
using namespace std;
int opencv_detail(const char* fromPath,const char* toPath)
{
Mat src = imread(fromPath);
if (src.empty()) {
printf("error");
return -1;
}
Mat gray;
cvtColor(src, gray, COLOR_RGB2GRAY);
Mat binary;
threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
vector<vector<Point>> contours;
vector<Vec4i> hireachy;
findContours(binary.clone(), contours, hireachy, RETR_TREE, CHAIN_APPROX_NONE, Point());
vector<vector<Point>> filter1;
int ic = 0;
int parentIdx = -1;
for (int i = 0; i < contours.size(); i++)
{
if (hireachy[i][2] != -1 && ic == 0)
{
parentIdx = i;
ic++;
}
else if (hireachy[i][2] != -1)
{
ic++;
}
else if (hireachy[i][2] == -1)
{
if (ic == 2) {
RotatedRect rect1 = minAreaRect(contours[parentIdx]);
RotatedRect rect2 = minAreaRect(contours[parentIdx + 1]);
RotatedRect rect3 = minAreaRect(contours[parentIdx + 2]);
if (rect1.size.width / rect1.size.height > 0.85) {
if ((rect1.size.width - rect2.size.width) / 2 + (rect2.size.width - rect3.size.width) / 2 < rect3.size.width) {
filter1.push_back(contours[parentIdx]);
}
}
}
parentIdx = -1;
ic = 0;
}
}
if (filter1.size() < 3) {
//imshow("input_image", src);
imwrite(toPath, src);
// waitKey();
return 0;
}
RotatedRect minRect1 = minAreaRect(Mat(filter1[0]));
RotatedRect minRect2 = minAreaRect(Mat(filter1[1]));
RotatedRect minRect3 = minAreaRect(Mat(filter1[2]));
int minLeft = min(min(minRect1.center.x, minRect2.center.x), minRect3.center.x);
int maxLeft = max(max(minRect1.center.x, minRect2.center.x), minRect3.center.x);
int minTop = min(min(minRect1.center.y, minRect2.center.y), minRect3.center.y);
int maxTop = max(max(minRect1.center.y, minRect2.center.y), minRect3.center.y);
int rectWidth = minRect1.size.width / 2;
Rect rect(minLeft - rectWidth, minTop - rectWidth, maxLeft - minLeft + minRect1.size.width, maxLeft - minLeft + minRect1.size.width);
Mat mat = src(rect);
//namedWindow("input_image", WINDOW_AUTOSIZE);
//imshow("input_image", mat);
imwrite(toPath, mat);
//waitKey();
return 0;
}
test.cpp
#include "jni.h"
#ifndef dllmain_h
#define dllmain_h
#include <string.h>
#include "opencv_core.h"
#include "zxing_core.h"
#endif
#define FROM_PATH_NULL -1001
#define TO_PATH_NULL -1002
#include <iostream>
using namespace std; //这两个支持linux cout
extern "C" {
JNIEXPORT jint JNICALL Java_com_java_wjs_opencv_qr_QRScan_scan(JNIEnv* env, jobject jobj, jstring fromPath, jstring toPath) {
const char* fromPathStr = env->GetStringUTFChars(fromPath, false);
const char* toPathStr = env->GetStringUTFChars(toPath, false);
if (fromPathStr == NULL || strlen(fromPathStr) == 0 || fromPathStr[0] == '\0') {
return FROM_PATH_NULL;
}
if (toPathStr == NULL || strlen(toPathStr) == 0 || toPathStr[0] == '\0') {
return TO_PATH_NULL;
}
opencv_detail(fromPathStr, toPathStr);
env->ReleaseStringUTFChars(fromPath, fromPathStr);
env->ReleaseStringUTFChars(toPath, toPathStr);
return 0;
}
JNIEXPORT jstring JNICALL Java_com_java_wjs_opencv_qr_QRScan_parse(JNIEnv* env, jobject jobj, jstring fromPath) {
const char* fromPathStr = env->GetStringUTFChars(fromPath, false);
if (fromPathStr == NULL || strlen(fromPathStr) == 0 || fromPathStr[0] == '\0') {
return env->NewStringUTF("-1001");
}
const char * result=parse(fromPathStr);
if (result != NULL) {
env->ReleaseStringUTFChars(fromPath, fromPathStr);
return env->NewStringUTF(result);
}
return env->NewStringUTF("-1003");
}
}
cmake .
make
编写 QRScan.java
package com.java.wjs.opencv.qr;
public class QRScan {
static {
System.loadLibrary("qrcode");
}
public native int scan(String fromPath,String toPath);
public native String parse(String fromPath);
}
编写DownLoadeManager.java
package com.java.wjs.opencv.qr;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownLoadeManager {
public void downImage(String url,String path) throws Exception {
URL mUrl=new URL(url);
HttpURLConnection connection= (HttpURLConnection) mUrl.openConnection();
connection.setDoInput(true);
InputStream inputStream=connection.getInputStream();
FileOutputStream outputStream=new FileOutputStream(path);
byte[] bytes=new byte[10240];
int len=-1;
while((len=inputStream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
}
outputStream.flush();
outputStream.close();
inputStream.close();
connection.disconnect();
}
}
编写Main.java
package com.java.wjs.opencv.qr;
public class Main {
private static final String PATH_PHOTO="/home/admin/test.png";
long tempTime = System.currentTimeMillis();
public static final void main(String[] args){
Main main=new Main();
try {
main.parseURL("http://e0.ifengimg.com/04/2019/0617/7ACF2BDDF6A7FC0899614A6E747A1AA8C708EDC3_size794_w800_h5706.jpeg");// 148
//main.parseURL("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1561121375932&di=5cbf93a41301953a1036072874c03095&imgtype=0&src=http%3A%2F%2Fwww.xp85.com%2Fuploadfile%2F2012%2F1120%2F20121120032231951.png");
//main.parseURL("https://csdnimg.cn/pubfooter/images/job-QR.png");
} catch (Exception e) {
e.printStackTrace();
}
}
public void parseURL(String url) throws Exception {
DownLoadeManager manager=new DownLoadeManager();
manager.downImage(url,PATH_PHOTO);
printTime("下载二维码所用时间");
QRScan qrScan = new QRScan();
qrScan.scan(PATH_PHOTO,PATH_PHOTO);
printTime("寻找二维码所用时间");
String strInfo=qrScan.parse(PATH_PHOTO);
System.out.println("解析结果:"+strInfo);
printTime("解析二维码所用时间");
}
public void printTime(String time){
System.out.println(time+ (System.currentTimeMillis() - tempTime));
tempTime=System.currentTimeMillis();
}
}
附加openCv是动态库的CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
add_library(opencv_calib3d SHARED IMPORTED)
set_target_properties(opencv_calib3d PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv_calib3d.so.4.1.0)
add_library(opencv_core SHARED IMPORTED)
set_target_properties(opencv_core PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv_core.so.4.1.0)
add_library(opencv_dnn SHARED IMPORTED)
set_target_properties(opencv_dnn PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv_dnn.so.4.1.0)
add_library(opencv_features2d SHARED IMPORTED)
set_target_properties(opencv_features2d PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv_features2d.so.4.1.0)
add_library(opencv_flann SHARED IMPORTED)
set_target_properties(opencv_flann PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv_flann.so.4.1.0)
add_library(opencv_highgui SHARED IMPORTED)
set_target_properties(opencv_highgui PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv_highgui.so.4.1.0)
add_library(opencv_imgcodecs SHARED IMPORTED)
set_target_properties(opencv_imgcodecs PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv_imgcodecs.so.4.1.0)
add_library(opencv_imgproc SHARED IMPORTED)
set_target_properties(opencv_imgproc PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv_imgproc.so.4.1.0)
add_library(opencv_ml SHARED IMPORTED)
set_target_properties(opencv_ml PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv_ml.so.4.1.0)
add_library(opencv_objdetect SHARED IMPORTED)
set_target_properties(opencv_objdetect PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv_objdetect.so.4.1.0)
add_library(opencv_photo SHARED IMPORTED)
set_target_properties(opencv_photo PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv_photo.so.4.1.0)
add_library(opencv_stitching SHARED IMPORTED)
set_target_properties(opencv_stitching PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv_stitching.so.4.1.0)
add_library(opencv_videoio SHARED IMPORTED)
set_target_properties(opencv_videoio PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv_videoio.so.4.1.0)
add_library(opencv_video SHARED IMPORTED)
set_target_properties(opencv_video PROPERTIES IMPORTED_LOCATION /var/SDK/install/opencv/lib64/opencv_video.so.4.1.0)
add_library(libzxing STATIC IMPORTED)
set_target_properties(libzxing PROPERTIES IMPORTED_LOCATION /var/SDK/install/zxing/lib/libzxing.a)
message("include 头文件目录")
include_directories(/var/SDK/install/opencv/include/opencv4)
include_directories(/var/SDK/install/zxing/include)
include_directories(/usr/java/jdk1.8.0_65/include)
include_directories(/usr/java/jdk1.8.0_65/include/linux)
include_directories(${SRC_ROOT_PATH}/)
message("设置源码根路径,即 cpp 目录")
set(SRC_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
message(${SRC_ROOT_PATH})
message("定义要编译的文件列表变量")
file(GLOB CPP_FILES_IN_UTILS "${SRC_ROOT_PATH}/*.cpp")
message("设置要编译的源文件")
set(SOURCE_FILES ${CPP_FILES_IN_UTILS})
add_library(qrcode SHARED ${SOURCE_FILES})
target_link_libraries(
qrcode
opencv_calib3d
opencv_core
opencv_dnn
opencv_features2d
opencv_flann
opencv_highgui
opencv_imgcodecs
opencv_imgproc
opencv_ml
opencv_objdetect
opencv_photo
opencv_stitching
opencv_videoio
opencv_video
libzxing
)