简体   繁体   English

Intent.createChooser android二维码阅读器

[英]Intent.createChooser android QR code reader

I want the user to choose a QR reader from his installed apps. 我希望用户从他安装的应用程序中选择一个QR阅读器。 This could be done by using the Intent.createChooser. 这可以通过使用Intent.createChooser来完成。 When a picture is taken with the QR reader, the QR code should be sent back to my application. 使用QR阅读器拍摄照片时,应将QR码发送回我的应用程序。 This is what Ive tried so far: 这是迄今为止我尝试过的:

        Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            intent.putExtra("SCAN_MODE", "QR_CODE_MODE");

            String title = (String) getResources().getText(R.string.chooser_title);

            Intent chooser = Intent.createChooser(intent, title);

            startActivityForResult(chooser, CUSTOM_REQUEST_QR_SCANNER);

The scanner doens't start correctly, it only shows an sample QR code. 扫描仪无法正确启动,它只显示一个示例QR码。 I have a feeling the intent.setType("text/plain") might be wrong? 我有一种感觉,intent.setType(“text / plain”)可能是错的? What type is a QR reader? QR读卡器是什么类型的? Or how do i start a QR reader this way correctly? 或者我如何正确地以这种方式启动QR阅读器?

I also have an ActivityResult when the QR app is done: QR应用程序完成后我还有一个ActivityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == CUSTOM_REQUEST_QR_SCANNER) {

        Log.d(TAG, "QR activity complete");
                        //Successful scan
                        if (resultCode == RESULT_OK) {

Replace 更换

intent.setType("text/plain");

with

intent.setType("com.google.zxing.client.android.SCAN");

Follow This Demo and do include " android-integration.jar " in your project it has this .jar file also... and also you can download Zxing Library from Here it will use the available QR Code Scanner in your app. 按照本演示 ,在你的项目中包含“ android-integration.jar ”它还有这个.jar文件......你也可以从这里下载Zxing Library它将在你的应用程序中使用可用的QR Code Scanner。 There are also other ways just use this first you will get to know other also by R and D. 还有其他方法只是使用它,你将通过R和D了解其他。

OR 要么

Use this: 用这个:

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



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

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
                  integrator.initiateScan(IntentIntegrator.QR_CODE_TYPES);

            }
        });
    }

    @Override
      public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if (result != null) {
          String contents = result.getContents();
          if (contents != null) {
            showDialog("Found QRcode", result.toString());
          } else {
            showDialog("NO QRcode Found", "The user gave up and pressed Back");
          }
        }
    }

    private void showDialog(String title, CharSequence message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton("OK", null);
        builder.show();
      }

and include same .jar file in project properties java build path. 并在项目属性java build path中包含相同的.jar文件。 you can download .jar from here same link. 你可以从这里链接下载.jar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM