阅读 723

zxing开源库工作流程源码详解

代码获取

作为移动客户端开发者来说,对二维码识别或二维码生成相关的开发需求肯定并不陌生,Android开发二维码相关的功能通常都会使用或参考大名鼎鼎的zxing库。而本文则主要是通过源码分析一下该开源库扫描二维码的工作流程,对这块能有个更深的了解。

首先使用git将项目代码clone到本地,新建项目,将zxing文件夹中的android以及core文件夹代码覆盖到对应的目录下,稍作一些修改即可运行一个简单的二维码扫描的示例应用。

整体流程

Demo代码运行起来后,会进入一个扫描的主功能界面,将扫描框对准一个二维码即可弹出解析结果信息的浮框。通过AndroidManifest.xml文件中可以得知这个页面对应的类为CaptureActivity.java,我们便从这个类开始,分析整个二维码扫描的流程。

要分析一个Activity,当然要从它的生命周期所对应的各个方法说起。首先我们来看它的onCreate()方法:

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    //保持屏幕常亮
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.capture);

    hasSurface = false;
    inactivityTimer = new InactivityTimer(this);
    beepManager = new BeepManager(this);
    ambientLightManager = new AmbientLightManager(this);

    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
}
复制代码

这个方法代码不多,也很容易看懂,主要就是做一些初始化的工作。InactivityTimer主要是用来监听当手机是使用电池而不是充电状态时,如果5分钟内没有做任何操作,则主动finish掉activity。BeepManager负责扫描到结果后震动或铃声相关,AmbientLightManager则是负责控制闪光灯。

继续往下走看onResume()方法:

@Override
protected void onResume() {
    super.onResume();

    ...

    // CameraManager must be initialized here, not in onCreate(). This is necessary because we don't
	// want to open the camera driver and measure the screen size if we're going to show the help on
	// first launch. That led to bugs where the scanning rectangle was the wrong size and partially
	// off screen.
	cameraManager = new CameraManager(getApplication());

    viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
    viewfinderView.setCameraManager(cameraManager);

    ...

    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
    SurfaceHolder surfaceHolder = surfaceView.getHolder();
    if (hasSurface) {
        // The activity was paused but not stopped, so the surface still exists. Therefore
        // surfaceCreated() won't be called, so init the camera here.
        initCamera(surfaceHolder);
    } else {
        // Install the callback and wait for surfaceCreated() to init the camera.
        surfaceHolder.addCallback(this);
    }
}
复制代码

这个方法很重要,初始化了CameraManager,扫描二维码毋庸置疑是需要用到相机,通过相机预览的一帧一帧的图片,去解析上面可能存在的二维码信息。而在最后面还初始化了SurfaceView,通过hasSurface来决定是走initCamera(surfaceHolder)还是surfaceHolder.addCallback(this)。在上面的onCreate()中我们可以看到hasSurface被初始化成false,所以这里走的应该是else的代码块。CaptureActivity实现了SurfaceHolder.Callback接口,因此该方法绑定了surfaceHolder的回调。当SurfaceView添加到 activity 中时,会调用surfaceCreated()

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if (holder == null) {
        Log.e(TAG, "*** WARNING *** surfaceCreated() gave us a null surface!");
    }
    if (!hasSurface) {
        hasSurface = true;
        initCamera(holder);
    }
}
复制代码

这里我们看到会改变hasSurface的状态,然后走initCamera(holder),和onResume()hasSurfacetrue时做的操作是一样的:

cameraManager.openDriver(surfaceHolder);
// Creating the handler starts the preview, which can also throw a RuntimeException.
if (handler == null) {
    handler = new CaptureActivityHandler(this, decodeFormats, decodeHints, characterSet, cameraManager);
}
复制代码

cameraManager 打开了驱动,并且把自己传入一个CaptureActivityHandler对象中去,那这个CaptureActivityHandler看起来像是一个进行消息通知的 Handler,它的具体作用又是什么呢?我们来看看它的构造方法:

CaptureActivityHandler(CaptureActivity activity,
                       Collection<BarcodeFormat> decodeFormats,
                       Map<DecodeHintType,?> baseHints,
                       String characterSet,
                       CameraManager cameraManager) {
    this.activity = activity;
    decodeThread = new DecodeThread(activity, decodeFormats, baseHints, characterSet,new ViewfinderResultPointCallback(activity.getViewfinderView()));
    decodeThread.start();
    state = State.SUCCESS;

    // Start ourselves capturing previews and decoding.
    this.cameraManager = cameraManager;
    cameraManager.startPreview();
    restartPreviewAndDecode();
}
复制代码

