ZXING代码阅读器中的自定义相机视图显示检测

时间:2014-08-07 12:15:36

标签: android qr-code zxing

我正在使用ZXING QR Code Reader。它工作正常。检测代码等,但在扫描QR码时没有显示任何检测进度(如红线)。这是截图

enter image description here

我的ScanActivity是,

ScanCode.Java

public class ScanCode extends Activity implements OnClickListener {

Button btnScan;
TextView scanResult;
String text;
Bitmap bmp;
ImageView ivPic;

@Override
public void onCreate(Bundle icicle) {
    // TODO Auto-generated method stub
    super.onCreate(icicle);
    setContentView(R.layout.scan_code);
    initViews();
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v.getId() == R.id.btnScanCode) {
        Intent intent = new Intent(
                "com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent, 1);
    }
}

private void initViews() {
    scanResult = (TextView) findViewById(R.id.scanResult);
    ivPic = (ImageView) findViewById(R.id.capturedImg);
    btnScan = (Button) findViewById(R.id.btnScanCode);
    btnScan.setOnClickListener(this);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1)
        if (resultCode == Activity.RESULT_OK) {
            String contents = data.getStringExtra("SCAN_RESULT");
            String format = data.getStringExtra("SCAN_RESULT_FORMAT");
            Toast.makeText(getApplicationContext(), contents,
                    Toast.LENGTH_SHORT).show();
            scanResult.setText(contents);
        }// if result_ok
}// onActivityResult
}

1 个答案:

答案 0 :(得分:2)

我知道这个答案有点晚了但我最近在我的应用程序中使用了Zxing条码/ QR码扫描器,如果有人有问题,请按照以下步骤操作(使用Android Studio官方IDE for android)。 / p>

首先在app --> build.gradle文件

中添加依赖项
dependencies {
      compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
      compile 'com.google.zxing:core:3.2.1'
}

活动使用:

public class MainActivity extends AppCompatActivity {

    Button btnScan;

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

        btnScan = (Button) findViewById(R.id.btnScan);
        btnScan.setOnClickListener(new View.OnClickListener() {

              @Override
              public void onClick(View v) {

                   scanBarcode();

              }

        });
    }

    private void scanBarcode()
    {
        new IntentIntegrator(this).initiateScan(); 
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if(result != null) {
            if(result.getContents() == null) {
                Log.d("MainActivity", "Cancelled scan");
                Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
            } else {
                Log.d("MainActivity", "Scanned");
                Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
            }
        } 
    }

}

如果要使用自定义callback和BarCodeView。 Zxing提供此功能只需制作布局文件

<com.journeyapps.barcodescanner.CompoundBarcodeView
        android:id="@+id/barcode_scanner"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </com.journeyapps.barcodescanner.CompoundBarcodeView>

<TextView
            android:id="@+id/tvScanResult"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Scan Results will be shown here"
            android:textColor="@android:color/white"
            android:textStyle="bold"/>

在“活动”中,使用以下代码

public class MainActivity extends AppCompatActivity
{

CompoundBarcodeView barcodeView;

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

        setContentView(R.layout.activity_main);

        barcodeView = (CompoundBarcodeView) findViewById(R.id.barcode_scanner);
        barcodeView.decodeContinuous(callback);
        barcodeView.setStatusText("");

     }

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

        barcodeView.resume();

        isScanned = false;
    }

    @Override
    protected void onPause()
    {
        super.onPause();

        barcodeView.pause();
    }

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

    private BarcodeCallback callback = new BarcodeCallback()
    {
        @Override
        public void barcodeResult(BarcodeResult result)
        {
            if (result.getText() != null)
            {
                barcodeView.setStatusText(result.getText());

                tvscanResult.setText("Data found: " + result.getText());

            }

            //you can also Add preview of scanned barcode
            //ImageView imageView = (ImageView) findViewById(R.id.barcodePreview);
            //imageView.setImageBitmap(result.getBitmapWithResultPoints(Color.YELLOW));
        }

        @Override
        public void possibleResultPoints(List<ResultPoint> resultPoints)
        {
            System.out.println("Possible Result points = " + resultPoints);
        }
    };
}

来源zxing-android-embedded。希望有人从这个解决方案中获得帮助