Zxing Qr代码扫描程序不适用于所有设备

时间:2016-05-14 08:05:26

标签: android zxing

当我在Asus_Z00AD(ver-5.0)中运行应用程序时,二维码扫描功能完美无缺,但当我在任何其他手机上运行时,如moto-g(ver-6.0),它显示一个空白屏幕 别无其他.. 怎么办.. plz建议我..

我的扫描仪活动

            package hashrail.com.aggregate.activity;

            import android.app.Activity;
            import android.content.Intent;
            import android.graphics.PointF;
            import android.os.Bundle;
            import android.support.design.widget.Snackbar;
            import android.support.v7.app.AlertDialog;
            import android.support.v7.app.AppCompatActivity;
            import android.support.v7.widget.Toolbar;
            import android.util.Log;
            import android.util.SparseArray;
            import android.view.KeyEvent;
            import android.view.MenuItem;
            import android.view.SurfaceHolder;
            import android.view.SurfaceView;
            import android.view.View;
            import android.widget.ImageView;
            import android.widget.LinearLayout;
            import android.widget.TextView;
            import android.widget.Toast;

            import com.google.zxing.Result;

            import java.io.IOException;

            import hashrail.com.aggregate.R;
            import me.dm7.barcodescanner.zxing.ZXingScannerView;


            public class BarcodeScanFragment extends AppCompatActivity implements ZXingScannerView.ResultHandler {
                Toolbar toolbar;
                LinearLayout rlpditem;
                private String codeFormat, codeContent;
                private TextView formatTxt, contentTxt;
                ImageView imgBarcode;
                LinearLayout llQRrcode;
                private ZXingScannerView mScannerView;


                public BarcodeScanFragment() {
                    // Required empty public constructor
                }

                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.fragment_barcode_scan);
                    toolbar = (Toolbar) findViewById(R.id.toolbar1);
                    llQRrcode = (LinearLayout) findViewById(R.id.llQRrcode);
                    formatTxt = (TextView) findViewById(R.id.scan_format);
                    contentTxt = (TextView) findViewById(R.id.scan_content);
                    imgBarcode = (ImageView) findViewById(R.id.scssan_button);

                    setSupportActionBar(toolbar);
                    getSupportActionBar().setDisplayShowHomeEnabled(true);
                    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                    setTitle("Barcode Scanner");

                   /* imgBarcode.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            setContentView(mScannerView);

                            mScannerView.startCamera();
                        }
                    });*/
                }

                @Override
                public boolean onOptionsItemSelected(MenuItem item) {

                    switch (item.getItemId()) {
                        case android.R.id.home:
                            onBackPressed();
                            return true;
                        default:
                            return super.onOptionsItemSelected(item);
                    }
                }
                public void QrScanner(View view) {
                         // Start camera
                    mScannerView = new ZXingScannerView(BarcodeScanFragment.this);   // Programmatically initialize the scanner view
                    mScannerView.setResultHandler(BarcodeScanFragment.this); // Register ourselves as a handler for scan results.
                    setContentView(mScannerView);
                    mScannerView.startCamera();
                }

               /* @Override
                public void onPause() {
                    super.onPause();
                    mScannerView.stopCamera ();   // Stop camera on pause
                }
            */
                @Override
                public void handleResult(Result rawResult) {
            // Do something with the result here

                    Log.e("handler", rawResult.getText()); // Prints scan results
                    Log.e("handler", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode)

                    // show the scanner result into dialog box.
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("Scan Result");
                    builder.setMessage(rawResult.getText());
                    AlertDialog alert1 = builder.create();
                    alert1.show();

                    // If you would like to resume scanning, call this method below:
                     mScannerView.resumeCameraPreview(this);
                }
            }

Gradle文件

                apply plugin: 'com.android.application'

                android {
                    compileSdkVersion 23
                    buildToolsVersion "23.0.3"

                    defaultConfig {
                        applicationId "hashrail.com.aggregate"
                        minSdkVersion 11
                        targetSdkVersion 23
                        versionCode 1
                        versionName "1.0"
                        useLibrary 'org.apache.http.legacy'
                    }
                    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:23.3.0'
                    compile 'com.android.support:design:23.0.1'
                    compile 'com.android.support:cardview-v7:23.0.1'
                    compile 'com.android.support:palette-v7:23.0.1'
                    compile 'com.android.support:recyclerview-v7:23.0.1'
                    compile 'com.github.lzyzsd:circleprogress:1.1.0@aar'

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


                    //compile 'com.dlazaro66.qrcodereaderview:qrcodereaderview:1.0.0'
                    /*compile 'com.journeyapps:zxing-android-embedded:2.0.1@aar'
                    compile 'com.journeyapps:zxing-android-legacy:2.0.1@aar'
                    compile 'com.journeyapps:zxing-android-integration:2.0.1@aar'
                    compile 'com.google.zxing:core:3.0.1'*/

                }

3 个答案:

答案 0 :(得分:0)

我正在使用相同的库,它在版本6.0和7.0设备上工作正常,但是对于较低版本的设备有一些问题。我也在moto g4上测试它在g4和g5上工作正常。

