lmw
2024-06-16 03172fc2d9a7717f4a9d8de1c5eca3158a550b30
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
package com.zxing.scanner;
 
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.view.View;
 
import com.google.zxing.BarcodeFormat;
import com.zxing.scanner.camera.open.CameraFacing;
import com.zxing.scanner.common.Scanner;
import com.zxing.scanner.decode.DecodeFormatManager;
 
import java.util.Collection;
 
/**
 * Created by hupei on 2017/11/21.
 */
 
public final class ScannerOptions {
 
    public static final int DEFAULT_LASER_LINE_HEIGHT = 2;//扫描线默认高度
    private LaserStyle laserStyle = LaserStyle.COLOR_LINE;
    private int laserLineColor = Scanner.color.VIEWFINDER_LASER;//扫描线颜色rgb值
    private int laserLineResId;//扫描线资源文件
    private int laserLineHeight = DEFAULT_LASER_LINE_HEIGHT;//扫描线高度,网络样式无效,单位dp
    private int laserLineMoveSpeed = 6;//扫描线移动间距,默认每毫秒移动6px,单位px
    private boolean laserMoveFullScreen;//扫描线全屏移动,默认在扫描框内移动
    private int frameStrokeColor = Color.WHITE;//扫描边框颜色rgb值
    private float frameStrokeWidth = 1f;//扫描边框的宽度,单位px
    private int frameWidth;//扫描框的宽度,单位dp
    private int frameHeight;//扫描框的高度,单位dp
    private int frameCornerColor = laserLineColor;//扫描框4角颜色rgb值
    private int frameCornerLength = 15;//扫描框4角长度,单位dp 默认15
    private int frameCornerWidth = 2;//扫描框4角宽度,单位dp 默认2
    private boolean frameCornerInside = true;//扫描框4角是否在框外,默认框内
    private boolean frameCornerHide;//是否隐藏扫描框4角,默认显示
    private int frameTopMargin;//扫描框与顶部间距,单位dp,默认居中
    private boolean frameHide;//是否隐藏扫描框,默认显示
    private boolean viewfinderHide;//是否隐藏整个取景视图,包括文字,默认显示
    private String tipText = "将二维码放入框内,即可自动扫描";//提示文字
    private int tipTextColor = Color.WHITE;//提示文字颜色rgb值,默认白色
    private int tipTextSize = 15;//提交文字大小,单位sp 默认15
    private boolean tipTextToFrameTop;//是否在扫描框上方,默认下方
    private int tipTextToFrameMargin = 20;//离扫描框间距,单位dp 默认20
    private int mediaResId;//扫描成功音频资源文件
    private Collection<BarcodeFormat> decodeFormats;//解码类型,默认解全部
    private boolean createQrThumbnail;//生成扫描结果缩略图,默认不生成,也就是扫描成功后的第三个参数
    private boolean showQrThumbnail;//是否显示扫描结果缩略图在扫描界面
    private CameraFacing cameraFacing = CameraFacing.BACK;//启动摄像头位置,默认后置
    private boolean scanFullScreen;//是否全屏扫描识别,默认扫描框内识别
    private boolean scanInvert;//是否扫描反色二维码(用于黑底白码)
    private double cameraZoomRatio;//相机变焦比率
    private ViewfinderCallback viewfinderCallback;
    private int frameOutsideColor = Scanner.color.VIEWFINDER_MASK;//扫描框以外区域半透明黑色
    private String characterSet;
 
    protected ScannerOptions() {
    }
 
    public LaserStyle getLaserStyle() {
        return laserStyle;
    }
 
    public int getLaserLineColor() {
        return laserLineColor;
    }
 
    public int getLaserLineResId() {
        return laserLineResId;
    }
 
    public int getLaserLineHeight() {
        return laserLineHeight;
    }
 
    public int getLaserLineMoveSpeed() {
        return laserLineMoveSpeed;
    }
 
    public boolean isLaserMoveFullScreen() {
        return laserMoveFullScreen;
    }
 
