2013-04-05 44 views
3

我想讓用戶從他安裝的應用程序中選擇一個QR閱讀器。這可以通過使用Intent.createChooser完成。使用QR閱讀器拍攝照片時,應將QR碼發送回我的應用程序。這是我迄今爲止所嘗試的:Intent.createChooser android二維碼讀取器

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

掃描儀dons't無法正確啓動,它只顯示示例QR碼。我有一種感覺intent.setType(「文本/平原」)可能是錯的?什麼類型的QR讀碼器?或者我該如何正確啓動QR閱讀器?

我也有當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) { 
+0

我不明白'SEND''text/plain'是如何解決的,因爲qr_code請求 – njzk2 2013-04-05 12:21:54

+0

您是否使用任何類似於zxing或zbar的庫? – Shiv 2013-04-05 12:33:04

+0

你說得對,我應該使用zxing。 – rtc11 2013-04-05 12:41:35

回答

3

更換

intent.setType("text/plain"); 

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

按照此Demo和不包括「Android的整合.jar「在你的項目中也有這個.jar文件...也可以從Here下載Zxing Library,它會在您的應用程序中使用可用的QR碼掃描儀。也有其他的方法只是用這個首先你會得到也R和D.知道其他

OR

使用此:

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

,包括在同一個項目的.jar文件屬性java構建路徑。你可以從here下載.jar同樣的鏈接。