Unable to get result from ZXing barcode scanner - android

I am trying to implement barcode scanner activity in my Android application but I heard that it's not possible to do so without asking the user to install the ZXing bar code scanner.
So what I did is that I used the ZXing integrator class for initiating a scan here is my code. But I am unable to show the result in the two text views. I must mention that the scan is started and the result is shown in the barcode scanner window itself, but then nothing comes on the ScanCodeActivity layout which has two text views on it.
I also would like to know if it's possible to return the result from th eweb into my text views.
I used this link as reference:
http://code.tutsplus.com/tutorials/android-sdk-create-a-barcode-reader--mobile-17162
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class ScanCodeActivity extends Activity {
private Button scan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scancodelayout);
scan= (Button)findViewById(R.id.scan_button);
Button scanBtn = (Button)findViewById(R.id.scan_button);
final TextView formatTxt = (TextView)findViewById(R.id.scan_format);
final TextView contentTxt = (TextView)findViewById(R.id.scan_content);
scanBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IntentIntegrator scanIntegrator = new IntentIntegrator(ScanCodeActivity.this);
scanIntegrator.initiateScan();
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
if (scanningResult != null) {
final TextView formatTxt = (TextView)findViewById(R.id.scan_format);
final TextView contentTxt = (TextView)findViewById(R.id.scan_content);
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
Toast toast = Toast.makeText(this, "Content:" + scanContent + " Format:" + scanFormat , Toast.LENGTH_LONG);
formatTxt.setText("FORMAT: " + scanFormat);
contentTxt.setText("CONTENT: " + scanContent);
//we have a result
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
Log.i("App","Scan unsuccessful");
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Remove the if (requestCode == 0) check from your onActivityResult() method similar to the example from the library's wiki page.
Note: You are checking for the wrong requestCode. The library uses 0x0000c0de (see the source) instead of 0.

Related

how to use toggle switch by result of speech recognition

i want use toggle switch by voice commands like switch on and switch off so i got a code for voice recognition from a site but dont know how to trigger my toggle button thru it
The code of voice recognition i used -
package com.authorwjf.talk2me;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
protected static final int REQUEST_OK = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(i, REQUEST_OK);
} catch (Exception e) {
Toast.makeText(this, "Error initializing speech to text engine.", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==REQUEST_OK && resultCode==RESULT_OK) {
ArrayList<String> thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
((TextView)findViewById(R.id.text1)).setText(thingsYouSaid.get(0));
}
}
}
your array thingsYouSaid have all possible string array you have . for example if I say hello it will have like [hello,aloe,hallo,no] so what you have to do is you can match your string switch off to result string array and if it is match with like "switch off" than change value of your switch from on to off likewise;
if (requestCode==REQUEST_OK && resultCode==RESULT_OK) {
ArrayList<String> thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
for(String value : thingsYouSaid.get(0)){
if(value.equalsignorecase("switch off")){
// change value for switch to off
break;
}
else if(value.equalsignorecase("switch on")){
// change value for switch to on
break;
}
}

Android Text to speech And Speech to text

I am working with text to speech and speech to text at the same time. I am making an app in which it ask a question through text to speech and get the answer from the user through speech and app convert it to text. but it does not work fine. both are working at the same time like what it speak, it text it back. can we give some delay so that when it stop speaking then it listen for the voice and return that text.
`
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;
public class MainActivity extends Activity implementsTextToSpeech.OnInitListener {
TextView eText1;
TextToSpeech textToSpeech;
String speech = "Hey, Can u read me?";
private final int REQ_CODE_SPEECH_INPUT = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eText1 = (TextView)findViewById(R.id.textView2);
textToSpeech = new TextToSpeech(this,this);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
eText1.setText(result.get(0));
}
break;
}
}
}
private void promptSpeechInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, speech);
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void speakOut() {
String text = speech;
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
#Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS){
int result = textToSpeech.setLanguage(Locale.ENGLISH);
if(result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA){
Toast.makeText(this, "This language is not supported", Toast.LENGTH_LONG).show();
}
else{
speakOut();
promptSpeechInput();
}
}else{
Toast.makeText(this, "Initialization failed", Toast.LENGTH_LONG).show();
}
}
}
`
Try changing:
speakOut();
promptSpeechInput();
to
promptSpeechInput();
And then add:
speakOut();
after
eText1.setText(result.get(0));
This should speak the text after it has finished getting the text
Edit: use this to determine when the speech is finished. When it is just call promptSpeechInput()

Android Capture image with filename

this is my first day on android and i would like to make an app that will capture an image and the image name will output at the variable and textbox under the ImageView.
This is my code now and capture image is functioning. What i need to do next is to get the file name and filepath. Thanks. I tried to find but i dont know where to put the codes.
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class Create extends ActionBarActivity {
Button capImg;
int requestcode = 1;
ImageView imgView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
capImg = (Button)findViewById(R.id.capImg);
imgView = (ImageView)findViewById(R.id.imgView);
capImg.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0){
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(i.resolveActivity(getPackageManager())!=null){
startActivityForResult(i, requestcode);
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.create, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onActivityResult(int requestCode, int resultcode, Intent data){
if(requestCode==requestcode){
if(resultcode==RESULT_OK){
Bundle bundle = new Bundle();
bundle = data.getExtras();
Bitmap BMP;
BMP = (Bitmap)bundle.get("data");
imgView.setImageBitmap(BMP);
}
}
}
}
To save a picture Add a fileUri to the intent.
private Uri fileUri;
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); // create a file to save the video
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
http://developer.android.com/guide/topics/media/camera.html
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}

