Android में ZXing लाइब्रेरी का उपयोग करके बारकोड/QR कोड स्कैन करना
यह उदाहरण दर्शाता है कि ZXing (Zebra Crossing) लाइब्रेरी का उपयोग करके बारकोड और QR कोड स्कैनिंग कैसे इम्प्लीमेंट करें। यह सबसे लोकप्रिय और विश्वसनीय ओपन-सोर्स बारकोड स्कैनिंग लाइब्रेरी है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें और प्रोजेक्ट बनाएं।
चरण 2: लेआउट में बटन और रिजल्ट TextView
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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="स्कैन करें"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/txtContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button"
android:layout_centerInParent="true"
android:layout_marginBottom="10dp"
android:text="कोड रीडर"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 3: Gradle में निर्भरताएं जोड़ें
build.gradle (Module: app) में:
dependencies {
implementation 'com.google.zxing:core:3.4.1'
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
}
नोट: नवीनतम संस्करण उपयोग करें। Sync Now पर क्लिक करें।
चरण 4: MainActivity.java - स्कैनिंग लॉजिक
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("स्कैन करें");
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, "रद्द किया गया", Toast.LENGTH_SHORT).show();
} else {
Log.d("MainActivity", "Scanned: " + result.getContents());
Toast.makeText(this, "स्कैन हुआ: " + result.getContents(), Toast.LENGTH_SHORT).show();
textView.setText("स्कैन परिणाम: " + result.getContents());
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
चरण 5: 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>
समर्थित बारकोड फॉर्मेट
- 1D: UPC-A, UPC-E, EAN-8, EAN-13, Code 39, Code 93, Code 128, ITF, Codabar
- 2D: QR Code, Data Matrix, PDF 417, Aztec
उन्नत विकल्प
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES); // केवल QR
integrator.setPrompt("QR कोड स्कैन करें");
integrator.setCameraId(0); // बैक कैमरा
integrator.setBeepEnabled(true); // बीप साउंड
integrator.setBarcodeImageEnabled(true); // स्कैन की गई इमेज सेव करें
integrator.setOrientationLocked(false); // ओरिएंटेशन लॉक
integrator.initiateScan();
ऐप चलाएं
डिवाइस कनेक्ट करें (कैमरा आवश्यक), Run पर क्लिक करें। "स्कैन करें" बटन दबाने पर कैमरा खुलेगा। बारकोड/QR कोड पर फोकस करें - परिणाम TextView में दिखेगा।