QRScanReader.java
9.18 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package com.lewin.qrcode;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.DecodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.PlanarYUVLuminanceSource;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import java.util.Hashtable;
/**
* Created by lewin on 2018/3/14.
*/
public class QRScanReader extends ReactContextBaseJavaModule {
public QRScanReader(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "QRScanReader";
}
@ReactMethod
public void readerQR(String fileUrl, Promise promise ) {
Result result = scanningImage(fileUrl);
if(result == null){
promise.reject("404","没有相关的二维码");
// result = decodeBarcodeRGB(fileUrl);
// if(result == null){
// result = decodeBarcodeYUV(fileUrl);
// if(result == null){
// promise.reject("404","没有相关的二维码");
// }else{
// promise.resolve(result.getText());
// }
// }else{
// promise.resolve(result.getText());
// }
}else{
promise.resolve(result.getText());
}
}
/**
* 扫描二维码图片的方法
* @param path
* @return
*/
public Result scanningImage(String path) {
if (path == null || path.length() == 0) {
return null;
}
Hashtable<DecodeHintType, String> hints = new Hashtable<>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF8"); //设置二维码内容的编码
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; // 先获取原大小
Bitmap scanBitmap = BitmapFactory.decodeFile(path, options);
options.inJustDecodeBounds = false; // 获取新的大小
int sampleSize = (int) (options.outHeight / (float) 200);
if (sampleSize <= 0)
sampleSize = 1;
options.inSampleSize = sampleSize;
scanBitmap = BitmapFactory.decodeFile(path, options);
int width=scanBitmap.getWidth();
int height=scanBitmap.getHeight();
int[] pixels=new int[width*height];
scanBitmap.getPixels(pixels,0,width,0,0,width,height);//获取图片像素点
RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap.getWidth(),scanBitmap.getHeight(),pixels);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
try {
return reader.decode(bitmap1, hints);
} catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
}
return null;
}
/**
* 解析二维码(使用解析RGB编码数据的方式)
*
* @param path
* @return
*/
public static Result decodeBarcodeRGB(String path) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 1;
Bitmap barcode = BitmapFactory.decodeFile(path, opts);
Result result = decodeBarcodeRGB(barcode);
barcode.recycle();
barcode = null;
return result;
}
/**
* 解析二维码 (使用解析RGB编码数据的方式)
*
* @param barcode
* @return
*/
public static Result decodeBarcodeRGB(Bitmap barcode) {
int width = barcode.getWidth();
int height = barcode.getHeight();
int[] data = new int[width * height];
barcode.getPixels(data, 0, width, 0, 0, width, height);
RGBLuminanceSource source = new RGBLuminanceSource(width, height, data);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
Result result = null;
try {
result = reader.decode(bitmap1);
} catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
}
barcode.recycle();
barcode = null;
return result;
}
/**
* 解析二维码(使用解析YUV编码数据的方式)
*
* @param path
* @return
*/
public static Result decodeBarcodeYUV(String path) {
if (path == null || path.length() == 0) {
return null;
}
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 1;
Bitmap barcode = BitmapFactory.decodeFile(path, opts);
Result result = decodeBarcodeYUV(barcode);
barcode.recycle();
barcode = null;
return result;
}
/**
* 解析二维码(使用解析YUV编码数据的方式)
*
* @param barcode
* @return
*/
public static Result decodeBarcodeYUV(Bitmap barcode) {
if (null == barcode) {
return null;
}
int width = barcode.getWidth();
int height = barcode.getHeight();
//以argb方式存放图片的像素
int[] argb = new int[width * height];
barcode.getPixels(argb, 0, width, 0, 0, width, height);
//将argb转换为yuv
byte[] yuv = new byte[width * height * 3 / 2];
encodeYUV420SP(yuv, argb, width, height);
//解析YUV编码方式的二维码
Result result = decodeBarcodeYUV(yuv, width, height);
barcode.recycle();
barcode = null;
return result;
}
/**
* 解析二维码(使用解析YUV编码数据的方式)
*
* @param yuv
* @param width
* @param height
* @return
*/
private static Result decodeBarcodeYUV(byte[] yuv, int width, int height) {
long start = System.currentTimeMillis();
MultiFormatReader multiFormatReader = new MultiFormatReader();
multiFormatReader.setHints(null);
Result rawResult = null;
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(yuv, width, height, 0, 0,
width, height, false);
if (source != null) {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
rawResult = multiFormatReader.decodeWithState(bitmap);
} catch (ReaderException re) {
re.printStackTrace();
} finally {
multiFormatReader.reset();
multiFormatReader = null;
}
}
long end = System.currentTimeMillis();
return rawResult;
}
/**
* RGB转YUV的公式是:
* Y=0.299R+0.587G+0.114B;
* U=-0.147R-0.289G+0.436B;
* V=0.615R-0.515G-0.1B;
*
* @param yuv
* @param argb
* @param width
* @param height
*/
private static void encodeYUV420SP(byte[] yuv, int[] argb, int width, int height) {
// 帧图片的像素大小
final int frameSize = width * height;
// ---YUV数据---
int Y, U, V;
// Y的index从0开始
int yIndex = 0;
// UV的index从frameSize开始
int uvIndex = frameSize;
// ---颜色数据---
int R, G, B;
int rgbIndex = 0;
// ---循环所有像素点,RGB转YUV---
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
R = (argb[rgbIndex] & 0xff0000) >> 16;
G = (argb[rgbIndex] & 0xff00) >> 8;
B = (argb[rgbIndex] & 0xff);
//
rgbIndex++;
// well known RGB to YUV algorithm
Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
Y = Math.max(0, Math.min(Y, 255));
U = Math.max(0, Math.min(U, 255));
V = Math.max(0, Math.min(V, 255));
// NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
// meaning for every 4 Y pixels there are 1 V and 1 U. Note the sampling is every other
// pixel AND every other scan line.
// ---Y---
yuv[yIndex++] = (byte) Y;
// ---UV---
if ((j % 2 == 0) && (i % 2 == 0)) {
//
yuv[uvIndex++] = (byte) V;
//
yuv[uvIndex++] = (byte) U;
}
}
}
}
}