    public int getFrameStrokeColor() {
        return frameStrokeColor;
    }
 
    public float getFrameStrokeWidth() {
        return frameStrokeWidth;
    }
 
    public int getFrameWidth() {
        return frameWidth;
    }
 
    public int getFrameHeight() {
        return frameHeight;
    }
 
    public int getFrameCornerColor() {
        return frameCornerColor;
    }
 
    public int getFrameCornerLength() {
        return frameCornerLength;
    }
 
    public int getFrameCornerWidth() {
        return frameCornerWidth;
    }
 
    public boolean isFrameCornerInside() {
        return frameCornerInside;
    }
 
    public boolean isFrameCornerHide() {
        return frameCornerHide;
    }
 
    public int getFrameTopMargin() {
        return frameTopMargin;
    }
 
    public boolean isFrameHide() {
        return frameHide;
    }
 
    public boolean isViewfinderHide() {
        return viewfinderHide;
    }
 
    public String getTipText() {
        return tipText;
    }
 
    public int getTipTextColor() {
        return tipTextColor;
    }
 
    public int getTipTextSize() {
        return tipTextSize;
    }
 
    public boolean isTipTextToFrameTop() {
        return tipTextToFrameTop;
    }
 
    public int getTipTextToFrameMargin() {
        return tipTextToFrameMargin;
    }
 
    public int getMediaResId() {
        return mediaResId;
    }
 
    public Collection<BarcodeFormat> getDecodeFormats() {
        return decodeFormats;
    }
 
    public boolean isCreateQrThumbnail() {
        return createQrThumbnail;
    }
 
    public boolean isShowQrThumbnail() {
        return showQrThumbnail;
    }
 
    public CameraFacing getCameraFacing() {
        return cameraFacing;
    }
 
    public boolean isScanFullScreen() {
        return scanFullScreen;
    }
 
    public boolean isScanInvert() {
        return scanInvert;
    }
 
    public double getCameraZoomRatio() {
        return cameraZoomRatio;
    }
 
    public ViewfinderCallback getViewfinderCallback() {
        return viewfinderCallback;
    }
 
    public int getFrameOutsideColor() {
        return frameOutsideColor;
    }
 
    public String getCharacterSet() {
        return characterSet;
    }
 
    public enum LaserStyle {
        /**
         * 颜色线值样式
         */
        COLOR_LINE
        /**
         * 资源文件线样式
         */
        , RES_LINE
        /**
         * 资源文件网格样式
         */
        , RES_GRID
    }
 
    public interface ViewfinderCallback {
        void onDraw(View view, Canvas canvas, Rect frame);
    }
 
    public static final class Builder {
        private ScannerOptions options;
 
        public Builder() {
            options = new ScannerOptions();
        }
 
        public ScannerOptions build() {
            return options;
        }
 
        /**
         * 扫描线样式
         *
         * @param style 默认为{@link LaserStyle#COLOR_LINE 颜色线}
         * @param value style=COLOR_LINE,value为颜色值rgb,其余样式value为resId
         * @return
         */
        public Builder setLaserStyle(LaserStyle style, int value) {
            options.laserStyle = style;
            if (style == LaserStyle.COLOR_LINE) {
                options.laserLineColor = value;
            } else {
                options.laserLineResId = value;
            }
            return this;
        }
 
        /**
         * 设置扫描线颜色值<br>
         * 只支持扫描线样式为{@link LaserStyle#COLOR_LINE 颜色线}
         *
         * @param color rgb 颜色值
         */
        public Builder setLaserLineColor(int color) {
            options.laserStyle = LaserStyle.COLOR_LINE;
            options.laserLineColor = color;
            return this;
        }
 
        /**
         * 设置扫描线高度<br>
         * 支持扫描线样式为{@link LaserStyle#COLOR_LINE 颜色线}
         * or {@link LaserStyle#RES_LINE 资源文件}
         *
         * @param height dp
         */
        public Builder setLaserLineHeight(int height) {
            options.laserLineHeight = height;
            return this;
        }
 
