Getting started with Android: zxing Study Notes (2)

Source: Internet
Author: User

The previous article introduced the process of zxing scanning the QR code. When I first read this code, I didn't understand much about it, but many details were unclear. After a deeper understanding, the Code is designed with high quality. The whole process of scanning two-dimensional code and one-dimensional code is very fast and efficient. Recently I found that there is a two-dimensional square id on Weibo, And the QR code figures are very Q. I don't know how to get it out. programmers can use this cute QR code for romance.

The main activity and camera are two important parts of zxing's Android code. This article describes how to use Android camera. Open the barcode pipeline under zxing and the following interface will be displayed. To better understand camera, first introduce this interface.

I was not familiar with this interface when I first got started with Android. I carefully read the code and understood it a little later. This interface is defined mainly in viewfinderview. java. This class inherits the View class and implements custom views. View is a canvas corresponding to the screen. You can draw any design on the screen. The most important function is to reload the ondraw function and draw it in it. Let's take a look at how viewfinderview achieves the feeling on the interface.

A picture is divided into two parts: a translucent part of the outside, and a transparent part in the middle. A translucent screen consists of four rectangles.

1 paint.setColor(resultBitmap != null ? resultColor : maskColor);
2 canvas.drawRect(0, 0, width, frame.top, paint);
3 canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
4 canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint);
5 canvas.drawRect(0, frame.bottom + 1, width, height, paint);

The drawrect function has five parameters. The first four parameters constitute two coordinates and form a rectangle, followed by a paint brush.

The entire transparent area in the middle is also composed of four rectangles, but each rectangle is very narrow and only one or two pixels are used to form a straight line.

1 paint.setColor(frameColor);
2 canvas.drawRect(frame.left, frame.top, frame.right + 1, frame.top + 2, paint);
3 canvas.drawRect(frame.left, frame.top + 2, frame.left + 2, frame.bottom - 1, paint);
4 canvas.drawRect(frame.right - 1, frame.top, frame.right + 1, frame.bottom - 1, paint);
5 canvas.drawRect(frame.left, frame.bottom - 1, frame.right + 1, frame.bottom + 1, paint);

The same is true for the middle red scanning line.

The last sentence of the ondraw () function is:

1 postInvalidateDelayed(ANIMATION_DELAY, frame.left, frame.top, frame.right, frame.bottom);

This sentence is critical. The postinvalidatedelayed function is mainly used to refresh the UI interface in non-UI threads, refresh the specified range for each animation_delay time. Therefore, the ondraw function is constantly called and green feature points are added to the interface. At the beginning, I didn't understand how to add green markup points. Now I have read it again. After obtaining the image in camera focus, use the library in core for parsing to obtain the coordinates of the feature points. Then, use the viewfinderresultpointcallback class callback to add the feature points to the arraylist container in viewfinderview.

1 public void foundPossibleResultPoint(ResultPoint point) {
2 viewfinderView.addPossibleResultPoint(point);
3 }

The feature points of this function are added to possibleresultpoints. Because you are not familiar with Java and do not know that the "=" value assignment is a shallow copy for the list, you are always thinking that the possibleresultpoints object is not assigned a value, how to obtain these features. It is only known later that this "=" value assignment is a small copy. To implement deep copy for a predefined set, you can use the constructor,

For example, list <resultpoint> points = new list <resultpoint> (possibleresultpoints );

 1   public void addPossibleResultPoint(ResultPoint point) {
2 List<ResultPoint> points = possibleResultPoints;
3 synchronized (point) {
4 points.add(point);
5 int size = points.size();
6 if (size > MAX_RESULT_POINTS) {
7 // trim it
8 points.subList(0, size - MAX_RESULT_POINTS / 2).clear();
9 }
10 }
11 }

If you want to thoroughly view the view refresh process and the specific implementation, check the following link. This series of articles is very detailed.

Androidbluetooth blog: view programming (2): invalidate ()

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.