2013-04-11 154 views
0

我正在嘗試將QR碼掃描器與Zxing的Android應用程序集成到一起。我已按照下列步驟操作:將QR碼掃描器與Zxing集成時出錯

  1. 我已經下載了ZXing.zip文件並將其解壓。

  2. 打開ZXing項目作爲android現有項目,然後進入android文件夾並打開android文件夾,並將core.jar文件包含到名爲CaptureActivity的ZXing項目中。

  3. 我已將CaptureActivity項目用作名爲'QRCodeSample'的項目中的庫。

這是我MainActivity.java文件:

package com.charith.qrcodesample; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    Button b1; 
    TextView scanResult; 
    String contents; 
    public static final int REQUEST_CODDE = 1; 
    protected static final String QR_CODE_MODE = null; 

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

     b1 = (Button) findViewById(R.id.bScan); 
     b1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       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) { 
     scanResult = (TextView) findViewById(R.id.tvContent); 
     if(requestCode == 0) { 
      if(resultCode == RESULT_OK) { 
       contents = intent.getStringExtra("SCAN_RESULT"); 
       String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); 
       scanResult.setText(contents); 
      }else if(resultCode == RESULT_CANCELED){ 
       scanResult.setText("Error"); 
      } 
     } 
    } 

} 

這是我的AndroidManifest.xml文件。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.charith.qrcodesample" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="10" 
     android:targetSdkVersion="10" /> 

    <uses-permission android:name="android.permission.CAMERA" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.google.zxing.client.android.CaptureActivity" 
      android:configChanges="orientation|keyboardHidden" 
      android:screenOrientation="landscape" 
      android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
      android:windowSoftInputMode="stateAlwaysHidden" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="com.google.zxing.client.android.SCAN" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.charith.qrcodesample.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

這是我的main_activity.xml文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <Button 
     android:id="@+id/bScan" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="33dp" 
     android:text="Scan" /> 

    <TextView 
     android:id="@+id/tvContent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/bScan" 
     android:layout_below="@+id/bScan" 
     android:layout_marginTop="44dp" 
     android:text="" /> 

</RelativeLayout> 

我在eclipse中使用模擬器檢查了我的應用程序。然後,我得到以下錯誤:

The application has stopped unexpectedly. Please try again 

如果有人能夠儘快澄清此問題,將不勝感激。

+0

Eclipse中的Android模擬器不支持相機。由於缺乏相機支持,模擬器是否可能無法工作?我將在運行Android設備上測試此應用程序,然後將此錯誤歸咎於代碼錯誤;很可能它只是一個Android模擬器/ Eclipse相關的問題。 – 2013-04-11 05:32:17

回答

1

首先,您已複製並粘貼我們的項目。我假設你也複製了UI。正如您在這裏看到的許多問題所述,並且在https://code.google.com/p/zxing/wiki/LicenseQuestions中討論過,開源許可證不允許這樣做。

其次,您已複製並粘貼AndroidManifest.xml聲明。您在我們的名稱空間中聲明Activity並攔截我們的Intent。這將干擾我們的應用程序,並且不好。刪除它並創建您自己的清單。

但是第三,你似乎試圖通過Intent進行整合。這比這更容易,並且與複製並粘貼所有這些東西不正確無關。請參閱https://code.google.com/p/zxing/wiki/ScanningViaIntent

+0

Thanx Sean.I've試圖做到這一點,但它不起作用。實際上它不是很清楚,因爲我是Android新手。如果你可以一步一步解釋它,我將非常感激。再次感謝你的善良迴應。 – Rose18 2013-04-19 04:26:42

+0

你有什麼嘗試?沒有什麼可以解釋的。添加jar文件並使用該wiki上的代碼。 – 2013-04-19 07:53:41