Scan Barcode And qrcode Programmatically In Android Studio


Step 6: Opening new activity in android studio



Yüklə 139,41 Kb.
səhifə3/7
tarix15.04.2023
ölçüsü139,41 Kb.
#98565
1   2   3   4   5   6   7

Step 6: Opening new activity in android studio


To open a new activity in android studio, click on File tab which is present at the left top bar.
Now go to File->New->Activity->Empty Activity and give name of activity as ScanActivity.
You can use below image for more reference.
open new activity

Step 7: Updating ScanActivity.java file:


Add below code to ScanActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import me.dm7.barcodescanner.zbar.ZBarScannerView;
public class ScanActivity extends AppCompatActivity implements ZBarScannerView.ResultHandler {
private ZBarScannerView mScannerView;
//camera permission is needed.
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
mScannerView = new ZBarScannerView(this); // Programmatically initialize the scanner view
setContentView(mScannerView); // Set the scanner view as the content view
}
@Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
@Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
@Override
public void handleResult(me.dm7.barcodescanner.zbar.Result result) {
// Do something with the result here
Log.v("kkkk", result.getContents()); // Prints scan results
Log.v("uuuu", result.getBarcodeFormat().getName()); // Prints the scan format (qrcode, pdf417 etc.)
MainActivity.tvresult.setText(result.getContents());
onBackPressed();
// If you would like to resume scanning, call this method below:
//mScannerView.resumeCameraPreview(this);
}
}
Note : We do not need to update activity_scan.xml file because we will open camera preview here so no need to do anything in layout file.

Yüklə 139,41 Kb.

Dostları ilə paylaş:
1   2   3   4   5   6   7




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin