Zxing scanned result how to paste on editText - android

Hello I want to thank you in advance for your answers. My problem is I am using Zxing to scan qr codes. And I want to paste the scan value on the my edit text at Login Activity.
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.zxing.Result;
import com.logizard.logizard_go.R;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class ScanResult extends AppCompatActivity implements ZXingScannerView.ResultHandler{
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_result);
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.)
Login.editTextUser.setText(rawResult.getText());
onBackPressed();
// If you would like to resume scanning, call this method below:
//mScannerView.resumeCameraPreview(this);
}
}
This is the sample code that I use but every time I use the button for scan nothing happens.

It looks like that you have a Login(Activity) that open the ScanResult(Activity) and you want to return the value of the scanned to Login
I suggest to you to look this doc, but also maybe you could move the handlingscan part of your code to Login activity
... implements ZXingScannerView.ResultHandler{
#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) {
editTextUser.setText(rawResult.getText());
}
}
BETTER do like that
in Login
static final int GET_SCANNED = 1;
...
private void openScanResult(){
Intent i = new Intent(this, ScanResult.class);
startActivityForResult(i, GET_SCANNED);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_SCANNED) {
if(resultCode == Activity.RESULT_OK){
String result = data.getStringExtra("scanned");
editTextUser.setText(result)
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
in ScanResult
#Override
public void handleResult(Result rawResult) {
Intent returnIntent = new Intent();
returnIntent.putExtra("scanned",rawResult.getText());
setResult(Activity.RESULT_OK,returnIntent);
finish();
}

Related

Android Studio Zxing Scanner, how to redirect to web page after qr code scan automatically (Webview)

I would like redirect to web page after QR code scan (Android Studio Zxing Scanner), after scan code when I press buttontoast, it is redirect to an URL, but I want to redirect without button press automatically alter scan.
No idea where to place the URL for redirect:
mWebView.loadUrl("https://example.com/d2.aspx?name=" + MainActivity.resulttextview.getText());
Using (Webview)
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resulttextview = findViewById(R.id.barcodetextview);
scanbutton = findViewById(R.id.buttonscan);
buttontoast = findViewById(R.id.buttontoast);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
scanbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(), ScanCodeActivity.class));
}
});
buttontoast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, resulttextview.getText(), Toast.LENGTH_SHORT).show();
mWebView.loadUrl("https://example.com/d2.aspx?name=" + MainActivity.resulttextview.getText());
}
});
ScanCodeActivity.java:
public class ScanCodeActivity extends Activity implements ZXingScannerView.ResultHandler {
int MY_PERMISSIONS_REQUEST_CAMERA=0;
ZXingScannerView scannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
}
#Override
public void handleResult(Result result) {
MainActivity.resulttextview.setText(result.getText());
onBackPressed();
}
#Override
protected void onPause() {
super.onPause();
scannerView.stopCamera();
}
#Override
protected void onPostResume() {
super.onPostResume();
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
scannerView.setResultHandler(this);
scannerView.startCamera();
}
}
enter code here
If you want to open the url directly when you scan a Qr code, add your logic in the handle result method like this:
#Override
public void handleResult(Result result) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://google.com"));
startActivity(intent)
}
This will open link in any browser installed on phone. If you want to specifically open a link in webview inside your application, call startActivityForResult in MainActivity, set result in ScanCodeActivity after you detect the URL and handle the sent result in OnActivityResult in MainActivity. I hope this might help you.

ZXingScannerView scan bulk mode in handleresult

