Getting started with Android: zxing Study Notes (6)

Source: Internet
Author: User

I want you to ride a motorcycle and leave here along the river until the beach.

I have already written five zxing study notes in succession. At the beginning, I just wanted to keep a simple record of my accumulated experience in learning android, but I didn't expect it to become like I was telling someone something simple about myself. Looking back at these immature notes, I found that the logic of thinking was a bit chaotic, and the details were disorganized, failing to reach the point. Not comprehensive, but thorough. In the process of writing these essays, I have understood the barcode scheme design more and understood a lot of previous insights, especially the potential misunderstanding of course. View, camera, thread, and logoff I carefully read the materials again and read the materials carefully multiple times. Be careful when I make mistakes. I hope to make progress in the new year.

Let's get down to the truth.

Scan the QR code encoded in gb2312 using barcode snippets. garbled characters are displayed in the scan results and cannot be displayed normally. This problem occurs. After tracking and debugging, I found that the original bytes was parsed IN THE decodedbitstreamparser class. Many of the Code did not understand this problem, the log shows the code running track. By scanning and analyzing the QR images of more than 20 different encoding methods, it is found that most of them are parsed by calling decodebytesegment, where the stringutils function is called. guessential coding (byte [] bytes, hashtable hints) to guess the encoding method. You can specify the encoding method in hits. If it is not specified, you can guess.

1 // For now, merely tries to distinguish ISO-8859-1, UTF-8 and Shift_JIS,
2 // which should be by far the most common encodings. ISO-8859-1
3 // should not have bytes in the 0x80 - 0x9F range, while Shift_JIS
4 // uses this as a first byte of a two-byte character. If we see this
5 // followed by a valid second byte in Shift_JIS, assume it is Shift_JIS.
6 // If we see something else in that second byte, we'll make the risky guess
7 // that it's UTF-8.

This part is the basic method to guess the encoding method in the code. Lack of guesses about gb2312. Gb2312 uses two bytes for encoding. The range of the first byte is (0xb0, 0xf7), followed by the range of the second byte (0xa0, 0xf7 ). According to this encoding rule of gb2312, you can make a simple judgment to solve the problem of scanning QR code of gb2312.

1 For (INT I = 0; I <length; I ++ ){
2 int value = bytes [I] & 0xff;
3 if (value> 0x7f) // if it is greater than 127, it may be gb2312. Then, start to judge this byte and the next byte.
4 {
5 If (value> 0xb0 & value <= 0xf7) // The first byte is within this range, then the second byte is determined.
6 {
7 int value2 = bytes [I + 1] & 0xff;
8 If (value2> 0xa0 & value2 <= 0xf7)
9 {
10 return true;
11}
12}
13}
14}

The above is a simple judgment. After being added to guesscoding, We can correctly identify the QR code of gb2312. However, it seems that this method is still relatively inefficient and is not truly integrated into the source code of zxing. Another step is required.

In the previous article about the rotation of camera, If you convert a photo to a portrait screen, in addition to specifying the direction of the activity in manifest, you also need camera. setdisplayorientation (90) to adjust the preview direction. However, the data retrieved by camera is still the orientation of the original horizontal screen. If no rotation is performed on the image before analysis, the image is saved, it will be found that it is horizontal, and those resultpoints cannot correctly mark the feature points on the QR map. I saw a post today and solved this problem. Please click here to read this post.

The most critical code is to perform the following rotation before the decode function in decodehandler. Java calls the buildluminancesource function.

1 byte[] rotatedData = new byte[data.length];
2 for (int y = 0; y < height; y++) {
3 for (int x = 0; x < width; x++)
4 rotatedData[x * height + height - y - 1] = data[x + y * width];
5 }

This solves the problem of incorrect resultpoint marking. Of course, in addition to setting the direction and rotating the data, you also need to adjust the layout of the view to make the scan box look more harmonious.

Today is the last day of work a year ago. The study notes on zxing are also coming up with a paragraph, which will be updated in the next year.

May the New Year be a great event.

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.