-
Notifications
You must be signed in to change notification settings - Fork 487
/
Copy pathMultiFormatAnalyzer.java
103 lines (86 loc) · 3.54 KB
/
MultiFormatAnalyzer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.king.zxing.analyze;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.PlanarYUVLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.common.GlobalHistogramBinarizer;
import com.google.zxing.common.HybridBinarizer;
import com.king.logx.LogX;
import com.king.zxing.DecodeConfig;
import java.util.Map;
import androidx.annotation.Nullable;
/**
* 多格式分析器:主要用于分析识别条形码/二维码
*
* @author <a href="mailto:[email protected]">Jenly</a>
* <p>
* <a href="https://github.com/jenly1314">Follow me</a>
*/
@SuppressWarnings("unused")
public class MultiFormatAnalyzer extends AreaRectAnalyzer {
MultiFormatReader mReader;
public MultiFormatAnalyzer() {
this((DecodeConfig) null);
}
public MultiFormatAnalyzer(@Nullable Map<DecodeHintType, Object> hints) {
this(new DecodeConfig().setHints(hints));
}
public MultiFormatAnalyzer(@Nullable DecodeConfig config) {
super(config);
initReader();
}
private void initReader() {
mReader = new MultiFormatReader();
}
@Nullable
@Override
public Result analyze(byte[] data, int dataWidth, int dataHeight, int left, int top, int width, int height) {
Result rawResult = null;
try {
long start = System.currentTimeMillis();
mReader.setHints(mHints);
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, dataWidth, dataHeight, left, top, width, height, false);
rawResult = decodeInternal(source, isMultiDecode);
if (rawResult == null && mDecodeConfig != null) {
if (mDecodeConfig.isSupportVerticalCode()) {
byte[] rotatedData = new byte[data.length];
for (int y = 0; y < dataHeight; y++) {
for (int x = 0; x < dataWidth; x++) {
rotatedData[x * dataHeight + dataHeight - y - 1] = data[x + y * dataWidth];
}
}
rawResult = decodeInternal(new PlanarYUVLuminanceSource(rotatedData, dataHeight, dataWidth, top, left, height, width, false), mDecodeConfig.isSupportVerticalCodeMultiDecode());
}
if (rawResult == null && mDecodeConfig.isSupportLuminanceInvert()) {
rawResult = decodeInternal(source.invert(), mDecodeConfig.isSupportLuminanceInvertMultiDecode());
}
}
if (rawResult != null) {
long end = System.currentTimeMillis();
LogX.d("Found barcode in " + (end - start) + " ms");
}
} catch (Exception ignored) {
} finally {
mReader.reset();
}
return rawResult;
}
private Result decodeInternal(LuminanceSource source, boolean isMultiDecode) {
Result result = null;
try {
try {
// 采用HybridBinarizer解析
result = mReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(source)));
} catch (Exception ignored) {
}
if (isMultiDecode && result == null) {
// 如果没有解析成功,再采用GlobalHistogramBinarizer解析一次
result = mReader.decodeWithState(new BinaryBitmap(new GlobalHistogramBinarizer(source)));
}
} catch (Exception ignored) {
}
return result;
}
}