QR码扫描仪无法扫描

时间:2018-08-14 18:28:27

标签: java android android-camera barcode-scanner

尝试制作一个Android QR码扫描仪...使用了一些在线教程作为参考,并且看起来是一样的..它在运行4.3 android的三星手机上打开了相机,相机保持打开状态,但没有根本检测不到QR码...
这是Scanbarcodeactivity类,不是主要活动,mainactivity仅有一个按钮,点击该按钮后,将加载此Scanbarcodeactivity类,显示相机预览并应扫描检测到的第一个QR码,然后将QR码返回到主要活动..试图解决整个一周的问题...所以我在做错什么,导致它无法检测到条形码?

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;


import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;

import java.io.IOException;

public class ScanBarcodeActivity extends Activity {
SurfaceView cameraPreview;

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

    cameraPreview = (SurfaceView) findViewById(R.id.camera_preview);
    createCameraSource();
}

private void createCameraSource() {
    BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(getApplicationContext())
            .setBarcodeFormats(Barcode.QR_CODE).build();



    final CameraSource cameraSource = new CameraSource.Builder(this, barcodeDetector)
            .setAutoFocusEnabled(true)
            .setRequestedPreviewSize(1600, 1024)
            .build();

    cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            if (ActivityCompat.checkSelfPermission(ScanBarcodeActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

                return;
            }
            try {
                cameraSource.start(cameraPreview.getHolder());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

        }

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



    barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {

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

                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                intent.putExtra("barcode",barcodes.valueAt(0).rawValue);
                setResult(CommonStatusCodes.SUCCESS, intent);
                finish();
            }
        }

        @Override
        public void release() {

        }

    });





}

}

1 个答案:

答案 0 :(得分:0)

使用此库:

https://github.com/dm77/barcodescanner

等级:

compile 'me.dm7.barcodescanner:zxing:1.9.8'