Skip to content

Commit

Permalink
已经可以解析了
Browse files Browse the repository at this point in the history
  • Loading branch information
devilsen committed Aug 17, 2019
1 parent 1d579cf commit 92cc267
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 152 deletions.
4 changes: 2 additions & 2 deletions czxing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
externalNativeBuild {
cmake {
// cppFlags "-std=c++11 -frtti -fexceptions -pthread"
cppFlags "-std=c++11 -pthread"
// cppFlags "-std=c++11 -pthread"
abiFilters "armeabi-v7a","arm64-v8a"
}
}
Expand All @@ -27,7 +27,7 @@ android {
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
// version "3.10.2"
}
}
compileOptions {
Expand Down
129 changes: 60 additions & 69 deletions czxing/src/main/cpp/ImageScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ ImageScheduler::~ImageScheduler() {
DELETE(reader);
DELETE(javaCallHelper);

DELETE(grayMat);
DELETE(thresholdMat);
DELETE(adaptiveMat);
cvDet(&pretreatmentMat);

DELETE(grayResult);
DELETE(thresholdResult);
DELETE(adaptiveResult);
}

Mat
void
ImageScheduler::pretreatment(jbyte *bytes, int left, int top, int cropWidth, int cropHeight,
int rowWidth,
int rowHeight) {
Expand All @@ -45,25 +43,23 @@ ImageScheduler::pretreatment(jbyte *bytes, int left, int top, int cropWidth, int
Mat gray;
cvtColor(src, gray, COLOR_RGBA2GRAY);

// 降低图片亮度
Mat lightMat;
gray.convertTo(lightMat, -1, 1.0, -60);

return lightMat;
pretreatmentMat = gray;
}

Result *
ImageScheduler::process(JNIEnv *env, jbyte *bytes, int left, int top, int cropWidth, int cropHeight,
ImageScheduler::process(jbyte *bytes, int left, int top, int cropWidth, int cropHeight,
int rowWidth,
int rowHeight) {

Mat gray = pretreatment(bytes, left, top, cropWidth, cropHeight, rowWidth, rowHeight);
pretreatment(bytes, left, top, cropWidth, cropHeight, rowWidth, rowHeight);

processGray(gray);
// processThreshold(gray);
// processAdaptive(gray);
// decodeGrayPixels();
processGray();
processThreshold();
processAdaptive();

return analyzeResult();
// return analyzeResult();
return nullptr;
}

void *threadProcessGray(void *args) {
Expand All @@ -84,88 +80,100 @@ void *threadProcessAdaptive(void *args) {
return nullptr;
}

void ImageScheduler::processGray(Mat gray) {
grayMat = &gray;
void ImageScheduler::processGray() {
pthread_create(&grayThread, nullptr, threadProcessGray, this);
pthread_join(grayThread, nullptr);
}

void ImageScheduler::processThreshold(Mat gray) {
void ImageScheduler::processThreshold() {
pthread_create(&thresholdThread, nullptr, threadProcessThreshold, this);
}

void ImageScheduler::processAdaptive() {
pthread_create(&adaptiveThread, nullptr, threadProcessAdaptive, this);
}

void ImageScheduler::decodeGrayPixels() {
Result result = decodePixels(pretreatmentMat);
javaCallHelper->onResult(result);
}

void ImageScheduler::decodeThresholdPixels() {
Mat mat;
rotate(gray, mat, ROTATE_90_CLOCKWISE);
rotate(pretreatmentMat, mat, ROTATE_90_CLOCKWISE);
threshold(mat, mat, 0, 255, CV_THRESH_OTSU);

thresholdMat = &mat;
pthread_create(&thresholdThread, nullptr, threadProcessThreshold, &mat);
pthread_join(thresholdThread, nullptr);
Result result = decodePixels(mat);
javaCallHelper->onResult(result);
}

void ImageScheduler::processAdaptive(Mat gray) {
void ImageScheduler::decodeAdaptivePixels() {
Mat mat;
rotate(gray, mat, ROTATE_180);
adaptiveThreshold(mat, mat, 255, ADAPTIVE_THRESH_MEAN_C,
THRESH_BINARY, 55, 3);
rotate(pretreatmentMat, mat, ROTATE_180);

adaptiveMat = &mat;
pthread_create(&adaptiveThread, nullptr, threadProcessAdaptive, &mat);
pthread_join(adaptiveThread, nullptr);
}
// 降低图片亮度
Mat lightMat;
mat.convertTo(lightMat, -1, 1.0, -60);

void ImageScheduler::getPixelsFromMat(Mat mat, int width, int height, unsigned char *pixels) {
int index = 0;
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
pixels[index++] = mat.at<unsigned char>(i, j);
}
}
adaptiveThreshold(lightMat, lightMat, 255, ADAPTIVE_THRESH_MEAN_C,
THRESH_BINARY, 55, 3);

Result result = decodePixels(lightMat);
javaCallHelper->onResult(result);
}

void ImageScheduler::decodePixels(Mat *mat, Result *result) {
int width = mat->cols;
int height = mat->rows;
Result ImageScheduler::decodePixels(Mat mat) {
try {
int width = mat.cols;
int height = mat.rows;

auto *pixels = static_cast<unsigned char *>(malloc(
width * height * sizeof(unsigned char)));
auto *pixels = new unsigned char[height * width];

getPixelsFromMat(*mat, width, height, pixels);
int index = 0;
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
pixels[index++] = mat.at<unsigned char>(i, j);
}
}

// Mat resultMat(height, width, CV_8UC1, pixels);
// imwrite("/storage/emulated/0/scan/result.jpg", resultMat);

try {
auto binImage = BinaryBitmapFromBytesC1(pixels, 0, 0, width, height);
free(pixels);
*result = reader->read(*binImage);
Result result = reader->read(*binImage);

delete pixels;

// javaCallHelper->onResult(result);
return result;
} catch (const std::exception &e) {
ThrowJavaException(env, e.what());
}
catch (...) {
ThrowJavaException(env, "Unknown exception");
}

return Result(DecodeStatus::NotFound);
}

Result *ImageScheduler::analyzeResult() {
Result *result = nullptr;

if (grayResult) {
if (grayResult != nullptr) {
if (grayResult->isValid()) {
return grayResult;
} else if (grayResult->isBlurry()) {
result = grayResult;
}
}

if (thresholdResult) {
if (thresholdResult != nullptr) {
if (thresholdResult->isValid()) {
return thresholdResult;
} else if (thresholdResult->isBlurry() && thresholdResult->resultPoints().size() > 2) {
result = thresholdResult;
}
}

if (adaptiveResult) {
if (adaptiveResult != nullptr) {
if (adaptiveResult->isValid()) {
return adaptiveResult;
} else if (adaptiveResult->isBlurry() && adaptiveResult->resultPoints().size() > 2) {
Expand All @@ -174,21 +182,4 @@ Result *ImageScheduler::analyzeResult() {
}

return result;
}

void ImageScheduler::decodeGrayPixels() {
javaCallHelper->onTest();

decodePixels(grayMat, grayResult);
}

void ImageScheduler::decodeThresholdPixels() {
decodePixels(thresholdMat, thresholdResult);

}

void ImageScheduler::decodeAdaptivePixels() {
decodePixels(adaptiveMat, adaptiveResult);

}

}
22 changes: 9 additions & 13 deletions czxing/src/main/cpp/ImageScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ImageScheduler {
~ImageScheduler();

Result *
process(JNIEnv *env, jbyte *bytes, int left, int top, int width, int height, int rowWidth,
process(jbyte *bytes, int left, int top, int width, int height, int rowWidth,
int rowHeight);

void decodeGrayPixels();
Expand All @@ -36,34 +36,30 @@ class ImageScheduler {
MultiFormatReader *reader;
JavaCallHelper *javaCallHelper;

pthread_t pretreatmentThread;
pthread_t grayThread;
pthread_t thresholdThread;
pthread_t adaptiveThread;

Mat *grayMat;
Mat *thresholdMat;
Mat *adaptiveMat;
Mat pretreatmentMat;

Result *grayResult;
Result *thresholdResult;
Result *adaptiveResult;

Mat pretreatment(jbyte *bytes, int left, int top, int width, int height, int rowWidth,
int rowHeight);
void pretreatment(jbyte *bytes, int left, int top, int width, int height, int rowWidth,
int rowHeight);

void processGray(Mat gray);
void processGray();

void processThreshold(Mat gray);
void processThreshold();

void processAdaptive(Mat gray);
void processAdaptive();

void decodePixels(Mat *mat, Result *result);
Result decodePixels(Mat mat);

Result *analyzeResult();

void getPixelsFromMat(Mat mat, int width, int height, unsigned char *pixels);

};


#endif //CZXING_IMAGESCHEDULER_H
21 changes: 18 additions & 3 deletions czxing/src/main/cpp/JNIUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ std::shared_ptr<ZXing::BinaryBitmap>
BinaryBitmapFromBytesC1(void *pixels, int left, int top, int width, int height) {
using namespace ZXing;

// LOGE("cropLeft %d , cropTop %d cropWidth %d cropHeight %d", cropLeft, cropTop, cropWidth,
// cropHeight);
// LOGE("cropLeft %d , cropTop %d cropWidth %d cropHeight %d", left, top, width,
// height);

std::shared_ptr<GenericLuminanceSource> luminance = std::make_shared<GenericLuminanceSource>(
left, top, width, height,
pixels, width);
pixels, width * sizeof(unsigned char));

return std::make_shared<HybridBinarizer>(luminance);
}
Expand Down Expand Up @@ -146,6 +146,21 @@ std::wstring StringToWString(const std::string &src) {
return desc;
}

std::string UnicodeToANSI(const std::wstring &wstr) {
std::string ret;
std::mbstate_t state = {};
const wchar_t *src = wstr.data();
size_t len = std::wcsrtombs(nullptr, &src, 0, &state);
if (static_cast<size_t>(-1) != len) {
std::unique_ptr<char[]> buff(new char[len + 1]);
len = std::wcsrtombs(buff.get(), &src, len, &state);
if (static_cast<size_t>(-1) != len) {
ret.assign(buff.get(), len);
}
}
return ret;
}

std::wstring ANSIToUnicode(const std::string &str) {
std::wstring ret;
std::mbstate_t state = {};
Expand Down
1 change: 1 addition & 0 deletions czxing/src/main/cpp/JNIUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ std::shared_ptr<ZXing::BinaryBitmap> BinaryBitmapFromBytesC4(JNIEnv* env, void *
std::shared_ptr<ZXing::BinaryBitmap> BinaryBitmapFromBytesC1(void *grayScale, int cropLeft, int cropTop, int cropWidth, int cropHeight);
bool AnalysisBrightness(JNIEnv* env,const jbyte *bytes, int width, int height);
std::wstring StringToWString(const std::string &src);
std::string UnicodeToANSI(const std::wstring & wstr);
std::wstring ANSIToUnicode(const std::string &src);
void ThrowJavaException(JNIEnv* env, const char* message);
jstring ToJavaString(JNIEnv* env, const std::wstring& str);
Expand Down
Loading

0 comments on commit 92cc267

Please sign in to comment.