2012-04-10 56 views
0

我在修改現有的應用程序。該應用程序通過Java類和包使用「Zxing的條形碼掃描儀」。在Android上使用「Zxing的條形碼掃描儀」應用程序

我的項目包括那些包:

com.google.zxing com.google.zxing.integration com.google.zxing.integration.android

我有一類像這樣的代碼:

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 

import com.google.zxing.integration.android.IntentIntegrator; 
import com.google.zxing.integration.android.IntentResult; 

public class QRdecoderActivity extends Activity { 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // temp = this; 

     IntentIntegrator.initiateScan(this); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     switch(requestCode) { 

      case IntentIntegrator.REQUEST_CODE: { 

       if (resultCode != RESULT_CANCELED) { 

        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); 

        if (scanResult != null) { 
         String upc = scanResult.getContents(); 

         Toast.makeText(this, "Contents : " + upc, Toast.LENGTH_LONG).show(); 

        } 

       } 
       finish();    

       break; 
      } 
     } 
    } 
} 

一切工作正常,但是當我開始測試過程時,我發現我需要安裝「條碼掃描儀」應用程序。

這就是對的?

我以爲我不需要,如果它在我的項目中使用Java類。

我如何檢查應用程序是否已安裝?以及我如何才能訪問「Google Play」並將其從我的代碼中顯示給用戶以供下載?

回答

5

這已經在之前討論過了,在Zxing網站上有很好的記錄。雖然您可以將源代碼集成到您的應用中,但您也可以通過意向進行掃描。

從您發佈的內容看,它的源代碼已經集成到應用程序中,因此您不需要安裝它(因爲所有類都應該在那裏)。

如果系統提示您安裝條形碼掃描器應用程序,它聽起來像正在使用意向掃描。最終的結果是,你有兩種方法的雞尾酒,通過意圖掃描是使用的方法。

我個人更喜歡通過意圖進行掃描。這裏記錄在這裏:http://code.google.com/p/zxing/wiki/ScanningViaIntent

我的推理是你的應用程序獨立於條碼掃描器。任何由新條碼標準或一般錯誤修正/改進引起的更新都會立即提供給最終用戶(作爲Google Play的更新),因爲他們無需等待您的應用程序集成任何更新的源代碼。此外,如果您打算爲其添加價值,則只能使用Zxing的來源,這是鼓舞人心的。

我該如何檢查應用程序是否已安裝?以及如何才能訪問 「Google Play」並將其從我的代碼中顯示給用戶以供下載?

Zxing提供的類優雅地處理用戶意圖和條碼掃描儀應用程序未安裝的情況。它會將用戶直接帶到Google Play上的應用。你可以在http://code.google.com/p/zxing/source/browse/trunk/android-integration/src/com/google/zxing/integration/android/IntentIntegrator.java找到它。

一旦你的類,你只需要調用如下:

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

,然後添加到您的活動

public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); 
    if (scanResult != null) { 
    // handle scan result 
    } 
    // else continue with any other code you need in the method 
    ... 
} 
+0

非常感謝你。我有一個過時的IntentIntegrator版本或定製版本。 我從鏈接複製代碼,它工作正常! 真的很感謝 – 2012-04-11 14:56:14

相關問題