I'm trying to implement Bulk Scan mode in me.dm7.barcodescanner:zxing:1.9 library. This is my snippet codes. Im trying to do multiple scan which from the codes for now i just trying to display each of the scan result in messagedialogue. however, after the first scan resulthandler, the second time scan automatically kill the activity.
private ZXingScannerView mScannerView;
private boolean mFlash;
private boolean mAutoFocus;
private int mCameraId = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
ViewGroup contentFrame = (ViewGroup) findViewById(R.id.content_frame);
mScannerView = new ZXingScannerView(this);
setupFormats();
contentFrame.addView(mScannerView);
}
//i want to make my scanner able to keep scanning getting the result.
//however after the first scan, the second scan will automatically close the activity
#Override
public void handleResult(Result result) {
try {
if(!result.getText().equals("")){
//In message dialogue will have 1 button handle on onDialogPositiveClick
showMessageDialog("Contents = " + result.getText() + ", Format =
" + result.getBarcodeFormat().toString());
}
} catch (Exception e) {
} finally {
}
}
public void showMessageDialog(String message) {
DialogFragment fragment = MessageDialogFragment.newInstance("Scan
Results", message, this);
fragment.show(getSupportFragmentManager(), "scan_results");
}
#Override
public void onDialogPositiveClick(DialogFragment dialog) {
mScannerView.resumeCameraPreview(this);
}
#Override
public void onPause() {
super.onPause();
mScannerView.stopCamera();
closeMessageDialog();
closeFormatsDialog();
}
#Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this);
mScannerView.startCamera(mCameraId);
mScannerView.setFlash(mFlash);
mScannerView.setAutoFocus(mAutoFocus);
}
Try it with onActivityResult
/*Here is where we come back after the Barcode Scanner is done*/
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// contents contains whatever the code was
String contents = intent.getStringExtra("SCAN_RESULT");
// Format contains the type of code i.e. UPC, EAN, QRCode etc...
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_FORMATS", "PRODUCT_MODE,CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF");
startActivityForResult(intent, 0); // start the next scan
} else if (resultCode == RESULT_CANCELED) {
//do whatever else you want.
}
}
}
you have to add handler or TimerTask for secondTime Scan.after get first scan result in handleResult you have to start scanning again after some delay, whatever delay you want add to handler.
#Override
public void handleResult(final Result rawResult) {
runOnUiThread(new Runnable() {
#Override
public void run() {
handleDecode(rawResult);
}
});
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
mScannerView.resumeCameraPreview(CaptureActivity.this);
}
}, 4000);// 4 sec delay to restart scan again.
}

Zxing - pressing back button from camera view, before reading any qr codes

This is part of the code that I've used to implement the QR code scanner,using zxing library.
Once the button is clicked, mScannerView.stopCamera() is activated, and the screen for scanning is shown. If I press phones back button from that screen, before any qrcode is read, the app completely closes and it does not go back to the previous activity. How do I go back to the previous activity(from where the startCamera() was called) when I press the back button on the phone, before reading any qr codes? Any ideas?
New Activity:
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class New extends Activity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
}
public void onClick(View v){
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
#Override
protected void onPause() {
super.onPause();
mScannerView.stopCamera();
}
#Override
public void handleResult(Result result) {
//Do anything with result here :D
Log.w("handleResult",result.getText( ));
AlertDialog.Builder builder= new AlertDialog.Builder(this);
builder.setTitle("Scan Result");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();
//Resume scanning uncomment below
//mScannerView.resumeCameraPreview(this);
}
}
Personally, I use this package. See https://github.com/journeyapps/zxing-android-embedded. Clear instruction is available for setting it up in Gradle.
In your original activity, add the followings.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null && resultCode == RESULT_OK) {
// if user scanned and the result is valid, do your stuff here
} else {
// if user pressed back or there's error, do your stuff here
}
}

app stops after scanning qr code

i wants to automatically navigate to the site after scanning qr code,hear is my code which crashes after scanning qr code.
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void QrScanner(View view){
mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
setContentView(mScannerView);
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera
}
#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.e("handler", rawResult.getText()); // Prints scan results
Log.e("handler", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode)
WebView webview = (WebView)this.findViewById(R.id.WebView);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadDataWithBaseURL("", rawResult.toString(), "text/html", "UTF-8", "");
}
}

How to prevent from scanning QR code when scanning barcode using zxing library?

I have come across this problem when scanning a barcode in my application, actually, the QR code is also present on some of the products beside barcode. so the zxing also scans QR code.
public class QRScannerActivity extends Activity implements ZXingScannerView.ResultHandler {
// begin variable listing
private ZXingScannerView mScannerView;
#Override
public void onCreate(Bundle state) {
super.onCreate(state);
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setFlash(true);
List<BarcodeFormat> myformat = new ArrayList<>();
myformat.add(BarcodeFormat.EAN_13);
myformat.add(BarcodeFormat.EAN_8);
myformat.add(BarcodeFormat.RSS_14);
myformat.add(BarcodeFormat.CODE_39);
myformat.add(BarcodeFormat.CODE_93);
myformat.add(BarcodeFormat.CODE_128);
myformat.add(BarcodeFormat.ITF);
myformat.add(BarcodeFormat.CODABAR);
myformat.add(BarcodeFormat.DATA_MATRIX);
myformat.add(BarcodeFormat.PDF_417);
mScannerView.setFormats(myformat);
// Programmatically initialize the scanner view
// 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
result=rawResult.getText();
Log.e("qr_Text1",result);
finish();
// Prints the scan format (qrcode, pdf417 etc.)
// If you would like to resume scanning, call this method below:
mScannerView.resumeCameraPreview(this);
}
}
compile 'me.dm7.barcodescanner:zxing:1.8.4'

Categories

Resources