I try to get QR code result to textView.
Although the camera is functioning the scan process is not supporting,although the function is inappropriate .an error message is not being display... What I do for that?
This is my QrActivity.....
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class QrActivity extends AppCompatActivity {
/**
* QR code declaration
*/
Button btnscan, btnSearch;
static TextView lblSearch;
#Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr);
btnscan = (Button) findViewById(R.id.btnscan);
btnscan.setClickable(false);
btnSearch = (Button) findViewById(R.id.btnSearch);
btnSearch.setClickable(false);
lblSearch = (TextView) findViewById(R.id.lblSearch);
final Activity activity = this;
btnscan.setOnClickListener(new View.OnClickListener() {
#Override
public
void onClick(View v) {
btnscan.setClickable(true);
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();
}
});
}
/**
* ---------------------------------------------------- QR code ----------------------------------------------------------
*/
#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_SHORT).show();
} else {
/**
* Qr code result
* */
lblSearch.setText(result.getContents());
// Toast.makeText(this, result.getContents(), Toast.LENGTH_SHORT).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
/**
* -----------------------------------------QR scan-- Finished-------------------------------------*
* */
Related
I have two buttons, both of them does barcode scanning. Once it returns from scanning, I want to know which button the user clicked so I can take different path on "onActivityResult" method. How do I do that?
I can find out which button was clicked inside onClick() method but that won't help.
public class ReaderActivity extends AppCompatActivity {
private Button scan_btn, verifyButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
scan_btn = (Button) findViewById(R.id.scan_btn);
verifyButton = (Button) findViewById(R.id.buttonVerify);
final Activity activity = this;
scan_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
verifyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("LINE UP BARCODE WITH SCANNER");
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);
}
}
}
You can add boolean value inside your activity
eg :
private boolean isScanButtonClicked;
Use below code
scan_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
isScanButtonClicked = true; // true a scan button clicked
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
verifyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
isScanButtonClicked = false; // false as scan button not clicked
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("LINE UP BARCODE WITH SCANNER");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
According to isScanButtonClicked value you can perform your action inside onActivityResult()
Hope these help you.
You could add some flag/data to Intent data return and process this data into onActivityResult to get the button clicked in your action, or change the value of resultCode to process this result on correct way.
Resolved it through get set method
Created a class like below
public class myApplication extends Application{
private String someVariable;
public String getWhichButton() {
return someVariable;
}
public void setWhichButton(String someVariable) {
this.someVariable = someVariable;
}
}
Make sure to add android:name=".myApplication" in the <Application> tag in AndroidManifest. Then in my ReaderActivity during the button click action, I set
((myApplication) activity.getApplication()).setWhichButton("scan");
OR
((myApplication) activity.getApplication()).setWhichButton("Verify");
depending on which button I click. On the onActivityResult method I use
((myApplication) this.getApplication()).getWhichButton();
to retrieve which button was clicked to take action accordingly.
public class ReaderActivity extends AppCompatActivity{
private Button scan_btn, verifyButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
scan_btn = (Button) findViewById(R.id.scan_btn);
verifyButton = (Button) findViewById(R.id.buttonVerify);
final Activity activity = this;
scan_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
((myApplication) activity.getApplication()).setWhichButton("scan");
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(true);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
verifyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
((myApplication) activity.getApplication()).setWhichButton("verify");
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("LINE UP BARCODE WITH SCANNER");
integrator.setCameraId(0);
integrator.setBeepEnabled(true);
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();
String val = ((myApplication) this.getApplication()).getWhichButton();
if(val=="scan") {
Toast.makeText(this, "Scan button pressed", Toast.LENGTH_LONG).show();
}
else
Toast.makeText(this, "Verify button pressed", Toast.LENGTH_LONG).show();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
public class MainActivity extends AppCompatActivity {
public Button btnOne;
public Button btnTwo;
public String btnClicked;
public static int VALUEONE = 1;
public static int VALUETWO = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnUm = (Button)findViewById(R.id.btnUm);
btnDois = (Button)findViewById(R.id.btnDois);
btnOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent test = new Intent(MainActivity.this, NextActivity.class);
btnClicked = "btnOne";
startActivityForResult(test,VALUEONE);
}
});
btnTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent test = new Intent(MainActivity.this, NextActivity.class);
btnClicked = "btnTwo";
startActivityForResult(test,VALUETWO);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("Btn clicked: " + btnClicked);
}
...........
I try this code in my Android Studio and it works. You made something similar to this?
I have the following code. Whenever I press a button, the result is displayed in Display class but sometimes it throws me out from app and sometimes it works properly. I Dont know how?
Can anyone explain why this is happening?
public class MainActivity extends AppCompatActivity {
Context con = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onScanClick(View view) {
initiateScan();
}
private void initiateScan() {
IntentIntegrator integrator = new IntentIntegrator((Activity) con);
integrator.setBeepEnabled(true);
integrator.initiateScan();
}
// Get the results:
#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, "Cancelled", Toast.LENGTH_LONG).show();
} else {
ShowResult.Dispay((Activity) con,result.getContents());
Vibrator.vibrate(250L,con);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
How do I take the 'this' value ( from the 6th line) and make it into a string?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qrscanner);
fragment = (BarcodeFragment)getSupportFragmentManager().findFragmentById(R.id.sample);
fragment.setScanResultHandler(this);
btn = ((Button)findViewById(R.id.scan));
btn.setEnabled(false);
}
For example instead of just toasting the value, can I use the value and make it into a string and then carrying it into another activity.
#Override
public void scanResult(ScanResult result) {
btn.setEnabled(true);
Intent intent = new Intent(QrscannerActivity.this,ProductInfoActivity.class);
startActivity(intent);
Toast.makeText(this, result.getRawResult().getText(), Toast.LENGTH_LONG).show();
}
You should use the Google ZXing Barcode Scanner library and with the below code:
extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HandleClick hc = new HandleClick();
findViewById(R.id.butQR).setOnClickListener(hc);
findViewById(R.id.butProd).setOnClickListener(hc);
findViewById(R.id.butOther).setOnClickListener(hc);
}
private class HandleClick implements OnClickListener{
public void onClick(View arg0) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
switch(arg0.getId()){
case R.id.butQR:
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
break;
case R.id.butProd:
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
break;
case R.id.butOther:
intent.putExtra("SCAN_FORMATS", "CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF,CODABAR");
break;
}
startActivityForResult(intent, 0); //Barcode Scanner to scan for us
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
TextView tvStatus=(TextView)findViewById(R.id.tvStatus);
TextView tvResult=(TextView)findViewById(R.id.tvResult);
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.");
}
}
}
}
I am using the ZXING QR Code Reader. It is working fine. Detecting codes etc but it does not shows any detection progress(Like red line) while it is scanning the QR Code. Here is Screenshot
And my ScanActivity is,
ScanCode.Java
public class ScanCode extends Activity implements OnClickListener {
Button btnScan;
TextView scanResult;
String text;
Bitmap bmp;
ImageView ivPic;
#Override
public void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.scan_code);
initViews();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.btnScanCode) {
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 1);
}
}
private void initViews() {
scanResult = (TextView) findViewById(R.id.scanResult);
ivPic = (ImageView) findViewById(R.id.capturedImg);
btnScan = (Button) findViewById(R.id.btnScanCode);
btnScan.setOnClickListener(this);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1)
if (resultCode == Activity.RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
String format = data.getStringExtra("SCAN_RESULT_FORMAT");
Toast.makeText(getApplicationContext(), contents,
Toast.LENGTH_SHORT).show();
scanResult.setText(contents);
}// if result_ok
}// onActivityResult
}
I know this answer is bit late but I've just used Zxing barcode/QR-Code scanner recently in my app, if anyone has issue implementing it just follow the steps below (Using Android Studio Official IDE for android).
First add dependencies in app --> build.gradle file
dependencies {
compile 'com.journeyapps:zxing-android-embedded:3.2.0#aar'
compile 'com.google.zxing:core:3.2.1'
}
Activity Usage:
public class MainActivity extends AppCompatActivity {
Button btnScan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnScan = (Button) findViewById(R.id.btnScan);
btnScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
scanBarcode();
}
});
}
private void scanBarcode()
{
new IntentIntegrator(this).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) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
}
}
}
If you want to use custom callback and BarCodeView. Zxing provide this functionality simply make layout file
<com.journeyapps.barcodescanner.CompoundBarcodeView
android:id="#+id/barcode_scanner"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.journeyapps.barcodescanner.CompoundBarcodeView>
<TextView
android:id="#+id/tvScanResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan Results will be shown here"
android:textColor="#android:color/white"
android:textStyle="bold"/>
In Activity use the following code
public class MainActivity extends AppCompatActivity
{
CompoundBarcodeView barcodeView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
barcodeView = (CompoundBarcodeView) findViewById(R.id.barcode_scanner);
barcodeView.decodeContinuous(callback);
barcodeView.setStatusText("");
}
#Override
protected void onResume()
{
super.onResume();
barcodeView.resume();
isScanned = false;
}
#Override
protected void onPause()
{
super.onPause();
barcodeView.pause();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return barcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
private BarcodeCallback callback = new BarcodeCallback()
{
#Override
public void barcodeResult(BarcodeResult result)
{
if (result.getText() != null)
{
barcodeView.setStatusText(result.getText());
tvscanResult.setText("Data found: " + result.getText());
}
//you can also Add preview of scanned barcode
//ImageView imageView = (ImageView) findViewById(R.id.barcodePreview);
//imageView.setImageBitmap(result.getBitmapWithResultPoints(Color.YELLOW));
}
#Override
public void possibleResultPoints(List<ResultPoint> resultPoints)
{
System.out.println("Possible Result points = " + resultPoints);
}
};
}
Source zxing-android-embedded. Hope someone get help from this solution
I have a Parent activity which is sending data to child activity but the child is not returning the result. I posted portion of code from both activity altogether below- Theres a few code that I didnt post for unnecessity.
public class TdeeActivity extends Activity {
public static final int CALLED_ACTIVITY = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tdee);
Button ok=(Button)findViewById(R.id.btnOk);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent bmr= new Intent(TdeeActivity.this,BMRActivity.class);
startActivityForResult(bmr,CALLED_ACTIVITY);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case CALLED_ACTIVITY:
if (resultCode == RESULT_OK) {
Toast.makeText(this, "THE RESULT-"+data.getExtras().getString("result"),
Toast.LENGTH_SHORT).show();
}
}
}
} // end class
public class BMRActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmr);
Button btnOk=(Button)findViewById(R.id.btnOk);
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showResult();
}
});
public void showResult() {
Intent data= new Intent();
data.putExtra("result",result);
setResult(RESULT_OK, data);
finish();
}
}
}// end class
I tested you code in a dummy project and it was working fine at my end.. following is the code for both activity:
public class ParentActivity extends Activity {
private static final int CALLED_ACTIVITY = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent bmr = new Intent(ParentActivity.this, ChildActivity.class);
startActivityForResult(bmr, CALLED_ACTIVITY);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case CALLED_ACTIVITY:
if (resultCode == RESULT_OK) {
Toast.makeText(this, "THE RESULT-"+data.getExtras().getString("result"),
Toast.LENGTH_SHORT).show();
}
}
}
}
public class ChildActivity extends Activity {
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Returning result from child activity
Intent data = new Intent();
data.putExtra("result", "from child"
+ this.getCallingActivity().getClassName());
setResult(RESULT_OK, data);
finish();
}
}
So please post more code so that issue can be found..