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

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

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CodeScannerView scannerView = findViewById(R.id.scanner_view);
        mCodeScanner = new CodeScanner(this, scannerView);
        mCodeScanner.setDecodeCallback(new DecodeCallback() {
            @Override
            public void onDecoded(@NonNull final Result result) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, result.getText(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        scannerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCodeScanner.startPreview();
            }
        });       
    }

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.

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

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CodeScannerView scannerView = findViewById(R.id.scanner_view);
        mCodeScanner = new CodeScanner(this, scannerView);
        mCodeScanner.setDecodeCallback(new DecodeCallback() {
            @Override
            public void onDecoded(@NonNull final Result result) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, result.getText(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        scannerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCodeScanner.startPreview();
            }
        });       
    }

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.

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

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:

确定