I'm working on a small Android project .. I have the Xzing barcode scanners linked and everything worked. now I need again a scann-button. But when I scan with the second button, it writes me the result in the same field as the first scann-button.can someone help me?
package de.example.addmeter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); //Fullscreen
setContentView(R.layout.add_strom);
}
public void onClick1 (View view) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
String meterid;
meterid = scanResult.getContents();
EditText etmeterid = (EditText) findViewById(R.id.etmeterid);
etmeterid.setText(meterid);
}
}
public void onClick2 (View view) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
}
public void onActivityResult1(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
String security;
security = scanResult.getContents();
EditText etsecurity = (EditText) findViewById(R.id.etsecurity);
etsecurity.setText(security);
}
}
That onActivityResult1 function is never called. When the Xzing intent returns, you have to manage all the result code in onActivityResult (that, by the way, should be marked as #Override).
Normally, what you want should be managed whith different requestCode, but seems that Xzing intent helper does not allow it.
So, in your case, I would make something like setting a global boolean variable wasCalledFromButton1, giving it a value in the corresponding onClick method, an then give the returned value to the correct EditText in onActivityResult according to this variable.
Related
I want to put a barcode reader in my android application, I want it to write the barcode it reads to the TextView, but when I read the barcode, it does not print. The code was working before making the Fragment of the page, it was writing automatically, but when I made the Fragment, it did not transfer what it read to the TextView after reading it.
Code :
public class UrunEklemeJavaFragment extends Fragment {
...
scanBtns.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IntentIntegrator intentIntegrator = new IntentIntegrator(getActivity());
intentIntegrator.setPrompt("Scan");
intentIntegrator.setBeepEnabled(true);
intentIntegrator.setOrientationLocked(true);
intentIntegrator.setCaptureActivity(BarcodeCapture.class);
intentIntegrator.initiateScan();
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
// if the intentResult is null then
// toast a message as "cancelled"
if (intentResult != null) {
if (intentResult.getContents() == null) {
Toast.makeText(
getActivity(),
"Cancel",
Toast.LENGTH_SHORT
).show();
}
else {
// if the intentResult is not null we'll set
// the content and format of scan message
messageText.setText(intentResult.getContents());
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
my main purpose is to write the barcode read in the "messageText" section, but I can't, it reads the barcode but does not write the barcode in the section I want.
scanBtns Code SS
onActivityResult Code SS
I am developping an application which convert scanned data (barcode) to GoogleSheet data, and I am trying to transfer the barcode number (from Page2.java) into another java file (ListItem.java)
I saw that a usual way to do it is to create intents. So I did it.
But the toast I put in ListItem.java gives me "null" instead of the scanned number (for example 0123456789012)
Please, can you tell me where am I wrong ? Thank you so much !
1st Code (Page2.java , where I get "scanContent2", the variable I need) :
public class Page2 extends Activity implements OnClickListener {
#SuppressLint("ClickableViewAccessibility")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page2);
scanBtn2 = (Button) findViewById(R.id.scan_button2);
scanBtn2.setOnClickListener(this);
}
public Button scanBtn2;
public String scanContent2;
#Override
public void onClick(View v) {
if (v.getId() == R.id.scan_button2) {
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
scanContent2 = scanningResult.getContents();
Intent intenta = new Intent(getApplicationContext(),ListItem.class);
intenta.putExtra("theScanContent2", scanContent2);
startActivity(intenta);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
}
2nd code (ListItem.java, where I get "null" on the toast) :
public class ListItem extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
String scanContent2 = getIntent().getStringExtra("theScanContent");
Toast toast = Toast.makeText(getApplicationContext(),
"BarCode number: " + scanContent2, Toast.LENGTH_SHORT);
toast.show();
}
}
In the Listitem.java in the following line,
String scanContent2 = getIntent().getStringExtra("theScanContent");
You are trying to get String with key theScanContent, while you are putting scanContent2 with key theScanContent2 in Page2.java in line
intent.putExtra("theScanContent2", scanContent2);
Make sure the keys are same for the Intent while putting the data in intent and accessing the data from the intent to avoid getting null Results.
So I have the zxing barcode scanner running and in my main activity I have the onResultActivity function telling my activity to push to a new activity with a result from the scanner.
The problem is that my scanner just scans any old QR code regardless of what it is.
I need the scanner to only accept my QR code to pass a successful result and ignore all other QR codes (this should pass a toaster to say "incorrect QR code, try again").
Here's what I currently have:
MainActivity
...
static final int SCAN_RESULT = 1; // The request code
...
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check which request we're responding to
if (requestCode == SCAN_RESULT) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Action to take if result successful
Intent intent = new Intent(this, ResultActivity.class);
startActivity(intent);
}
}
}
ScannerActivity
...
public class ScanBarcodeActivity extends AppCompatActivity {
Button mBtnClose;
private CaptureManager capture;
private DecoratedBarcodeView barcodeScannerView;
private ViewfinderView viewfinderView;
private void initViews() {
mBtnClose = findViewById(R.id.barcode_header_close);
barcodeScannerView = findViewById(R.id.zxing_barcode_scanner);
viewfinderView = findViewById(R.id.zxing_viewfinder_view);
}
private void initListener() {
mBtnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barcode);
initViews();
initListener();
capture = new CaptureManager(this, barcodeScannerView);
capture.initializeFromIntent(getIntent(), savedInstanceState);
capture.decode();
changeMaskColor(null);
}
#Override
protected void onResume() {
super.onResume();
capture.onResume();
}
#Override
protected void onPause() {
super.onPause();
capture.onPause();
}
#Override
protected void onDestroy() {
super.onDestroy();
capture.onDestroy();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
capture.onSaveInstanceState(outState);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
public void changeMaskColor(View view) {
}
}
EDIT: I've tried this but it's obviously not working, this is basically what I'm looking to get working. If the SCAN_RESULT = the QR_CODE then go to next activity, else pop a message saying try again.
static final int SCAN_RESULT = 1; // The request code
String QR_CODE = "EC0111-1234567899";
int RESULT = Integer.parseInt(QR_CODE);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check which request we're responding to
if (requestCode == SCAN_RESULT) {
// Make sure the request was successful
if (SCAN_RESULT == RESULT) {
Intent intent = new Intent(this, ResultActivity.class);
startActivity(intent);
} else {
Toast.makeText(this, "Incorrect QR code, please try again", Toast.LENGTH_LONG).show();
}
}
}
There are some approaches you can try.
Encrypt information: You can encrypt information coded in QR so that other can't read it as well as you can identify your own QR. To do so
Encrypt information with a key
Generate QR with encrypted information
Read and try to decrypt information. If you can decrypt than it's your QR.
Develop your own QR: It may be costly for you but it is a wonderful idea to generate your own styled QR like facebook messenger , snapchat and whatsapp etc. In that case you can't use standard ZXING library. You have to customised ZXING library or develop a new one.
Add tag to information: You can add a unique tag(text) in your QR information. By which you can identify your QR code.
So I'm trying to build a QR Code Scanner, but my result only showing a text. Even the QR Code is have a URL link on it. It can't be open to a browser.
Here's my code for the scanner:
public class ReaderActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
Button scan_btn = findViewById(R.id.scan_btn);
final Activity activity = this;
scan_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.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, "You cancelled the scanning", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}}
What should I add or change to get clickable URL result? Can someone fix this? Please help me to get my project done. I really need it.
You will get QR results on result.getContents() as soon as you get it you should have to use action intent for view to open URL in browser
I am using the android lock pattern library for a project of mine when I print the output pattern in a toast I get all garbage values like [C245faa3a8
I got the library and the code from https://code.google.com/p/android-lockpattern/
here is my code.
public class PatternTest extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// This is your preferred flag
Intent intent = new Intent(LockPatternActivity.ACTION_CREATE_PATTERN,
null, getBaseContext(), LockPatternActivity.class);
startActivityForResult(intent, REQ_CREATE_PATTERN);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
char[] pattern = null;
switch (requestCode) {
case REQ_CREATE_PATTERN: {
if (resultCode == RESULT_OK) {
pattern = data
.getCharArrayExtra(LockPatternActivity.EXTRA_PATTERN);
}
break;
}// REQ_CREATE_PATTERN
}
Toast.makeText(getApplicationContext(),pattern.toString(),
Toast.LENGTH_LONG).show();
}
}
use new String(byte[]) instead of toString():
Toast.makeText(getApplicationContext(),new String(pattern),
Toast.LENGTH_LONG).show();