2013-02-17 60 views
1

我使用this answer創建了一個獨立的Android庫項目,其中包含ZXing源代碼(ZXing v2.1)。它編譯得很好,如果我運行CaptureActivity,我可以按照預期讀取QR碼。通過集成到您的應用中的ZXing獲取QR掃描結果

我有另一個Android項目,我想從中拉這個庫。我已正確設置了該庫關係。

我遇到的問題是,如何通過IntentIntegrator(提到的here)啓動我的本地ZXing掃描器副本。

我嘗試修改IntentIntegrator.initiateScan()方法以使用我的本地副本CaptureActivity,並正確加載QR掃描儀。 但是,一旦QR碼被掃描,QR信息就會顯示在屏幕上,而不是將結果發送回onActivityResult中的調用活動。

如何將QR掃描結果發送到我的通話活動的onActivityResult?

僅供參考,這裏是我改變了IntentIntegrator.initiateScan()方法:

public AlertDialog initiateScan(Activity act, Collection<String> desiredBarcodeFormats) {  

    //Hardcoding name of activity to call --> is this where I've gone wrong? 
    Intent intentScan = new Intent(act, CaptureActivity.class); 

    intentScan.addCategory(Intent.CATEGORY_DEFAULT); 

    // check which types of codes to scan for 
    if (desiredBarcodeFormats != null) { 
     // set the desired barcode types 
     StringBuilder joinedByComma = new StringBuilder(); 
     for (String format : desiredBarcodeFormats) { 
     if (joinedByComma.length() > 0) { 
      joinedByComma.append(','); 
     } 
     joinedByComma.append(format); 
     } 
     intentScan.putExtra("SCAN_FORMATS", joinedByComma.toString()); 
    } 


//Commented this out because it didn't seem to find my class... 

// String targetAppPackage = findTargetAppPackage(intentScan); 
// if (targetAppPackage == null) { 
//  return showDownloadDialog(); 
// } 
//  
//  
// intentScan.setPackage(targetAppPackage); 
    intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 
    attachMoreExtras(intentScan); 
    startActivityForResult(intentScan, REQUEST_CODE); 
    return null; 
    } 

而且我開始喜歡這個掃描:

IntentIntegrator integrator = new IntentIntegrator(getActivity()); 
integrator.initiateScan(getActivity()); 

我覺得像我在這裏錯過了一些簡單的東西,任何正確的方向都會很棒。

SOLUTION

這裏是結束了工作。

IntentIntegrator integrator = new IntentIntegrator(getActivity()); 
integrator.initiateScan(getActivity()); 

但initiateScan方法現在看起來是這樣的:

public AlertDialog initiateScan(Activity act, Collection<String> desiredBarcodeFormats) 
    { 

    Intent intentScan = new Intent(BS_PACKAGE + ".SCAN"); 

    intentScan.addCategory(Intent.CATEGORY_DEFAULT); 

    // check which types of codes to scan for 
    if (desiredBarcodeFormats != null) { 
     // set the desired barcode types 
     StringBuilder joinedByComma = new StringBuilder(); 
     for (String format : desiredBarcodeFormats) { 
     if (joinedByComma.length() > 0) { 
      joinedByComma.append(','); 
     } 
     joinedByComma.append(format); 
     } 
     intentScan.putExtra("SCAN_FORMATS", joinedByComma.toString()); 
    } 

    //THIS WAS THE KEY 
    setSingleTargetApplication(act.getPackageName()); 

    String targetAppPackage = findTargetAppPackage(intentScan); 
    if (targetAppPackage == null) { 
     return showDownloadDialog(); 
    } 

    intentScan.setPackage(targetAppPackage); 
    intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 
    attachMoreExtras(intentScan); 
    act.startActivityForResult(intentScan, REQUEST_CODE); 
    return null; 
    } 

重要的事情是確保BS_PACKAGE指向CaptureActivity包,你所說的「行爲我仍然調用它以同樣的方式.startActivityForResult ...「,而不僅僅是」startActivityForResult ...「,並且您將調用setSingleTargetApplication以及將調用掃描器的應用程序的包名稱。

回答

1

試圖改變線路startActivityForResult(intentScan, REQUEST_CODE);

act.startActivityForResult(intentScan, REQUEST_CODE);

你不需要評論包含findTargetAppPackage的代碼,只需調用setSingleTargetApplication()(設置目標應用程序的包,如果你是使用這個唯一的應用程序庫)

+1

謝謝iTech,這兩件事情是最大的問題。我還必須退出對「intentScan」變量中的Activity進行硬編碼,而是使用原始代碼:Intent intentScan = new Intent(BS_PACKAGE +「.SCAN」);並且必須確保BS_PACKAGE指向CaptureActivity所在的包。謝謝! – DiscDev 2013-02-17 21:30:32

+1

不客氣,很高興它的工作! – iTech 2013-02-17 21:32:15