如何通过移动相机扫描条形码和二维码?

时间:2016-06-21 09:36:32

标签: android

大家好我正在构建一个Android应用程序我需要扫描条形码结果任何人都可以告诉我我该怎么做。 提前谢谢。

5 个答案:

答案 0 :(得分:1)

以下是您的完整解决方案:

 public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {

        private ZXingScannerView mScannerView;
        Button scan_code;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            scan_code = (Button) findViewById(R.id.button);

        }


        public void QrScanner(View view){

            mScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view
            setContentView(mScannerView);
            mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
            mScannerView.startCamera();         // Start camera
        }

        @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();


    }

答案 1 :(得分:0)

您可以使用任何第三方库来实现您的目标。 ZXing是您可以使用的一个很棒的库。

此代码段可用于实现此目的:

public Button.OnClickListener mScan = new Button.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent, 0);
    }
};

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            // Handle successful scan
        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel
        }
    }
}

答案 2 :(得分:0)

答案 3 :(得分:0)

如评论中所述,您需要集成zxing库。 请查看sample link

上的示例代码

答案 4 :(得分:-1)

自7.8版以来,Google Play Services SDK支持条形码扫描 在这里您可以找到所有关于的信息:
http://android-developers.blogspot.com/2015/08/barcode-detection-in-google-play.html