Android:如何等待QR码扫描器完成,然后在主线程中继续下一个扫描。

huangapple go评论97阅读模式
英文:

Android : How to wait for QR code scanner to finish and then continue for next scan in main thread

问题

I am new to android development

I use this QR code scanner library

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_main);
  5. CodeScannerView scannerView = findViewById(R.id.scanner_view);
  6. mCodeScanner = new CodeScanner(this, scannerView);
  7. mCodeScanner.setDecodeCallback(new DecodeCallback() {
  8. @Override
  9. public void onDecoded(@NonNull final Result result) {
  10. runOnUiThread(new Runnable() {
  11. @Override
  12. public void run() {
  13. Toast.makeText(MainActivity.this, result.getText(), Toast.LENGTH_SHORT).show();
  14. }
  15. });
  16. }
  17. });
  18. scannerView.setOnClickListener(new View.OnClickListener() {
  19. @Override
  20. public void onClick(View view) {
  21. mCodeScanner.startPreview();
  22. }
  23. });
  24. }

I am able to get the QR code result in the onDecoded method for once successfully.

I need to scan QR multiple times, so I tried using a for loop. However, my requirement is to wait for the first QR to be scanned and then proceed to the next scan.

  1. for (int i = 0; i < 10; i++) {
  2. mCodeScanner.startPreview();
  3. // Wait for the first QR to finish scanning and then go for the next iteration
  4. }

I tried using wait() and notify(), but it simply crashes on the wait() statement without throwing any errors.

英文:

I am new to android development

I use this QR code scanner library

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_main);
  5. CodeScannerView scannerView = findViewById(R.id.scanner_view);
  6. mCodeScanner = new CodeScanner(this, scannerView);
  7. mCodeScanner.setDecodeCallback(new DecodeCallback() {
  8. @Override
  9. public void onDecoded(@NonNull final Result result) {
  10. runOnUiThread(new Runnable() {
  11. @Override
  12. public void run() {
  13. Toast.makeText(MainActivity.this, result.getText(), Toast.LENGTH_SHORT).show();
  14. }
  15. });
  16. }
  17. });
  18. scannerView.setOnClickListener(new View.OnClickListener() {
  19. @Override
  20. public void onClick(View view) {
  21. mCodeScanner.startPreview();
  22. }
  23. });
  24. }

I am able to get the qr code result in onDecoded method for once successfully.

I need to scan QR multiple times so I tried using for loop, but my need is wait for the first QR to be scanned, and then go for next scan.

  1. for(int i =0;i&lt;10;i++) {
  2. mCodeScanner.startPreview();
  3. // wait for first QR to finish scanning and then go for next iteration
  4. }

I tried using wait() and notify() but it simply crashes on wait() statement without any error throwing
https://stackoverflow.com/questions/14329654/how-to-notify-another-thread

答案1

得分: 0

"I need to scan QR multiple times..."
为什么不从onDecoded回调内开始每次扫描?

"I tried using wait() and notify() but it simply crashes..."
如果你想知道出了什么问题,那么你应该在你的问题中展示导致崩溃的代码。

英文:

> I need to scan QR multiple times...

Why not start each next scan from within the onDecoded callback?

> I tried using wait() and notify() but it simply crashes...

If you are asking what went wrong there, then you should show the code that crashes in your question.

huangapple
  • 本文由 发表于 2023年5月11日 20:05:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定