Scanning QR code and bar code with Google zxing in Android

Scanning QR code and bar code with Google zxing in Android

I learned about QR code from wechat. At that time, wechat launched QR code scanning function. I felt it was very novel. Scanning a picture could directly add friends. It was incredible. At that time, I didn't know QR code. Hehe, when I was working on a project, the boss said to add QR code scanning function, and then I went to Baidu, Google or something. I found that many friends have the function of introducing QR code scanning. Then I started the QR code scanning function with the introduction of others. With the posts of others, I soon added the function of scanning QR code to my project, and then I was very happy.

With the advent of wechat, QR codes are becoming more and more popular. QR codes can be seen everywhere, such as shopping malls, KFC, restaurants, etc. for QR code scanning, we use Google's open source framework zxing, which we can go to http://code.google.com/p/zxing/ Download the source code and jar package. Previously, the QR code scanning function in my project only realized the scanning function, Its UI is really ugly. For a good application software, its UI interface should also be accepted by the public, otherwise people will not use your software. Therefore, the function and interface of the application software are as important as those of wechat. For example, I believe that wechat UI is imitated by many application software, and I also imitate the effect of wechat scanning QR code, although it is not as exquisite as wechat, But the effect is still OK, so I share the code of modifying UI and scanning QR code with you. First, I will directly copy the same function in my future projects. Second, I will give a reference to people who have not added QR code function and stand on the shoulders of giants. Ha ha, I also added this function on the shoulders of giants before, Next, follow me step by step to realize this function, which removes many unnecessary files

Let's look at the structure of the project first

If you also want to add this function to your project, you can directly copy the three packages com.mining.app.zxing.camera, com.mining.app.zxing.coding and com.mining.app.zxing.view into your project, and then introduce the corresponding resources. I also directly referenced them from my project. The package name has not been changed. Of course, you need to reference zxing.jar

com.example.qr_ There is a mipcaactivitycapture in the codescan package, which is also directly introduced into the code of my previous project. This activity mainly deals with the classes of the scanning interface, such as sound and vibration after successful scanning. It mainly focuses on the handledecode (result, bitmap barcode) method, After the scanning is completed, transfer the scanned results and the bitmap parameters of the QR code to the handledecode (result result, bitmap barcode). We only need to write the corresponding processing code in it. There is no need to change other places. I process the scanning results and scanned photos here

I made my own changes to the layout of the mipcaactivitycapture interface. First, let's take a look at the rendering. It mainly uses FrameLayout, which is nested with relativelayout, and the pictures in it are also taken from wechat. Usually, I go to wechat to find any pictures I need. Programmers of companies without art works are forced

The layout code is as follows

In it, I write the upper part of the interface in another layout, and then include it because of this activity_ The title is also used by other activities in my project, and I copy it directly

In my demo, there is a main interface mainactivity with a button, an ImageView and a textview. Click the button to enter the QR code scanning interface. When the scanning is OK, return to the main interface, display the scanning results in textview and the pictures in ImageView. Then you can not process the pictures. I add the pictures here, The layout of the main interface is as follows

The code in mainactivity is as follows, and the functions have been described above

The above code is still relatively simple, but if we want to make a scanning frame like wechat, the above code will not have that effect. We must rewrite the viewfinderview class under the com.mining.app.zxing.view package. All the pictures in wechat are used. I drew them myself. The code comments are relatively clear. Let's look at the code directly, I believe you can understand. If you want to modify the size of the scan box, go to the cameramanager class to modify it

In the above code, the line in the middle of wechat is a picture. I drew it here. If you want to simulate more, you can use the following code

Change to

Go to wechat to find the scanning line. The one I posted is distorted. Download wechat APK, change the suffix to zip, and then unzip it

The code for drawing the font below the scan box needs to be modified so that the font can be automatically arranged in the middle according to the font. If the word is too long and I don't handle it, it needs to wrap automatically. You can handle it yourself

Screenshot of the operation interface. The green line in the middle will move up and down, which is similar to the effect of wechat. Of course, you also need the corresponding permission to run. Friends who are interested can download the demo

Since I wrote this blog more than 8 o'clock, it looks like Braille, but it's actually time-consuming. If you think this article is helpful to you, you can say, ha ha, take a bath and go to bed. I haven't posted some resource files in the above project. If you want to see the effect, you can download the source code

I scanned the QR code in the mobile phone based on Google zxing on Android. In this article, I scanned the QR code photos in the mobile phone and replaced the middle scanning line, which is more similar to the effect of wechat. I suggest you go to the project source code of that article

Project source code, click download: http://xiazai.jb51.net/201705/yuanma/QR_ CodeScan(jb51.net).rar

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>