Zxing barcode integeration: The constructor IntentIntegrator() is undefined

i'm trying to integrate Zxing library, and use the barcode scanner from my app.
so, downloaded the 2 java files IntentIntegrator and IntentResult, put them into this package:
com.google.zxing.integration
where my app is in this package:
com.example.mindstormsgamepad
the code I'm using in my activity is:
package com.example.mindstormsgamepad;
import com.google.zxing.integration.IntentIntegrator;
import com.google.zxing.integration.IntentResult;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.Button;
import android.widget.TextView;
/**
* BarcodeScan Activity
*/
public class BarcodeScanActivity extends CommonActivity implements OnClickListener{
/** Debug */
protected String TAG_SUB = "BarcodeScanActivity";
private Button scanBtn;
private TextView formatTxt, contentTxt;
#Override
public void onCreate(Bundle savedInstanceState) {
initTabSub( TAG_SUB );
log_d( "onCreate" );
super.onCreate( savedInstanceState );
/* set the layout on the screen */
View view = getLayoutInflater().inflate( R.layout.activity_barcode_scan, null );
setContentView( view );
/* Initialization of Bluetooth */
initManager( view );
setTitleName( R.string.activity_barcodescan );
initButtonBack();
initInputDeviceManager();
Toast.makeText(BarcodeScanActivity.this, "QR Scan!", Toast.LENGTH_SHORT).show();
/* Initialization of Scanning */
scanBtn = (Button)findViewById(R.id.scan_button);
formatTxt = (TextView)findViewById(R.id.scan_format);
contentTxt = (TextView)findViewById(R.id.scan_content);
scanBtn.setOnClickListener(this);
}
// --- onCreate end ---
/**
* === onResume ===
*/
#Override
public void onResume() {
log_d( "onResume()" );
super.onResume();
startService();
mInputDeviceManager.register();
}
/**
* === onPause ===
*/
#Override
public void onPause() {
log_d( "onPause()" );
super.onPause();
sendStop();
mInputDeviceManager.unregister();
}
#Override
public void onClick(View v) {
// Start Scan
if(v.getId()==R.id.scan_button){
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
//we have a result
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
formatTxt.setText("FORMAT: " + scanFormat);
contentTxt.setText("CONTENT: " + scanContent);
}else{
Toast toast = Toast.makeText(getApplicationContext(),
"No Barcode data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
}
but I'm getting this error,
The constructor IntentIntegrator(BarcodeScanActivity) is undefined
and
The method initiateScan(Activity) in the type IntentIntegrator is not applicable for the arguments ()
on these lines:
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
I'm new to android programming,
how to solve this problem?
thanks for your help.
Try This :
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE");
startActivityForResult(intent, 0);
For Result Do the code in onActivityResult method
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Toast.makeText(getActivity(), "result ", 1000).show();
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
//do code here
}
else if (resultCode == RESULT_CANCELED) {
//do code here
}
}
This means your Activity does not ultimately extend android.app.Activity. see what CommonActivity extends. Otherwise maybe you somehow have an old or wrong copy of the IntentIntegrator.

Zxing when qr code is scanned it reverts to menu

I have the zxing library imported into my project and the scanner works like a charm but when i scan a qr code it says Qr code found and goes back to the menu i had set up is there any way to show the result and set it to open the url
package com.Qrgolf.App;
import java.util.regex.Pattern;
import com.google.zxing.Result;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button scan = (Button) findViewById(R.id.SCANBUTTON);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
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
}
}
}
}
You should consider going back to your old questions and accepting answers if they were correct.
Also you need to change the onActivityResult() method to do whatever it is that you want to do with the resulting String from the QR.
here is an example:
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
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(android.net.Uri.parse(contents));
startActivity(intent);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}

Categories

Resources