- Scanning the QR code can be done programmatically by using many ways:
- Using a web-based API solution, where the QR code or barcode is uploaded to the server, and the server returns the results.
- Using a web-based application that accesses your camera and scans the QR code or barcode and returns the results.
- By Integrating the Mobile Vision API of Google Play Service.
Following are the major formats that the Vision API supports.
> 1 Dimension barcodes: EAN-13, EAN-8, UPC-A, UPC-E, Code-39, Code-93, Code-128, ITF, Codabar.
>> 2 Dimension barcodes: QR Code, Data Matrix, PDF-417, AZTEC.
Android QR Code Scanner Example :-
In this example, we will scan the QR code of a web URL and Email address, and act on it. Here we will use Google Play Mobile Vision API to scan the QR code.
<?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:gravity="center" android:layout_height="match_parent"> <Button android:id="@+id/btnScanBarcode" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="44dp" android:text="Scan Barcode The Code Hub Demo" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.676" app:layout_constraintStart_toStartOf="parent" /> </LinearLayout>
Create an activity_barcode__scane_.xml layout and add the following code. The SurfaceView widget is used for camera source.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <SurfaceView android:id="@+id/surfaceView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/btnAction" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_centerVertical="true" /> <TextView android:gravity="center" android:id="@+id/txtBarcodeValue" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:text="No Barcode Detected" android:textColor="@android:color/white" android:textSize="20sp" /> <Button android:id="@+id/btnAction" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="ADD CONTENT IN THE MAIL" /> </RelativeLayout>
Create an activity_email_.xml layout to perform email action.
activity_email_.xml
<?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:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/txtEmailAddress" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:text="Email Address: " android:textSize="16dp" android:textStyle="bold" /> <EditText android:id="@+id/inSubject" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/txtEmailAddress" android:layout_centerHorizontal="true" android:ems="10" android:hint="Subject" /> <EditText android:id="@+id/inBody" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/inSubject" android:layout_centerHorizontal="true" android:ems="10" android:hint="Body" /> <Button android:id="@+id/btnSendEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/inBody" android:layout_centerHorizontal="true" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="64dp" android:gravity="center" android:text="Send Email" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </LinearLayout>
build.gradle To Add This dependency implementation ‘com.google.android.gms:play-services-vision:20.1.3’
MainActivity.java
package com.thecodehub.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button btnScanBarcode; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnScanBarcode=findViewById(R.id.btnScanBarcode); btnScanBarcode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent =new Intent(getApplicationContext(),Barcode_Scane_Activity.class); startActivity(intent); } }); } }Barcode_Scane_Activity.Java
package com.thecodehub.myapplication; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.util.SparseArray; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.vision.CameraSource; import com.google.android.gms.vision.Detector; import com.google.android.gms.vision.barcode.Barcode; import com.google.android.gms.vision.barcode.BarcodeDetector; import java.io.IOException; public class Barcode_Scane_Activity extends AppCompatActivity { SurfaceView surfaceView; TextView txtBarcodeValue; private BarcodeDetector barcodeDetector; private CameraSource cameraSource; private static final int REQUEST_CAMERA_PERMISSION = 201; Button btnAction; String intentData = ""; boolean isEmail = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_barcode__scane_); initViews(); } private void initViews() { txtBarcodeValue = findViewById(R.id.txtBarcodeValue); surfaceView = findViewById(R.id.surfaceView); btnAction = findViewById(R.id.btnAction); btnAction.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (intentData.length() > 0) { if (isEmail) startActivity(new Intent(getApplicationContext(), Email_Activity.class).putExtra("email_address", intentData)); else { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(intentData))); } } } }); } private void initialiseDetectorsAndSources() { Toast.makeText(getApplicationContext(), "Barcode scanner started", Toast.LENGTH_SHORT).show(); barcodeDetector = new BarcodeDetector.Builder(this) .setBarcodeFormats(Barcode.ALL_FORMATS) .build(); cameraSource = new CameraSource.Builder(this, barcodeDetector) .setRequestedPreviewSize(1920, 1080) .setAutoFocusEnabled(true) //you should add this feature .build(); surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { if (ActivityCompat.checkSelfPermission(Barcode_Scane_Activity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { try { cameraSource.start(surfaceView.getHolder()); } catch (IOException e) { e.printStackTrace(); } } else { ActivityCompat.requestPermissions(Barcode_Scane_Activity.this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { cameraSource.stop(); } }); barcodeDetector.setProcessor(new Detector.Processor<Barcode>() { @Override public void release() { Toast.makeText(getApplicationContext(), "To prevent memory leaks barcode scanner has been stopped", Toast.LENGTH_SHORT).show(); } @Override public void receiveDetections(Detector.Detections<Barcode> detections) { final SparseArray<Barcode> barcodes = detections.getDetectedItems(); if (barcodes.size() != 0) { txtBarcodeValue.post(new Runnable() { @Override public void run() { if (barcodes.valueAt(0).email != null) { txtBarcodeValue.removeCallbacks(null); intentData = barcodes.valueAt(0).email.address; txtBarcodeValue.setText(intentData); isEmail = true; btnAction.setText("ADD CONTENT TO THE MAIL"); } else { isEmail = false; btnAction.setText("LAUNCH URL"); intentData = barcodes.valueAt(0).displayValue; txtBarcodeValue.setText(intentData); } } }); } } }); } @Override protected void onPause() { super.onPause(); cameraSource.release(); } @Override protected void onResume() { super.onResume(); initialiseDetectorsAndSources(); } }This class performs the email sending task to the address mention in the QR code.
EmailActivity.java
package com.thecodehub.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Email_Activity extends AppCompatActivity implements View.OnClickListener{ EditText inSubject, inBody; TextView txtEmailAddress; Button btnSendEmail; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_email_); initViews(); } private void initViews() { inSubject = findViewById(R.id.inSubject); inBody = findViewById(R.id.inBody); txtEmailAddress = findViewById(R.id.txtEmailAddress); btnSendEmail = findViewById(R.id.btnSendEmail); if (getIntent().getStringExtra("email_address") != null) { txtEmailAddress.setText("Recipient : " + getIntent().getStringExtra("email_address")); } btnSendEmail.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_EMAIL, new String[]{txtEmailAddress.getText().toString()}); intent.putExtra(Intent.EXTRA_SUBJECT, inSubject.getText().toString().trim()); intent.putExtra(Intent.EXTRA_TEXT, inBody.getText().toString().trim()); startActivity(Intent.createChooser(intent, "Send Email")); } }); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnScanBarcode: startActivity(new Intent(Email_Activity.this, Barcode_Scane_Activity.class)); break; } } }
AndroidMenifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.thecodehub.myapplication"> <uses-feature android:name="android.hardware.camera" android:required="true" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.CAMERA" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyApplication"> <activity android:name=".Email_Activity"></activity> <activity android:name=".Barcode_Scane_Activity" /> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Code Resource:-
Now you can run your project.
Congratulations!!! you have developed your Bar Code Scanner In Android Example Integration. in Android Studio and now just keep following the rest of the tutorial step by step to become a great Android Developer. All the very best.
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular