Getting started with Android: zxing Study Notes (5)

Source: Internet
Author: User

Mo daochang is early and has more early pedestrians.

Barcode preview is not just as simple as the above two articles. There are other processing methods, such as flash, zoom-in, and optimal preview sizes. This does not affect your understanding of the Code. After you know how to use camera, you can start to see how barcode handler can efficiently identify and process the code. In the android folder, there is a thread class: decodethread, two handler classes: captureactivityhandler and decodehandler. When I didn't take two classes seriously, I always thought these two handler methods were taken for granted and thought I knew it. Later we found that this is not so simple, especially the implementation of decodethread is a classic implementation of working threads in Android.

In barcode handler, there are a lot of messages transmitted, and there are two handler for processing messages. How to send a message to a specified handler is clear in the code, and the message is bound to the specified handler. As mentioned above, there are main thread and working thread in Android. Main threads such as activity and work threads created by themselves. If you want to update the UI, you need to process them in main thread. You can use handler to transmit messages, send the message to the Message Queue of main thread.

Barcode Handler has two threads and two handler. In these four classes, the relationship between them is determined by adding such a statement:

1 system. Out. println (TAG + "the worker thread id =" + thread. currentthread (). GETID (); // judge the thread ID

The final running result:

1 01-12 02:41:12.594: I/System.out(655): CaptureActivity The main thread id = 1
2 01-12 02:41:14.605: I/System.out(655): CaptureActivityHandler The handler thread id = 1
3 01-12 02:41:12.946: I/System.out(655): DecodeThread The worker thread id = 13
4 01-12 02:41:13.094: I/System.out(655): DecodeHandler The handler thread id = 13

It can be seen that both handler belong to their thread respectively. However, when creating these two handler, there is a big difference. The creation of captureactivityhandler is just a simple new, with no other assistance. This means that when the main activity is created, the system creates a lo for it by default, which is responsible for managing the message loop of the thread, receiving and sending messages, and so on, and does not need to be specified. However, by default, the system does not create logoff for a self-created thread, and you need to create a message loop for it.

Let's take a look at the code of decodethread: removed the code irrelevant to understanding the thread.

1 final class decodethread extends thread {
2 public static string tag = decodethread. Class. getsimplename ();
3 private final captureactivity activity;
4 private handler;
5 private final countdownlatch handlerinitlatch; // The counter lock.
6 decodethread (captureactivity activity,
7 vector <barcodeformat> decodeformats,
8 string characterset,
9 resultpointcallback ){
10
11 This. Activity = activity;
12 handlerinitlatch = new countdownlatch (1); // starts from 1 to count
13
14}
15
16 handler gethandler (){
17 try {
18 handlerinitlatch. Await (); // the handler is initialized before returning the result. Change the Count lock to equal countdown --> 0.
19} catch (interruptedexception IE ){
20 // continue?
21}
22 return handler;
23}
24
25 @ override
26 Public void run (){
27 logoff. Prepare ();
28 handler = new decodehandler (activity, hints );
29 handlerinitlatch. Countdown (); // start to count, countdown-1 to 0;
30 system. Out. println (TAG + "the worker thread id =" + thread. currentthread (). GETID (); // judge the thread ID
31 logoff. Loop ();
32}

Previously, a countdownlatch type variable is defined, which is a lock used for inverted counting. The usage is quite simple, for example, in the Code, first new countdownlatch (1), the Count value is 1, handlerinitlatch. Countdown (), and start to countdown. Handlerinitlatch. Await () is always blocked if the Count value does not change to 0. Return handler is not returned until the Count value is 0. Therefore, a null handler is not returned when gethandler is called.

When creating the handler of the decodethread thread, The logoff is called in the thread first. prepare () is used to create a message queue, create a handler object attached to the thread, and call logoff. loop () enters the message loop. This loop () loop does not return immediately. You need to call logoff manually. mylogoff (). quit () will return. This is a simple and rapid way to create a working thread and allocate a message queue to it.

As mentioned above, the automatic focus of camera only regularly distributes automatic focus requests to captureactivityhandler. This is a message transmission:

1 Message message = autoFocusHandler.obtainMessage(autoFocusMessage, success);
2 autoFocusHandler.sendMessageDelayed(message, AUTOFOCUS_INTERVAL_MS);

In this way, a message is created and the time is cut off for sending.

For more information about the message processing mechanism of Android, click here. This article also introduces logoff. These two articles are more thorough and practical. This also found a masterpiece.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.