你的代码完全正确我正在做同样的事情,做一件事来处理它进行扫描为扫描仪视图制作单独的活动并从handleResult()方法获取你想要的数据,因为空白屏问题导致可能是由于to setcontentView覆盖扫描器view.try再次这样,如果你需要代码,那么我也可以共享代码......

答案 1 :(得分:0)

请检查此演示。

public class ContinuousCaptureActivity extends Activity implements ZxingScannerView.TorchListener, View.OnClickListener
{
    // PICK_PHOTO_CODE is a constant integer
    public final static int PICK_IMAGE_REQUEST_CODE = 1046;
    private static final String TAG = ContinuousCaptureActivity.class.getSimpleName();
    private static final int READ_EXTERNAL_STORAGE_REQUEST_CODE = 1051;
    private ZxingScannerView barcodeView;
    private BeepManager beepManager;
    private String lastText;
    private boolean isTorchEnabled;

    private BarcodeCallback callback = new BarcodeCallback() {
        @Override
        public void barcodeResult(BarcodeResult result) {
            if (result.getText() == null || result.getText().equals(lastText)) {
                // Prevent duplicate scans
                return;
            }
            lastText = result.getText();
            barcodeView.setStatusText(result.getText());
            beepManager.playBeepSoundAndVibrate();
        }

        @Override
        public void possibleResultPoints(List<ResultPoint> resultPoints) {
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.continuous_scan);
        beepManager = new BeepManager(this);
        barcodeView = findViewById(R.id.barcode_scanner);

        barcodeView.setFullScreenScan();
        barcodeView.getCameraSettings().setAutoFocusEnabled(true);
        barcodeView.getCameraSettings().setScanInverted(true);
        barcodeView.getCameraSettings().setExposureEnabled(true);

        Collection<BarcodeFormat> formats = Arrays.asList(BarcodeFormat.QR_CODE, BarcodeFormat.CODE_39);
        barcodeView.getBarcodeView().setDecoderFactory(new DefaultDecoderFactory(formats));
        barcodeView.initializeFromIntent(getIntent());
        barcodeView.decodeContinuous(callback);
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.flashlight:

                toggleTorch();
                break;

            case R.id.galleryPicker:

                pickImage();
                break;
        }
    }

    private void pickImage() {

        if (ActivityCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            // Create intent for picking a photo from the gallery
            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            if (intent.resolveActivity(getPackageManager()) != null) {

                intent.setType("image/*");
                intent.putExtra("crop", "true");
                intent.putExtra("scale", true);
                intent.putExtra("aspectX", 16);
                intent.putExtra("aspectY", 9);
                startActivityForResult(intent, PICK_IMAGE_REQUEST_CODE);
            }
        } else {
            ActivityCompat.requestPermissions(
                    this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    READ_EXTERNAL_STORAGE_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case READ_EXTERNAL_STORAGE_REQUEST_CODE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // pick image after request permission success
                    pickImage();
                }
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == PICK_IMAGE_REQUEST_CODE) {
            if (resultCode != Activity.RESULT_OK) {
                return;
            }
            if (data != null) {
                Uri photoUri = data.getData();
                // Do something with the photo based on Uri
                Bitmap selectedBitmap = null;
                try {
                    selectedBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), photoUri);

                    AnimatedViewFinder viewFinder = barcodeView.getViewFinder();
                    Bitmap resizedBitmap = BitMapScaler.scaleToFill(selectedBitmap, viewFinder.getWidth(), viewFinder.getWidth());
                    barcodeView.scanBitmap(resizedBitmap);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void toggleTorch() {
        if (isTorchEnabled) {
            barcodeView.setTorchOff();
        } else {
            barcodeView.setTorchOn();
        }
    }

    private void toggleTorchListener(boolean enableListener) {
        if (!ZxingUtils.hasFlash(getApplicationContext())) {
            barcodeView.getFlashlight().setVisibility(View.GONE);
        } else {
            if (true) {
                barcodeView.getFlashlight().setOnClickListener(this);
                barcodeView.setTorchListener(this);
            } else {
                barcodeView.getFlashlight().setOnClickListener(null);
                barcodeView.setTorchListener(null);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        barcodeView.resume();
        toggleTorchListener(true);
        barcodeView.getGalleryPicker().setOnClickListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        barcodeView.pause();
        toggleTorchListener(false);
        barcodeView.getGalleryPicker().setOnClickListener(null);
    }

    @Override
    public void onTorchOn() {
        isTorchEnabled = true;
        barcodeView.getFlashlight().setImageResource(R.drawable.vector_flash_on);
    }

    @Override
    public void onTorchOff() {
        isTorchEnabled = false;
        barcodeView.getFlashlight().setImageResource(R.drawable.vector_flash_off);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return barcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }
}

https://github.com/tiger1990/QrScannerDemo

答案 2 :(得分:0)

这是ZXingScannerView新版本1.9中的问题。用户旧版本1.7.2,则可以在所有设备上正常工作。

//在build.gradle中使用此版本 实施'me.dm7.barcodescanner:zxing:1.7.2'