QR检测不起作用(Google API)

时间:2017-08-02 07:23:37

标签: android google-play-services qr-code

我有一项活动,显示我的相机正在看到的内容并检测到QR码。我可以看到我的相机看到了什么,但是当我看到QR时我无法得到响应。我认为它并没有进入" recieveDetections"方法。我有这个权限和关于Manifest的这个元数据:

清单:

<uses-permission android:name="android.permission.CAMERA" />
<meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="barcode"/>

我的渐变有这种依赖关系:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services-vision:11.0.4'
}

我的MaintActivity是:

public class MainActivity extends AppCompatActivity {


BarcodeDetector barcodeDetector;
CameraSource cameraSource;
SurfaceView cameraView;

TextView qrValue;

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

    qrValue = (TextView)findViewById(R.id.textView);

    requestPermissions(new String[]{android.Manifest.permission.CAMERA}, 1);

    // creo el detector qr
    barcodeDetector = new BarcodeDetector.Builder(MainActivity.this).setBarcodeFormats(Barcode.QR_CODE).build();

    // creo la camara fuente
    cameraSource = new CameraSource.Builder(MainActivity.this, barcodeDetector).setRequestedPreviewSize(640, 480).build();

    cameraView = (SurfaceView)findViewById(R.id.camera_view);

    // listener de ciclo de vida de la camara
    cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {

            // verifico si el usuario dio los permisos para la camara
            if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                try {
                    cameraSource.start(cameraView.getHolder());
                } catch (IOException ie) {
                    Log.e("CAMERA SOURCE", ie.getMessage());
                }
            } else {
                Toast.makeText(MainActivity.this, getResources().getString(R.string.error_permisos), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            cameraSource.stop();
        }
    });

    // preparo el detector de QR
    barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
        @Override
        public void release() {
        }

        @Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {
            final SparseArray<Barcode> barcodes = detections.getDetectedItems();

            if (barcodes.size() != 0) {
                String valorQR = barcodes.valueAt(0).displayValue.toString();
                // hacer algo
                qrValue.setText(valorQR);
            }

            barcodeDetector.release();
        }
    });
}
}

有人可以帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的 barcodeDetector 不知道要检测什么。您需要从相机或预览中捕获位图或字节数组。

建议您阅读githubtutorial

中的示例代码

导入barcode-reader项目,并从此OCR tutorial

中学习