There are several ways to scan a QR code on an Android device: by downloading a special application through the Play Market or using the code reading tool built into the OS.
QR code (English: Quick Response Code; abbreviated QR code) is a matrix (two-dimensional) barcode developed by the Japanese company Denso-Wave in 1994. Initially, these codes were used in the automotive industry to quickly record information about the production and sale of cars.
Now images with black squares can be found on:
- cash register receipts;
- air and train tickets;
- plaques in museums, architectural monuments, monuments;
- In the underground;
- cafe and restaurant menus;
- on product packaging;
- utility bills.
A QR code is convenient because it allows you to encrypt several thousand characters in one picture. In this case, the image itself for scanning can be either a small centimeter square or a rather large picture placed on a flat surface.
Currently, QR codes are widely used in Asia, Europe and North America. They appeared in Russia relatively recently, but are actively used in the areas of goods and services.
Ways to scan the code
In order to pay bills and receive reliable information about certain cultural objects, you do not need to carry a special barcode scanner with you. In fact, you need a minimum of funds: a smartphone with a main or front camera and software that can display the data encrypted in the picture.
Programs
The most popular applications for reading QR codes:
- "QR and barcode scanner" from Gamma Play.
- "QRcode" from YUNteam.
- "QR Code Scanner" by InShot Inc.
Working with mobile scanners is very simple:
- Download the application to your smartphone.
- Open the program.
- Point the camera at the QR code.
- We receive information about the product or copy the web link to go to the site.
Most applications allow you to customize the scanning process and also allow you to view the history of previously scanned codes.
What you need to scan a QR code on Android OS
Matrix barcodes have become a part of our lives. Initially, coding was used in the automotive industry, but then migrated to other industries. Currently QR codes are used to mark:
- railway and air tickets;
- cash receipts;
- landmarks signs;
- restaurant menus;
- product packaging;
- utility bills.
To get the required information, you will need a modern smartphone equipped with a camera and simple software. Even if you use the front camera on your device, you will achieve what you want. Applications released by Gamma Play, InShot Inc and YUNteam will provide invaluable assistance.
Built-in applications for Android
Some smartphone manufacturers that develop their own Android shells implement tools for reading QR codes. They do not need to be installed through the application store - the programs are pre-installed on the system and take up virtually no space on the phone.
Xiaomi
Xiaomi smartphones have the “Scanner” application, which by default is located in the “Tools” folder or in the additional menu to the left of the main screen.
All you need to do is run the tool and place the code inside the scanning frame. If the code is correct and the camera has focused well on the image, then the display should display all the information encrypted in the image, which you can use at your discretion.
A special feature of the Xiaomi scanner is the ability to scan any photo from the Gallery for a QR code.
Huawei
Huawei smartphones have a very convenient feature that displays several tools on the locked screen for quick opening. A QR code scanner is one of them.
To open the scanner from a locked device, you need to:
- Swipe up from the bottom edge of the locked screen, causing the tool menu to appear at the bottom.
- Select the scanner icon from the list.
Next, we point the camera at the QR code, wait for the tool to decipher the image and look at the result. However, to get complete information you need to unlock your smartphone.
On an unlocked phone, you can find the scanner in the quick search menu for applications. To do this, on the main screen, swipe down from the center of the screen and click on the scanner icon located in the search bar.
The EMUI shell also has the ability to scan photos with a QR code. To use it, you need to go to the Gallery, select a photo that has a barcode, click on the “More” button and select “Scan QR code” in the menu that appears.
Samsung
On Samsung devices, the scanner is a function built into the standard camera, and in order for the code to be read, you just need to point the lens at it. But if automatic recognition does not work, you need to activate a special mode. For this:
- Go to the Camera application.
- Let's go to additional settings.
- Select scanner mode.
This way you can get information from the QR code on all modern Samsung models.
We generate and scan QR/BAR codes
The article provides a short example of how to integrate a generator and/or scanner of QR codes (or barcodes) into your application, and thereby facilitate the task of transferring short amounts of information from device to device. QR codes have replaced outdated barcodes (hereinafter instead of 'Bar code') and are increasingly becoming part of our lives; they are used in dozens of different solutions, from transmitting links to a website to complex authorization and purchase systems. You can find out in detail what a QR code is in the article Reading a QR code
To complete the task we will need 2 libraries from two projects:
- ZBar code reader
- ZXing (“Zebra Crossing”)
Scanning QR codes
Libraries from ZBar bar code reader
, so here we go:
- Add zbar.jar to the project
- Add libiconv.so and libzbarjni.so libraries to the native project, which are responsible for analyzing and recognizing images from a camera in real time.
- Load native libraries into memory static { System.loadLibrary("iconv"); }
- Initialize the scanner scanner = new ImageScanner(); scanner.setConfig(0, Config.X_DENSITY, 3); //why are these parameters not specified anywhere scanner.setConfig(0, Config.Y_DENSITY, 3);
- Next, we transfer to the scanner each new frame from the camera preview PreviewCallback previewCb = new PreviewCallback() { public void onPreviewFrame(byte[] data, Camera camera) { String lastScannedCode; codeImage.setData(data); int result = scanner.scanImage(codeImage); if (result != 0) { SymbolSet syms = scanner.getResults(); for (Symbol sym : syms) { lastScannedCode = sym.getData(); } } } }
- As a result, in lastScannedCode
we receive a recognized code.There is one peculiarity here, result = scanner.scanImage(codeImage)
sometimes returns the correct result, even when there is no QR code in front of the camera. That is, the camera sometimes recognizes something even in an ordinary blurry picture. Therefore, I recommend introducing an additional check for the size of the code read or for compliance with the expected format.
Generating QR codes
In this case, the resources of the ZXing
.
Input parameters encodeAsBitmap
: text or code for encoding, the standard we are encoding into, the size of the output image. Bitmap barcode_bitmap = encodeAsBitmap(text, BarcodeFormat.QR_CODE, 200, 200); targetImageView.setImageBitmap(barcode_bitmap); private static Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) throws WriterException { String contentsToEncode = contents; if (contentsToEncode == NULL) { return NULL; } Map hints = NULL; String encoding = guessAppropriateEncoding(contentsToEncode); if (encoding != NULL) { hints = new EnumMap(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, encoding); } MultiFormatWriter writer = new MultiFormatWriter(); BitMatrix result; try { result = writer.encode(contentsToEncode, format, img_width, img_height, hints); } catch (IllegalArgumentException iae) { // Unsupported format return NULL; } int width = result.getWidth(); int height = result.getHeight(); int[] pixels = new int; for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels = result.get(x, y) ? BLACK: WHITE; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
What about Barcodes?
The scanner understands all types of barcodes without any modifications, out of the box
.
The generator is modified not just, but very simply: into the encodeAsBitmap
we pass in the
format
instead of BarcodeFormat.QR_CODE, something like BarcodeFormat.CODE_128, which will correspond to the barcode of the Code 128 standard
A couple of final tips
Keep in mind that working with the camera may have its own characteristics on different platforms
It is noticed that the method public void onPreviewFrame(byte[] data, Camera camera) { codeImage.setData(data); …. } constantly loses memory (there is Memory Leak) due to the fact that the frame buffer is constantly created and cleared on each new preview frame from the camera. In order to avoid this, it is possible to use CallbackBuffer to allocate a static buffer for preview frames. This really helps get rid of memory leaks and even increases the frame rate of the preview image from the camera. But!
, there was a phone model that didn’t want to work with the preview buffer in any way, and it’s not a fact that they won’t be found yet, so I left a more reliable method in the example.
Barcode generation has limitations according to the selected standard: maximum size in bytes, allowed symbols, etc. Study the features of linear barcodes to ensure compatibility of the barcodes you display with store scanners
For those who are going to use .so libraries in projects using the Gradle build system, the steps are as follows: create a jar file iconv.jar with the following structure:
- lib/
- lib/x86
- lib/armeabi
- lib/armeabi-v7a
and add to its dependencies
section compile files('libs/iconv.jar') Or, a more universal option, so as not to describe all JAR files separately: dependencies { compile fileTree(dir: 'lib', include: '*. jar') }
UPD. Uploaded the source code to GitHub What came out of the project (Generator and Scanner in one bottle):
Create your own QR code
To create your own QR code, you can use various tools or online generators. Most popular services:
- https://qrcoder.ru.
- https://ru.qr-code-generator.com.
- https://goqr.me.
- Application "AndQR - Creating QR codes".
- Application "QR code generator".
First you need to choose what function the generated code should perform and enter the information that needs to be encrypted. Next, the code is customized - color design and logo.
Each service has its own settings and limitations, which you should familiarize yourself with before starting work.
What to do if it doesn’t scan on Android?
If everything is done, but the scanner does not work, try the following:
- Check on another object, the stroke may be damaged.
- If the previous point also does not work, change the application or update it (if there is an update package).
- Have all the above measures been taken and still not working? Check the camera for damage, dust, etc.
- Update Android.
Sometimes the reason lies in some little things, perhaps one of the installed applications crashes and causes a conflict. In the case of a large number of applications, it is difficult to find out which one is failing. In such a situation, it is easier to save all the data to an external drive and reset the device to factory settings. Many other questions about what a QR code is are described in detail in my other article, so welcome to read.
What to do if the code is not scanned
If the smartphone does not recognize the QR code and does not want to display the information encrypted in it, you need to:
- Try scanning the code under different lighting, adjusting the focus.
- Restart the reading application or completely reinstall it.
- Reboot your smartphone.
It is also worth noting that the programs do not read damaged or poorly printed QR codes.
QR codes are gradually being introduced into people’s daily lives, significantly improving and simplifying life. This is exactly why Denso-Wave invented them - for simplicity and convenience in obtaining the necessary information.
Helpful 9
↑ What applications read the rq code?
The scanning method depends largely on the smartphone model.
For example, on Samsung there is no need to download an application, since the scanner is already built into the firmware. If you have this year's flagship, then look for a scanner in your phone. If you haven’t found it, go to the play market - more than 50 programs that read barcodes are waiting for you. The principle of operation is almost the same for all. The most popular ones include:
- Lightning qr scanner
— pros: fast speed, Russian interface, simple controls, no advertising;from Play Market
- QR barcode scanner
– allows you not only to read codes, but also to create them yourself;from Play Market
- QR Code & Scanner
- in addition to reading ability, it can “hide” the current geolocation, contact information and more.from Play Market
How QR and barcode scanner apps work
To understand how scanners receive information, you need to understand what a barcode and a QR code represent.
Barcode
A barcode is an alternation of black and white stripes of different thicknesses. Sometimes a black and white sequence of other figures is used. In the linear barcode we are used to, a combination of 4 stripes of varying thickness is encoded into numbers from 0 to 9. There are many types of barcodes, including complex ones, into which you can enter not only numbers, but also the entire English alphabet with some special characters.
The scanner program for Android uses a camera to recognize the barcode on the area of the image that falls within the scanning area. Then it draws a horizontal line along it and extracts the encoded characters. The result obtained is displayed on the display. After which the application sends a request in the form of a combination of numbers to the database, where it finds the desired product.
QR code
Unlike a linear barcode, where only the horizontal bar is the significant coding area, a QR code is a two-dimensional matrix. Visually, it is a square filled with black and white areas, various combinations of which are assembled into meaningful modules. Accordingly, information is recorded along the entire perimeter of the image.
Large squares at the corners of the image are needed for orientation. A simple example: draw a square on a piece of paper, place dots around its perimeter, rotate the drawing and ask another person to determine the original position of the square. This will prove to be very difficult. In this way, the original code image can also be rotated. Marking the squares allows you to determine the correct location of the graphic design.
The QR code scanner for Android recognizes the boundaries of an image, breaks it into modules, and then matches the image with a database. And then, the application operates depending on what was encrypted in the code, as well as on the functionality of the program.