এই উদাহরণটি দেখায় কিভাবে অ্যান্ড্রয়েডে বারকোড স্ক্যানিং ব্যবহার করা যায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project এ যান এবং প্রজেক্ট তৈরি করার জন্য সকল প্রয়োজনীয় তথ্য পূরণ করুন।
ধাপ ২: লেআউট XML ফাইল যোগ করুন
res/layout/activity_main.xml ফাইলে নিচের কোডটি যোগ করুন।
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
tools:context=".MainActivity">
<TextView
android:layout_below="@id/button"
android:layout_centerInParent="true"
android:layout_marginBottom="10dp"
android:text=" code reader"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtContent"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Process"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:id="@+id/button" />
</RelativeLayout>
ধাপ ৩: Gradle-এ ডিপেন্ডেন্সি যোগ করুন
build.gradle ফাইলে নিচের ডিপেন্ডেন্সিগুলো যোগ করুন। ZXing লাইব্রেরি ব্যবহার করা হয়েছে।
implementation 'com.google.zxing:core:3.2.1' implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
ধাপ ৪: MainActivity.java ফাইলে কোড যোগ করুন
src/MainActivity.java ফাইলে নিচের কোডটি যোগ করুন। IntentIntegrator ব্যবহার করে বারকোড স্ক্যান শুরু করা হয়েছে।
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
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 btnBarcode;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnBarcode = findViewById(R.id.button);
textView = findViewById(R.id.txtContent);
btnBarcode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator intentIntegrator = new IntentIntegrator(MainActivity.this);
intentIntegrator.setDesiredBarcodeFormats(intentIntegrator.ALL_CODE_TYPES);
intentIntegrator.setBeepEnabled(false);
intentIntegrator.setCameraId(0);
intentIntegrator.setPrompt("SCAN");
intentIntegrator.setBarcodeImageEnabled(false);
intentIntegrator.initiateScan();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult Result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (Result != null) {
if (Result.getContents() == null) {
Toast.makeText(this, "cancelled", Toast.LENGTH_SHORT).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned -> " + Result.getContents(), Toast.LENGTH_SHORT).show();
textView.setText(String.format("Scanned Result: %s", Result));
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
ধাপ ৫: AndroidManifest.xml-এ ক্যামেরা অনুমতি এবং ফিচার যোগ করুন
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-feature android:name="android.hardware.camera.autoFocus" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
অ্যাপ্লিকেশন চালান
আপনার অ্যান্ড্রয়েড মোবাইল ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে ধরে নেওয়া হয়েছে। Android Studio-তে আপনার প্রজেক্টের যেকোনো এক্টিভিটি ফাইল খুলুন এবং টুলবার থেকে Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস সিলেক্ট করুন এবং ডিভাইসে ডিফল্ট স্ক্রিন দেখুন। বাস্তব ডিভাইস ব্যবহার করে ভালো ফলাফল পাওয়া যাবে।