Skip to content

Commit bcda688

Browse files
committed
修复某些图片无法识别的问题
1 parent 8f9b554 commit bcda688

4 files changed

Lines changed: 13 additions & 9 deletions

File tree

czxing/src/main/cpp/JNIUtils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ BitmapToMat(JNIEnv *env, jobject bitmap, cv::Mat &mat) {
126126
} else {
127127
// info.format == ANDROID_BITMAP_FORMAT_RGB_565
128128
LOGE("nBitmapToMat: RGB_565 -> CV_8UC4");
129-
cv::Mat tmp(bmInfo.height, bmInfo.width, CV_8UC2, pixels);
129+
cv::Mat tmp(bmInfo.height, bmInfo.width, CV_8UC4, pixels);
130+
tmp.copyTo(dst);
130131
}
131132
} else {
132133
throw std::runtime_error("Failed to read bitmap's data");

czxing/src/main/java/me/devilsen/czxing/code/BarcodeReader.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package me.devilsen.czxing.code;
22

33
import android.graphics.Bitmap;
4+
import android.graphics.BitmapFactory;
45
import android.util.Log;
56

67
import me.devilsen.czxing.util.BarCodeUtil;
8+
import me.devilsen.czxing.util.SaveImageUtil;
79

810
public class BarcodeReader {
911

@@ -47,10 +49,11 @@ public CodeResult read(Bitmap bitmap) {
4749
BarCodeUtil.e("bitmap is null");
4850
return null;
4951
}
50-
int imgWidth = bitmap.getWidth();
51-
int imgHeight = bitmap.getHeight();
52+
Bitmap grayBitmap = SaveImageUtil.getBinaryzationBitmap(bitmap);
53+
int imgWidth = grayBitmap.getWidth();
54+
int imgHeight = grayBitmap.getHeight();
5255
Object[] result = new Object[2];
53-
int resultFormat = NativeSdk.getInstance().readBarcode(_nativePtr, bitmap, 0, 0, imgWidth, imgHeight, result);
56+
int resultFormat = NativeSdk.getInstance().readBarcode(_nativePtr, grayBitmap, 0, 0, imgWidth, imgHeight, result);
5457
if (resultFormat >= 0) {
5558
CodeResult decodeResult = new CodeResult(BarcodeFormat.values()[resultFormat], (String) result[0]);
5659
if (result[1] != null) {

czxing/src/main/java/me/devilsen/czxing/util/SaveImageUtil.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ private static int[] rotate(int[] data, int width, int height, int rowWidth) {
106106
* @return 二值化处理后的图片
107107
*/
108108
public static Bitmap getBinaryzationBitmap(Bitmap bm) {
109-
Bitmap bitmap = null;
109+
Bitmap bitmap;
110110
// 获取图片的宽和高
111111
int width = bm.getWidth();
112112
int height = bm.getHeight();
113113
// 创建二值化图像
114114
bitmap = bm.copy(Bitmap.Config.ARGB_8888, true);
115115
// 遍历原始图像像素,并进行二值化处理
116-
for (int i = 0; i < height; i++) {
117-
for (int j = 0; j < width; j++) {
116+
for (int i = 0; i < width; i++) {
117+
for (int j = 0; j < height; j++) {
118118
// 得到当前的像素值
119119
int pixel = bitmap.getPixel(i, j);
120120
// 得到Alpha通道的值

sample/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ android {
5757
dependencies {
5858

5959
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-beta-3'
60-
// implementation project(':czxing')
61-
implementation 'me.devilsen:CZXing:0.9.9'
60+
implementation project(':czxing')
61+
// implementation 'me.devilsen:CZXing:0.9.9'
6262

6363
implementation rootProject.ext.appcompat
6464
testImplementation rootProject.ext.junit

0 commit comments

Comments
 (0)