        /**
         * 设置扫描框线移动间距
         *
         * @param moveSpeed 每毫秒移动 moveSpeed 像素 px
         * @return
         */
        public Builder setLaserMoveSpeed(int moveSpeed) {
            options.laserLineMoveSpeed = moveSpeed;
            return this;
        }
 
        /**
         * 扫描线是否全屏移动
         *
         * @param fullScreen true全屏,false扫描框内
         * @return
         */
        public Builder setLaserMoveFullScreen(boolean fullScreen) {
            options.laserMoveFullScreen = fullScreen;
            return this;
        }
 
        /**
         * 扫描边框颜色值,默认白色
         *
         * @param color rgb颜色值
         * @return
         */
        public Builder setFrameStrokeColor(int color) {
            options.frameStrokeColor = color;
            return this;
        }
 
        /**
         * 扫描边框宽度,默认1px
         *
         * @param width 单位px
         * @return
         */
        public Builder setFrameStrokeWidth(float width) {
            options.frameStrokeWidth = width;
            return this;
        }
 
        /**
         * 设置扫描框大小
         *
         * @param width  dp
         * @param height dp
         */
        public Builder setFrameSize(int width, int height) {
            options.frameWidth = width;
            options.frameHeight = height;
            return this;
        }
 
        /**
         * 设置扫描框4角颜色值
         *
         * @param color rgb
         */
        public Builder setFrameCornerColor(int color) {
            options.frameCornerColor = color;
            return this;
        }
 
        /**
         * 设置扫描框4角长度
         *
         * @param length dp
         */
        public Builder setFrameCornerLength(int length) {
            options.frameCornerLength = length;
            return this;
        }
 
        /**
         * 设置扫描框4角宽度
         *
         * @param width dp
         */
        public Builder setFrameCornerWidth(int width) {
            options.frameCornerWidth = width;
            return this;
        }
 
        /**
         * 设置扫描框4角是否在框内,默认框外
         *
         * @param inside true内部
         * @return
         */
        public Builder setFrameCornerInside(boolean inside) {
            options.frameCornerInside = inside;
            return this;
        }
 
        /**
         * 是否隐藏扫描框4角
         *
         * @param hide true隐藏
         * @return
         */
        public Builder setFrameCornerHide(boolean hide) {
            options.frameCornerHide = hide;
            if (!hide)
                options.laserMoveFullScreen = false;
            return this;
        }
 
        /**
         * 设置扫描框与屏幕顶部距离
         *
         * @param margin dp
         */
        public Builder setFrameTopMargin(int margin) {
            options.frameTopMargin = margin;
            return this;
        }
 
        /**
         * 是否隐藏扫描框,默认不隐藏
         *
         * @param hide true隐藏
         * @return
         */
        public Builder setFrameHide(boolean hide) {
            options.frameHide = hide;
            if (!hide)//非显示则关闭全屏移动扫描线
                options.laserMoveFullScreen = false;
            return this;
        }
 
        /**
         * 设置隐藏取景视图包括文字,默认不隐藏
         *
         * @param hide
         * @return
         */
        public Builder setViewfinderHide(boolean hide) {
            options.viewfinderHide = hide;
            return this;
        }
 
        /**
         * 设置文字
         *
         * @param text 文字
         */
        public Builder setTipText(String text) {
            options.tipText = text;
            return this;
        }
 
        /**
         * 设置文字颜色
         *
         * @param color 文字颜色
         */
        public Builder setTipTextColor(int color) {
            options.tipTextColor = color;
            return this;
        }
 
        /**
         * 设置文字大小
         *
         * @param size 文字大小 sp
         */
        public Builder setTipTextSize(int size) {
            options.tipTextSize = size;
            return this;
        }
 
        /**
         * 设置文字与扫描框间距
         *
         * @param margin 间距 单位dp
         */
        public Builder setTipTextToFrameMargin(int margin) {
            options.tipTextToFrameMargin = margin;
            return this;
        }
 
