Qr代码扫描程序未在Android 6.0.1中运行(显示白屏)

时间:2017-03-29 17:32:42

标签: java android qr-code zxing

Qr代码扫描程序未在android 6.0.1中运行,即redmi note 4(显示白屏和相机未打开)。此外,该应用程序在我的kit-kat(4.4)和lolipop设备(5.1)上运行得非常好。请解释导致问题的原因。

package com.example.android.qrking;

import android.app.Activity;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class MainActivity extends Activity implements ZXingScannerView.ResultHandler{
    private  ZXingScannerView mScannerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mScannerView = new ZXingScannerView(this);
    }

    public void onClick(View v){

        setContentView(mScannerView);
        mScannerView.setResultHandler(this);
        mScannerView.startCamera();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mScannerView.stopCamera();
    }

    @Override
    public void handleResult(Result result){
        Log.v("handler result", result.getText());
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("scan results");
        builder.setMessage(result.getText());
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
        //uncomment to scan again
        mScannerView.resumeCameraPreview(this);
    }
}

我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android.qrking.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Scan qr code"
        android:layout_centerInParent="true"
        android:onClick="onClick"/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

使用zxing的正确方法是发送它将处理的Intent。有关如何执行此操作的详细信息,请参阅http://github.com/zxing/zxing/wiki/Scanning-Via-Intent