Zxing 二维码扫描仪不适用于所有设备

Posted

技术标签:

【中文标题】Zxing 二维码扫描仪不适用于所有设备【英文标题】:Zxing Qr code scanner does not working on all Devices 【发布时间】:2016-09-10 11:48:18 【问题描述】:

当我在 Asus_Z00AD(ver-5.0) 中运行应用程序时,QR 码扫描效果很好,但是当我在 moto-g(ver-6.0) 等任何其他手机上运行它时,它会显示一个空白屏幕 没有别的了。。 怎么办..请建议我..

我的扫描仪活动

            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'*/

                

【问题讨论】:

【参考方案1】:

我正在使用相同的库,它在 6.0 和 7.0 版设备上运行良好,但在低版本设备上存在一些问题。我还在 moto g4 上对其进行了测试,在 g4 和 g5 上运行良好。

您的代码完全正确,我正在做同样的事情,做一件事来处理它以进行扫描为扫描仪视图创建单独的活动,并从 handleResult() 方法中获取您想要的数据,因为可能是由于导致空白屏幕问题为扫描仪视图再次设置内容视图覆盖。如果您需要代码,请尝试这种方式,然后我也可以共享代码...

【讨论】:

可以分享给我吗?无论使用什么设备,我都会遇到白屏问题【参考方案2】:

请查看此演示。

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

【讨论】:

【参考方案3】:

这是ZXingScannerView 1.9新版本的问题。用户使用旧版本 1.7.2 那么它可以在所有设备上正常工作。

//在你的 build.gradle 中使用这个版本 实现'me.dm7.barcodescanner:zxing:1.7.2'

【讨论】:

一切还如预期吗?

以上是关于Zxing 二维码扫描仪不适用于所有设备的主要内容,如果未能解决你的问题,请参考以下文章

在WPF中开启摄像头扫描二维码(Media+Zxing)

运行Google 官方zxing二维码扫描器

android中zxing扫描条码没有声音

android开发 如何实现扫描本地二维码图片

android zxing扫码 扫描二维码有对焦的黄点怎么去除

Android实战——Zxing实现二维码扫描