将QR码扫描器与Zxing集成时出错
我正在尝试将QR码扫描器与Zxing的Android应用程序集成到一起。我已按照下列步骤操作:将QR码扫描器与Zxing集成时出错
我已经下载了ZXing.zip文件并将其解压。
打开ZXing项目作为android现有项目,然后进入android文件夹并打开android文件夹,并将core.jar文件包含到名为CaptureActivity的ZXing项目中。
我已将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
如果有人能够尽快澄清此问题,将不胜感激。
首先,您已复制并粘贴我们的项目。我假设你也复制了UI。正如您在这里看到的许多问题所述,并且在https://code.google.com/p/zxing/wiki/LicenseQuestions中讨论过,开源许可证不允许这样做。
其次,您已复制并粘贴AndroidManifest.xml
声明。您在我们的名称空间中声明Activity
并拦截我们的Intent
。这将干扰我们的应用程序,并且不好。删除它并创建您自己的清单。
但是第三,你似乎试图通过Intent
进行整合。这比这更容易,并且与复制并粘贴所有这些东西不正确无关。请参阅https://code.google.com/p/zxing/wiki/ScanningViaIntent
Thanx Sean.I've试图做到这一点,但它不起作用。实际上它不是很清楚,因为我是Android新手。如果你可以一步一步解释它,我将非常感激。再次感谢你的善良回应。 – Rose18 2013-04-19 04:26:42
你有什么尝试?没有什么可以解释的。添加jar文件并使用该wiki上的代码。 – 2013-04-19 07:53:41
Eclipse中的Android模拟器不支持相机。由于缺乏相机支持,模拟器是否可能无法工作?我将在运行Android设备上测试此应用程序,然后将此错误归咎于代码错误;很可能它只是一个Android模拟器/ Eclipse相关的问题。 – 2013-04-11 05:32:17