How can I program a QR reader?

Scan pixaQR codes

At Daniel's request, we will deal with the processing of QR codes today, how you can read them in on Android and have a simple string returned.
QR codes are these square fields, consisting of smaller white or black fields that are more often found on posters or the like. They contain information such as: B. texts, Internet addresses, contact data or the like and can now be scanned by the vast majority of devices.
Perhaps it would first be interesting to clarify how a QR code is structured, how the data comes into a QR code and how it can be read out by a program.

Structure of QR code:

The idea behind a QR code is actually quite simple and yet very interesting.
Most of the time we only get a black and white field which we can then scan, but how is such a QR code structured?

Basically, I would differentiate between 3 categories of data.

  1. Help information (red, green)
  2. Processing information (yellow, blue)
  3. Coded data (unmarked)

We'll make these 3 types clearer with an example.

The red marked points provide information about the alignment. A QR code scanner can use these fields to recognize how the QR code is turned. So we can turn it as we like and the scanner recognizes this.
The green marked areas help the scanner to recognize how big the individual fields are.
Within the blue areas it is coded with which version the QR code was created, whereas the yellow fields indicate the pattern according to which the correct data was coded.
The unmarked places themselves define the coded text.

QR code functionality:

Now that we know how such a QR code is structured, we can deal with the theory of creation.

We can - if we represent it in binary form - represent a text or numbers in 1's and 0's.
To correct errors, we add a few additional bits at the end of this bit sequence.
We can also display these 1's and 0's alternatively as white and black. So we took the first step towards our QR code.
In the next step, we select one of the general patterns and pack this into the QR code with the version of the QR code (blue), the auxiliary lines (green) and the position (red). We also add which pattern has been selected to the QR code in the places marked in yellow.

The real work now consists of overlaying the pattern over our binary coded text and comparing it.
If the pattern and our text have the same color in the same places, then we add a white field to the QR code.
If the pattern and our text have a different color in the same places, then we add a black field to the QR code.

Incidentally, the pattern is not chosen at random, but in such a way that the number of white and black fields is as equal as possible.

On the next page I will explain how you can read out the QR code and then display the content.

Read out the QR code:

Preliminary work:

Before we can start programming we have to add a library and define a button. The library does all the work for us and we can take care of the data processing in a relaxed manner.

We open the file “build.gradle (Module: app)” and insert there in the brackets {} of the tag “dependencies”:

compile 'com.journeyapps: zxing-android-embedded: [email protected]' compile ' core: 3.2.0'

Now we open the file activity_main.xml and add under “textview”:


As already mentioned, programming is pretty easy for us.
We only have to react to the button we just created and then receive the data from the scanner:

@Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); Button button = (Button) findViewById (; button.setOnClickListener (new View.OnClickListener () {@Override public void onClick (View view) {IntentIntegrator scanIntegrator = new IntentIntegrator (MainActivity.this); scanIntegrator.setPrompt ("Scan a Barcode"); scanIntegrator.setDesired_BarcodeFormEats ); scanIntegrator.setBeepEnabled (true); scanIntegrator.setOrientationLocked (true); scanIntegrator.setBarcodeImageEnabled (true); scanIntegrator.initiateScan ();}}); } public void onActivityResult (int requestCode, int resultCode, Intent intent) {IntentResult scanningResult = IntentIntegrator.parseActivityResult (requestCode, resultCode, intent); if (scanningResult! = null) {String scanContent = scanningResult.getContents (); Toast.makeText (this, scanContent, Toast.LENGTH_SHORT) .show (); } else {Toast.makeText (this, "No data received!", Toast.LENGTH_SHORT) .show (); }}

Try the QR code in the article picture or on the previous page.
Also pay attention to the licenses of the library used.


I am a person who, besides programming, is interested in a thousand other things that are sometimes more and sometimes less crazy. Above all, however, I am enthusiastic about programming my own little apps and programs that enrich my life.