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
/*
 * Copyright (C) 2008 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.zxing.scanner;
 
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
 
import com.google.zxing.Result;
import com.zxing.scanner.camera.CameraManager;
import com.zxing.scanner.common.Scanner;
import com.zxing.scanner.decode.DecodeThread;
 
/**
 * 针对扫描任务的Handler,可接收的message有启动扫描(restart_preview)、扫描成功(decode_succeeded)、扫描失败(decode_failed)等等
 * This class handles all the messaging which comprises the state machine for
 * capture.
 *
 * @author dswitkin@google.com (Daniel Switkin)
 */
final class ScannerViewHandler extends Handler {
 
    public interface HandleDecodeListener {
        void restartPreview();
 
        void decodeSucceeded(Result rawResult, Bitmap barcode, float scaleFactor);
    }
 
    private final DecodeThread decodeThread;
    private State state;
    private final CameraManager cameraManager;
    private HandleDecodeListener handleDecodeListener;
 
    private enum State {
        PREVIEW, SUCCESS, DONE
    }
 
    ScannerViewHandler(ScannerOptions scannerOptions, CameraManager cameraManager
            , HandleDecodeListener handleDecodeListener) {
        this.cameraManager = cameraManager;
        this.handleDecodeListener = handleDecodeListener;
        //启动扫描线程
        decodeThread = new DecodeThread(cameraManager, this
                , scannerOptions.getDecodeFormats(),scannerOptions.getCharacterSet()
                , scannerOptions.isCreateQrThumbnail());
 
        decodeThread.start();
        state = State.SUCCESS;
        //开启相机预览界面
        cameraManager.startPreview();
        //将preview回调函数与decodeHandler绑定、调用viewfinderView
        restartPreviewAndDecode();
    }
 
    @Override
    public void handleMessage(Message message) {
        switch (message.what) {
            case Scanner.RESTART_PREVIEW:
                restartPreviewAndDecode();
                break;
            case Scanner.DECODE_SUCCEEDED:
                state = State.SUCCESS;
                Bundle bundle = message.getData();
                Bitmap barcode = null;
                float scaleFactor = 1.0f;
                if (bundle != null) {
                    byte[] compressedBitmap = bundle
                            .getByteArray(DecodeThread.BARCODE_BITMAP);
                    if (compressedBitmap != null && compressedBitmap.length > 0) {
                        barcode = BitmapFactory.decodeByteArray(compressedBitmap,
                                0, compressedBitmap.length, null);
                        barcode = barcode.copy(Bitmap.Config.ARGB_8888, true);
                    }
                    scaleFactor = bundle.getFloat(DecodeThread.BARCODE_SCALED_FACTOR);
                }
                if (handleDecodeListener != null)
                    handleDecodeListener.decodeSucceeded((Result) message.obj, barcode, scaleFactor);
                break;
            case Scanner.DECODE_FAILED:
                state = State.PREVIEW;
                cameraManager.requestPreviewFrame(decodeThread.getHandler(), Scanner.DECODE);
                break;
            case Scanner.RETURN_SCAN_RESULT:
                break;
            case Scanner.LAUNCH_PRODUCT_QUERY:
                break;
        }
    }
 
    public void quitSynchronously() {
        state = State.DONE;
        cameraManager.stopPreview();
        Message quit = Message.obtain(decodeThread.getHandler(), Scanner.QUIT);
        quit.sendToTarget();
        try {
            decodeThread.join(500L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        removeMessages(Scanner.DECODE_SUCCEEDED);
        removeMessages(Scanner.DECODE_FAILED);
    }
 
    private void restartPreviewAndDecode() {
        if (state == State.SUCCESS) {
            state = State.PREVIEW;
            cameraManager.requestPreviewFrame(decodeThread.getHandler(), Scanner.DECODE);
            if (handleDecodeListener != null)
                handleDecodeListener.restartPreview();
        }
    }
}