I have implemented some of the zebra crossing (zxing) code in an attempt to create an Android application that scans some barcodes. Pretty basic stuff really. Here are the fundamentals of my code:
public void recognizeQRClick(View view) {
printingNewQR = false;
recognizingQR = true;
text1.setText("Recognize QR button clicked");
IntentIntegrator intentIntegrator = new IntentIntegrator(this);
intentIntegrator.initiateScan();
return;
}
#Override
public void onActivityResult (int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE:
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
String contents = scanResult.getContents();
if(printingNewQR) {
return;
} else if(recognizingQR){
displayQRInfo(contents);
}
text1.setText(contents);
}
break;
}
}
The problem is that, while the scanning portion appears to work (when the appropriate button is clicked, the app jumps to the scanning screen, and when the barcode is centered a small golden dot appears in the center to indicate a good scan) it never finishes. The app just remains in the scanning screen, scanning the same barcode over and over. I have googled this problem extensively and found a similar issue in a couple of instances, but always their problems seem to be fixed once they use the IntentIntegrator class, which I am doing. What is wrong?
Related
Google Speech-to-Text API supports Marathi as per their documentation here. However I have not been able to get it working on my Android phone. I have already added 'Marathi' in languages for my Android device (Moto G6, running android 7.1.1). However, I am not yet able to get a simple SMS converted from speech-to-text with this. Marathi typing works fine though.
Do I need to modify any other setting? What else is required? Any pointers for this would be highly appreciated.
Have you configured your intent to detect marathi languages as below?
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,"mr-IN");
You can checkout other languages codes here.
You can use following code for your reference:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.RecordBtn:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// Use Off line Recognition Engine only...
intent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, false);
// Use Marathi Speech Recognition Model...
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "mr-IN");
try {
startActivityForResult(intent, SST_REQUEST_CODE);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.error),
Toast.LENGTH_SHORT).show();
}
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SST_REQUEST_CODE:
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> getResult = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
Original.setText(getResult.get(0));
conversionTable = new ConversionTable();
String Transformed_String = conversionTable.transform(getResult.get(0));
result.setText(Transformed_String);
}
break;
}
}
I have implemented search view using Leanback Search Fragment in Android TV App.
It works fine for the keyboard input text. But the voice search returns RESULT_CLIENT_ERROR 99%. However the text appears in the edit text on the screen but does not return in onActivityResult. Once in a bluemoon I get RESULT_OK with correct data in the intent.
Device: Nexus Player
Even the default search works the same way most of the items. Hardly do i get the results via voice. (Search in home screen of nexus player i mean)
I try to get the text using the reference of the SearchEditText no luck but.
The following is the code:
setSpeechRecognitionCallback(new SpeechRecognitionCallback() {
#Override
public void recognizeSpeech() {
try {
startActivityForResult(getRecognizerIntent(), REQUEST_SPEECH);
} catch (final ActivityNotFoundException e) {
Log.d("ActivityNotFoundException",e.toString());
}
}
});
onActivityResult
#Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_SPEECH:
switch (resultCode) {
case Activity.RESULT_OK:
setSearchQuery(data, true);
break;
case RecognizerIntent.RESULT_CLIENT_ERROR:
Log.d("RESULT_CLIENT_ERROR", String.valueOf(requestCode));
break;
}
}
}
How do i make it work 100% Is there anything I can change in the intent provided?
This is a problem with Nexus player as even the default search operation of the Nexus Player behaves the same way.
However the same piece of code works 100% fine on Sony Bravia and other Android TVs.
I'm developing an app with a QR scanner.
I have three activities; "Skatte" which has a button that refers to another activity called "Skat1". "Skat1" contains a QR scanner. When you scan and get a positive result, it moves on to a third acitivy called "Skat1Resultat".
My problem is that I need to close the "Skat1" activity completely (or at least just make it impossible to enter) when you have scanned a QR code, which means that I also need to make the button on "Skatte" which refers to "Skat1" unclickable.
I've read about background services, threads and intents, but I still can't figure out how to do it. I've found a code which can change text on a third activity when you scan, but I need to do it with either a button or a clickable text.
This is the code for when a QR code has been scanned and it moves from "Skat1" to "Skat1Resultat".
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
Toast toast = Toast.makeText(Skat1.this, contents, 9000);
toast.show();
startActivity(new Intent(Skat1.this, Skat1Resultat.class));
finish();
}
} else if (resultCode == RESULT_CANCELED) {
}
I though that finish(); could do the work and close the activity (I've also tried with onDestroy), but it's still possible to enter the activity when you click the button on "Skatte".
You can use a static boolean flag with value false.
Set it to true when you get "resultCode == RESULT_OK"
If the flag is false then the button is clickable
If the flag is true then the button is not clickable.
if(flag)
{
button.setClickable(false);
}
else
{
button.setClickable(true);
}
I have three buttons that onlongpress bring up my new IntentIntegrator scans via Barcode Scanner. I successfully have/had it scanning and using the code scanned to do something with when it is only one button.
How can I pass a value or something that when "protected void onActivityResult" is called it will know which button it came from so I can do different things with it depending on which button was long pressed.
My current setup is like this:
button1.setOnLongClickListener(this);
button2.setOnLongClickListener(this);
button3.setOnLongClickListener(this);}
}
public boolean onLongClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
return true;
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
case IntentIntegrator.REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
IntentResult intentResult =
IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (intentResult != null) {
String contents = intentResult.getContents();
String format = intentResult.getFormatName();
//do stuff with the scan. But I want to do different stuff depending on which button was pressed.
Two options:
One, you can modify the IntentIntegrator source code so that it takes different REQUEST_CODES as constructor parameters (or just add a setRequestCode parameter). Then in onActivityResult, check for which requestCode was returned (each of your buttons would return a different request code). This would be the recommended approach.
Second option is the hacky one: In your code, create a member variable that tracks which was the last button pressed (check whether the view passed in onLongClick matches button1,button2, or button3), and use that information in onActivityResult to choose what to do with the results passed back from barcode scanner.
I am trying to produce
public Button.OnClickListener mScan = new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
};
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
Using the libraries therefore not needing to install the software, I have had a look at the application that comes with it, and added CaptureActivity.java to my project to see if I can locate the core scanning loop but it kept just requiring more and more files (ended up with about 28 in total) and after I had no errors it still didn't work.
I am not looking into doing anything fancy, just on a button click open the capture layout, scan a code, return the code.
There are a few examples on how to decode local files but not the actual scanning side of it to detect a actual barcode.
So, what I help with is embedding the scanning code
Thanks
I think you're mixing up two things. The code you have shows you are trying to integrate via Intent, in which case you do not need any project code in your application at all.
The source in android/ is a complete stand-alone app (Barcode Scanner) and it is not written for you to copy (although the Apache License permits it). What you are doing is not supported, recommended, or encouraged -- integrating via Intent is much easier.
Write your own app, perhaps looking to the Android source code for inspiration, and that uses the core/ library for scanning. The core scanning happens in DecodeThread.