        /**
         * 设置文字是否在扫描框上方,默认下方
         *
         * @param top true=上方,false=下方
         */
        public Builder setTipTextToFrameTop(boolean top) {
            options.tipTextToFrameTop = top;
            return this;
        }
 
        /**
         * 设置扫描成功的音频
         *
         * @param resId
         */
        public Builder setMediaResId(int resId) {
            options.mediaResId = resId;
            return this;
        }
 
        /**
         * 设置扫描解码类型(二维码、一维码、商品条码)
         *
         * @param scanMode {@linkplain Scanner.ScanMode mode}
         * @return
         */
        public Builder setScanMode(String scanMode) {
            options.decodeFormats = DecodeFormatManager.parseDecodeFormats(scanMode);
            return this;
        }
 
        /**
         * 设置扫描解码类型
         *
         * @param barcodeFormat
         * @return
         */
        public Builder setScanMode(BarcodeFormat... barcodeFormat) {
            options.decodeFormats = DecodeFormatManager.parseDecodeFormats(barcodeFormat);
            return this;
        }
 
        /**
         * 是否创建扫描结果缩略图,也就是扫描成功后的第三个参数,默认不创建
         *
         * @param thumbnail
         * @return
         */
        public Builder setCreateQrThumbnail(boolean thumbnail) {
            options.createQrThumbnail = thumbnail;
            return this;
        }
 
        /**
         * 是否显示扫描结果缩略图在扫描界面,默认不显示<br>
         * {@link #setCreateQrThumbnail(boolean) setCreateQrThumbnail(true)才有效}
         *
         * @param show
         * @return
         */
        public Builder setShowQrThumbnail(boolean show) {
            options.showQrThumbnail = show;
            return this;
        }
 
        /**
         * 设置扫描摄像头,默认后置
         *
         * @param cameraFacing
         * @return
         */
        public Builder setCameraFacing(CameraFacing cameraFacing) {
            options.cameraFacing = cameraFacing;
            return this;
        }
 
        /**
         * 是否全屏扫描,默认非全屏扫描<br>
         * true=全屏扫描,则隐藏扫描框,扫描框4角,扫描线全屏上下移动<br>
         * 如全屏扫描情况下,仍需显示扫描框、扫描框4角及扫描线在扫描框内上下的移动,则调用该方法之后再调用:
         * {@link #setFrameHide(boolean) setFrameHide(false)},
         * {@link #setFrameCornerHide(boolean) setFrameCornerHide(false)},
         * {@link #setLaserMoveFullScreen(boolean) setLaserMoveFullScreen(false)}
         *
         * @param scanFullScreen
         * @return
         */
        public Builder setScanFullScreen(boolean scanFullScreen) {
            options.scanFullScreen = scanFullScreen;
            if (scanFullScreen) {
                options.frameHide = true;
                options.frameCornerHide = true;
                options.laserMoveFullScreen = true;
            }
            return this;
        }
 
        /**
         * 是否扫描反色二维码(黑底白码)
         *
         * @param invertScan
         * @return
         */
        public Builder setScanInvert(boolean invertScan) {
            options.scanInvert = invertScan;
            return this;
        }
 
        /**
         * 设置相机变焦比率
         *
         * @param cameraZoomRatio >0 建议2倍
         * @return
         */
        public Builder setCameraZoomRatio(double cameraZoomRatio) {
            options.cameraZoomRatio = cameraZoomRatio;
            return this;
        }
 
        public Builder setViewfinderCallback(ViewfinderCallback callback) {
            options.viewfinderCallback = callback;
            return this;
        }
 
        /**
         * 设置扫描框以外区域颜色值
         *
         * @param color rgb
         * @return
         */
        public Builder setFrameOutsideColor(int color) {
            options.frameOutsideColor = color;
            return this;
        }
 
        /**
         * 指定编码解析二维码
         *
         * @param characterSet
         * @return
         */
        public Builder setCharacterSet(String characterSet) {
            options.characterSet = characterSet;
            return this;
        }
    }
}