Scan Barcode And qrcode Programmatically In Android Studio


Step 6: Opening new activity in android studio



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

Step 6: Opening new activity in android studio


To open new activity in the android studio, click on File tab which is present at the left top bar.
Now go to File->New->Activity->Empty Activity and place name of activity as ScanActivity.
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 com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class ScanActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
private ZXingScannerView mScannerView;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
mScannerView = new ZXingScannerView(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(Result rawResult) {
// Do something with the result here
// Log.v("tag", rawResult.getText()); // Prints scan results
// Log.v("tag", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)
MainActivity.tvresult.setText(rawResult.getText());
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 the 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 ©muhaz.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin