Android:ZXingScannerView by - me.dm7.barcodescanner:zxing:1.9无法扫描QR码。

5
我正在使用https://github.com/dm77/barcodescanner来扫描QR码。但是它根本就无法扫描QR码(handleResult从未被调用)。当我将相机对准QR码时,它也不能扫描代码。

这是我的活动。

    package education.qrexample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.google.zxing.Result;

import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{

    private ZXingScannerView mScannerView;
    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        mScannerView = new ZXingScannerView(this);   // Programmatically              initialize the scanner view
        setContentView(mScannerView);
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on resume// Set the scanner view as the content view
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();           // Stop camera on pause
    }

    @Override
    public void handleResult(Result rawResult) {
        Toast.makeText(this,rawResult.getText(),Toast.LENGTH_LONG);
     }
}

我的Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "education.qrexample"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'me.dm7.barcodescanner:zbar:1.9'
    compile 'me.dm7.barcodescanner:zxing:1.9'
}

我已经在清单文件中获取了访问相机的权限,不确定缺少什么。


你在清单文件中添加了权限 <uses-permission android:name="android.permission.CAMERA" /> 吗? - guanpj
我已经在代码中添加了权限。无论如何,我有一个工作得很好的切换视觉API。 - Sandy
2个回答

1

删除此处的 compile 'me.dm7.barcodescanner:zxing:1.9',并仅添加 implementation 'me.dm7.barcodescanner:zxing:1.9.8'

然后检查下面的 activity

public class ScannerActivity extends BaseActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view
    setContentView(mScannerView);
}
@Override
public void onResume() {
    super.onResume();
    mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
    mScannerView.startCamera();          // Start camera on resume
}

@Override
public void onPause() {
    super.onPause();
    mScannerView.stopCamera();           // Stop camera on pause
}

@Override
protected void onStop() {
    super.onStop();
    mScannerView.stopCamera();
}
@Override
public void handleResult(Result result) {
    Log.e(TAG, result.getText());
    Log.e(TAG, result.getBarcodeFormat().toString());

    String scannedText=result.getText();


}

}

请点击以下链接:this url


0

会尝试一下,它能在API 11上运行吗?它说Android要求是4.4.2+? - Sandy
使用Vision API创建了QR码扫描器示例,工作良好。现在正在将其集成到实际项目中。感谢帮助。 - Sandy
3
@BoxAndBirdie的链接已失效。 - John

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接