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.
Related
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class ReaderActivity extends AppCompatActivity {
private Button scan_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
scan_btn = (Button) 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
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result.getContents() != null) {
if(result.getContents().equals("Electronics A1")){
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);
}
}
}
Hi all, currently doing a project for my school.
now im having problems with the program.
if(result.getContents() != null) {
if(result.getContents().equals("Electronics A1")){
Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_LONG).show();
curently this if stament is being ignored by the app.
the purpose of the statement is when i scan a specific qr code it will move to a new activity or do something.
yesterday the statement was working but suddenly today it wont work......
can you guys help me?
currently new to android studio
I am integrating the zxing scanner into my android application and I am coming across a weird situation where the onActivityResult (in the activity initiating the scan) is being called before the scanning intent opens. I have looked at several examples and my code seems to match what I am seeing in many of the tutorials. Here is the code for the activity.
package com.honeydewit;
import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.honeydewit.adapters.ListItemAdapter;
import com.honeydewit.listeners.OneOffClickListener;
import com.honeydewit.pojos.BasicList;
import com.honeydewit.pojos.ListItem;
public class ListHomeActivity extends BasicActivity{
private ImageButton addItemBtn;
private ImageButton addByScanBtn;
private ArrayList<ListItem> lists = new ArrayList<ListItem>();
public static ListItemAdapter listAdapter;
private TextView headerTxt;
private BasicList basicList;
private ListView listView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listshome);
listView =(ListView) findViewById(R.id.list);
basicList = getApplicationContext().getCurrentList();
headerTxt = (TextView)findViewById(R.id.headerTxt);
headerTxt.setText(basicList.getName());
headerTxt.setTypeface(getApplicationContext().getTypeface());
//add button
addItemBtn = (ImageButton)findViewById(R.id.add);
addItemBtn.setOnClickListener(new OneOffClickListener() {
#Override
public void onClick(View v) {
addToList(v, basicList);
}
});
addByScanBtn = (ImageButton)findViewById(R.id.addByScan);
addByScanBtn.setVisibility(View.VISIBLE);
addByScanBtn.setOnClickListener(new OneOffClickListener() {
#Override
public void onClick(View v) {
addToListByScan(v, basicList);
}
});
setupListAdapter(basicList.get_id());
}
private void setupListAdapter(int listId) {
populateListItems(listId);
listAdapter = new ListItemAdapter(this, R.layout.listrow, lists);
listView.setAdapter(listAdapter);
}
private void populateListItems(int listId) {
ArrayList<ListItem> items = (ArrayList<ListItem>)getApplicationContext().getShoppingListDbHelper().getShoppingListItems(listId);
for(ListItem item : items ) {
lists.add(item);
}
}
private void addToList(View view, BasicList list) {
if(basicList.getListTypeId() == Constants.TODO_LIST_TYPE_CDE) {
Intent newListIntent = new Intent(getBaseContext(), ToDoItemActivity.class);
startActivityForResult(newListIntent, 1);
}
else {
Intent newListIntent = new Intent(getBaseContext(), ItemActivity.class);
startActivityForResult(newListIntent, 1);
}
}
private void addToListByScan(View view, BasicList list) {
try {
IntentIntegrator zxingIntegrator = new IntentIntegrator(this);
zxingIntegrator.initiateScan();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "ERROR:" + e, 1).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
Log.d(getClass().getName(), scanResult.getContents());
Log.d(getClass().getName(), scanResult.getFormatName());
Log.d(getClass().getName(),data.getStringExtra("SCAN_RESULT_FORMAT"));
Log.d(getClass().getName(),data.getStringExtra("SCAN_RESULT"));
}
}
}
}
onActivityResult is called by Android whenever your app needs to be told about an Intent. That could be from lots of places. You know by looking at the request code and comparing it to the one in IntentIntegrator. Or let that class do all this for you.
My guess is your intent-filter is too broad and you're hearing things you don't expect.
I'm trying to use a barcode scanner and then take that input and use in another activity to open with a url. I've been able to get the data to return, just not in another activity and haven't seen any projects exactly like this. I'm not sure if it has to do with intent or how I'm calling the string. The webview in the second java works but doesn't take the string. Thanks for the help!
Scanner.java (which works okay)
package com.pangolin.rollin.ts;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Scanner extends Activity {
TextView tvStatus;
TextView tvResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
Button websku = (Button) findViewById(R.id.btnsku);
websku.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myintent = new Intent(Scanner.this, Websku.class);
startActivity(myintent);
}
});
tvStatus = (TextView) findViewById(R.id.tvStatus);
tvResult = (TextView) findViewById(R.id.tvResult);
Button scanBtn = (Button) findViewById(R.id.btnScan);
// in some trigger function e.g. button press within your code you
// should add:
scanBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE");
startActivityForResult(intent, 0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getApplicationContext(), "ERROR:" + e, Toast.LENGTH_LONG)
.show();
}
}
});
}
// In the same activity you’ll need the following to retrieve the results:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
tvStatus.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
tvResult.setText(intent.getStringExtra("SCAN_RESULT"));
} else if (resultCode == RESULT_CANCELED) {
tvStatus.setText("Press a button to start a scan.");
tvResult.setText("Scan cancelled.");
}
}
}
}
And websku.java (doesn't work, supposed to take results from previous activity.
package com.pangolin.rollin.ts;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Websku extends Activity {
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String sku = intent.getStringExtra("SCAN_RESULT");
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_websku);
WebView webView = (WebView) findViewById(R.id.webview_sku);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if (progress == 100)
activity.setTitle(R.string.title_activity_websku);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://m.radioshack.com/radioshack/catalog/searchList.do?categoryId=&keyword="+sku);
};
}
You don't set any extra to Websku intent:
websku.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myintent = new Intent(Scanner.this, Websku.class);
startActivity(myintent);
}
});
Should be:
websku.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myintent = new Intent(Scanner.this, Websku.class);
myintent.putExtra("somename", somevalue);
startActivity(myintent);
}
});
You don't set the extras for the websku Activity. Save the intent returned from the scanner:
private Intent mWebskuIntent;
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
mWebskuIntent = intent;
// more of your code
Then when you start the websku Activity make a copy of the saved intent which will copy also the extras returned from the scanner:
Intent myintent = new Intent(mWebskuIntent);
myintent.setClass(Scanner.this, Websku.class);
startActivity(myintent);
You might want to check for mWebskuIntent being null as well.
I have a progress bar simply designed and placed in MainActivity.
I have second activity to set max value of ProgressBar
My question is; how can use public void onActivityResult(int requestCode, int resultCode, Intent data)'s returned value for ProgressBar's setMax() method?
package com.example.example;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity {
final int request_Code = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*I want to use value cames from onActivityResult() for setMax()
instead of default entered value -> 100 */
ProgressBar calorieBar = (ProgressBar) findViewById(R.id.progress1);
calorieBar.setMax(100);
calorieBar.setProgress(70);
//Go to another Activity and start
Button newPlan = (Button) findViewById(R.id.newPlanBTN);
newPlan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent("com.hakkikonu.dailycalorie.NEWPLAN");//new_plan activity
startActivityForResult(i, 1);
onResume();
}
});
}//end of onCreate
//Sets Calorie value Comes from NewPlan.java and makes it Toast to show user
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
Toast.makeText(this,"Adjusted to "+data.getData().toString(),
Toast.LENGTH_LONG).show();
// Do somethings here: get data and convert it to integer
}
}
}
}
NewPlan.Java
package com.hakkikonu.dailycalorie;
import com.hakkikonu.dailycalorie.R.layout;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
public class NewPlan extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_plan);
final TextView seekBarValue = (TextView)findViewById(R.id.seekBarValue);
//Plan Button Click Listener
final Button setPlanButton = (Button)findViewById(R.id.setPlanBTN);
setPlanButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
setPlanButton.setBackgroundColor(Color.YELLOW);
Intent calorieVal = new Intent();
//---set the data to pass back---
calorieVal.setData(Uri.parse(
seekBarValue.getText().toString()));
setResult(RESULT_OK, calorieVal);
//---closes the activity---
finish();
}
});//end SetPlan Button Listener
SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar);
seekBar.setProgress(0); //initial value of seekBar
seekBar.incrementProgressBy(50); //increment step
seekBar.setMax(5000); //maximum possible calory for a day.
//seekBar Listener
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//text view # top of the seekBar
progress = progress / 50;
progress = progress * 50;
if(progress == 0){
seekBarValue.setText(String.valueOf(progress)+" Calorie");
}
else{
seekBarValue.setText(String.valueOf(progress)+" Calories");
}
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
//Don't delete
}
#Override
public void onStopTrackingTouch(SeekBar arg0) {
// Don't delete
}
});
}
}
Assuming your second Activity puts an int as its extra, with a key of "YOUR_KEY" in its result Intent, just simply set the progress bar's max to that int extra:
//Sets Calorie value Comes from NewPlan.java and makes it Toast to show user
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
Toast.makeText(this,"Adjusted to "+data.getData().toString(),
Toast.LENGTH_LONG).show();
((ProgressBar) findViewById(R.id.progress1)).setMax (data.getIntExtra ("YOUR_KEY", 0));
}
}
}
To make things simple (I'd say):
public class NewPlan extends Activity{
private int myProgress = 0;//add this line
Then in the changed listener:
progress = progress / 50;
progress = progress * 50;
myProgress = progress;//add this line
Then to set the data, change to this:
Intent calorieVal = new Intent();
calorieVal.putExtra ("YOUR_KEY", myProgress);
setResult(RESULT_OK, calorieVal);
//---closes the activity---
finish();
This is how you set resulting intent in NEWPLAN Activity:
Intent intent = new Intent();
intent.putExtra("myInt", 12);
setResult(Activity.RESULT_OK, intent);
finish();
And then in MainActivity you fetch that data using:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == yourRequestCode && RESULT_OK) {
int myInt = data.getIntExtra("myInt", 0);
// do more if you wish
}
}
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
}
}
}