通过进入CaptureActivityHandler.java可以看到该类确实继承了Handler,并且在它的构造方法中开启了一个DecodeThread的线程,并且调用了cameraManagerstartPreview()方法:

Asks the camera hardware to begin drawing preview frames to the screen.

开启相机预览后,再看下面的restartPreviewAndDecode()

private void restartPreviewAndDecode() {
    if (state == State.SUCCESS) {
        state = State.PREVIEW;
        cameraManager.requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
        activity.drawViewfinder();
    }
}

public synchronized void requestPreviewFrame(Handler handler, int message) {
    OpenCamera theCamera = camera;
    if (theCamera != null && previewing) {
        previewCallback.setHandler(handler, message);
        theCamera.getCamera().setOneShotPreviewCallback(previewCallback);
    }
}
复制代码

可以看到这个 handler 会一直传递到一个previewCallback对象中去,而PreviewCallbacksetOneShotPreviewCallback()方法的一个回调,setOneShotPreviewCallback方法上的注释说明:

Installs a callback to be invoked for the next preview frame in addition to displaying it on the screen. After one invocation, the callback is cleared. This method can be called any time, even when preview is live. Any other preview callbacks are overridden.

使用此方法注册预览回调接口时,会将下一帧数据回调给onPreviewFrame()方法,调用完成后这个回调接口将被销毁,也就是只会回调一次预览帧数据。继续顺着这个方法走下去,看回调方法onPreviewFrame()

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
    Point cameraResolution = configManager.getCameraResolution();
    Handler thePreviewHandler = previewHandler;
    if (cameraResolution != null && thePreviewHandler != null) {
        Message message = thePreviewHandler.obtainMessage(previewMessage, cameraResolution.x,cameraResolution.y, data);
        message.sendToTarget();
        previewHandler = null;
    } else {
        Log.d(TAG, "Got preview callback, but no handler or resolution available");
    }
}
复制代码

这里将返回的 byte 数组数据和预览帧的宽高信息通过 handler 进行通知,这个 handler 就是上文中传过来的decodeThread.getHandler()previewMessageR.id.decode,目的就是把图片数据拿到该线程中进行解析。我们跟进到DecodeHandler.java中查看handleMessage()方法:

@Override
public void handleMessage(Message message) {
    if (message == null || !running) {
        return;
    }
    switch (message.what) {
        case R.id.decode:
            decode((byte[]) message.obj, message.arg1, message.arg2);
            break;
        case R.id.quit:
            running = false;
            Looper.myLooper().quit();
            break;
    }
}

private void decode(byte[] data, int width, int height) {
    long start = System.currentTimeMillis();
  
    //省略具体解析代码
    ...
    Handler handler = activity.getHandler();//CaptureActivityHandler
    if (rawResult != null) {
        // Don't log the barcode contents for security.
        long end = System.currentTimeMillis();
        Log.d(TAG, "Found barcode in " + (end - start) + " ms");
        if (handler != null) {
            Message message = Message.obtain(handler, R.id.decode_succeeded, rawResult);
            Bundle bundle = new Bundle();
            bundleThumbnail(source, bundle);        
            message.setData(bundle);
            message.sendToTarget();
        }
    } else {
        if (handler != null) {
            Message message = Message.obtain(handler, R.id.decode_failed);
            message.sendToTarget();
        }
    }
}
复制代码

上面代码很清晰,DecodeHandler 接收到R.id.decode的消息后,会调用decode()方法去解析传过来的图片数据。经过一系列解析操作,得到结果。如果结果为不为空,则通过CaptureActivityHandler将解析成功的消息传到CaptureActivity中进行后续解析结果展示。而如果解析结果为空呢,说明二维码信息解析失败了,传了一个R.id.decode_failedCaptureActivityHandler中:

@Override
public void handleMessage(Message message) {
    switch (message.what) {

    	...

        case R.id.decode_failed:
            // We're decoding as fast as possible, so when one decode fails, start another.
            state = State.PREVIEW;
            cameraManager.requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
            break;
            
        ...
    }
}

复制代码

可以看到,解析失败时,重新调用requestPreviewFrame获取下一帧预览照片,再拿去解析,知道返回正确结果或者手动退出。

整个过程的时序图如下: