Android Zxing相机在反压时无法正常工作

时间:2017-07-26 07:25:06

标签: android qr-code zxing

我在ZXing二维码扫描仪上遇到问题。问题是当我从网页或新活动按下后退按钮时,扫描仪将停止工作并且无法扫描任何内容。谁能告诉我我的错误在哪里? :X 提前致谢。

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.util.Patterns;
import android.content.Intent;
import com.google.zxing.Result;
import android.net.Uri;

import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class ScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
    private ZXingScannerView zXingScannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanner);
    }

    public void scan(View view) {
        zXingScannerView = new ZXingScannerView(getApplicationContext());
        setContentView(zXingScannerView);
        zXingScannerView.setResultHandler(this);
        zXingScannerView.startCamera();
    }

    public void onPause() {
        super.onPause();
        zXingScannerView.stopCamera();
    }

    @Override
    public void handleResult(Result result) {
        Toast.makeText(getApplicationContext(), result.getText(), Toast.LENGTH_SHORT).show();
        zXingScannerView.resumeCameraPreview(this);

        if(Patterns.WEB_URL.matcher(result.getText()).matches()) {
            // Open URL
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(result.getText()));
            startActivity(browserIntent);
        }
        if(result.getText().equals("Welcome to Booth A")) {
            // Open nextActivity
            Intent Quiz1 = new Intent(this, QuizActivity.class);
            startActivity(Quiz1);
        }

    }

1 个答案:

答案 0 :(得分:0)

您需要覆盖onBackPressed中的ScannerActivity方法。你可以像

那样做
@Override
public void onBackPressed() {
    //do something
}

默认方法调用会破坏当前片段或活动。这经历了onPause,其中您通过zXingScannerView.stopCamera();停止了相机。