
What is QRCode Scanner in Android ?
QR Code scanner or Barcode scanner for android features are present in many apps to read some useful data. Some Time Required Some Application in QRCode Scanning To Read Data of Scan the QRCode.
In Android built-in QR code scanner And It works inside Camera app.And Focus the Camera to scan QRCode in Android.
It is Required Camera Permission to Access Camera Scan by default.It can In Built Function of camera Scanning.and Manage the Scan Size on Open the Camera Scan on your Activity.
Example of QRCode Scanner in Android
Implement Dependency in App Module build.gradle File.
1 |
implementation 'com.journeyapps:zxing-android-embedded:3.6.0' |
Create activity_main.xml File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Scan Barcode" android:id="@+id/btn_scan" android:layout_gravity="center" android:layout_marginTop="20dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:textSize="20dp" android:layout_gravity="center_horizontal" android:id="@+id/tv_result"/> </LinearLayout> |
Create MainActivity.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package com.example.bhaumik.qrcodescanner; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentResult; public class MainActivity extends AppCompatActivity { Button bt_scan; TextView tv_result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_scan = findViewById(R.id.btn_scan); tv_result = findViewById(R.id.tv_result); bt_scan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { IntentIntegrator intentIntegrator = new IntentIntegrator(MainActivity.this); intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES); intentIntegrator.setPrompt("Scan"); intentIntegrator.setCameraId(0); intentIntegrator.setBeepEnabled(true); intentIntegrator.initiateScan(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { IntentResult result = IntentIntegrator.parseActivityResult(requestCode,resultCode,data); if (result.getContents() == null){ Log.d("tag","Cancel Scan"); Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show(); }else{ Log.d("tag","Scan -> " + result.getContents()); tv_result.setText(result.getContents()); } super.onActivityResult(requestCode, resultCode, data); } } |
Above the Example of Create Simple QRCode Scanner in Android. In this Example to Declared Button And TextView. And Declared Library in Scanner Functionality Provide.
Now When you can click this Button to Give the Camera Permission And then It is Automatically Open the camera and focus on QRCode Scanning. When you can scan the code to display result in TextView.