我想将zxing扫描仪集成到我的应用程序中而无需外部应用程序(来自Play商店的zxing扫描仪).这是我的代码
Button scan = (Button) findViewById(R.id.scan_button);
scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage("com.mypackage.app");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, SCANNER_REQUEST_CODE);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == SCANNER_REQUEST_CODE) {
// Handle scan intent
if (resultCode == Activity.RESULT_OK) {
// Handle successful scan
String contents = intent.getStringExtra("SCAN_RESULT");
String formatName = intent.getStringExtra("SCAN_RESULT_FORMAT");
byte[] rawBytes = intent.getByteArrayExtra("SCAN_RESULT_BYTES");
int intentOrientation = intent.getIntExtra("SCAN_RESULT_ORIENTATION", Integer.MIN_VALUE);
Integer orientation = (intentOrientation == Integer.MIN_VALUE) ? null : intentOrientation;
String errorCorrectionLevel = intent.getStringExtra("SCAN_RESULT_ERROR_CORRECTION_LEVEL");
} else if (resultCode == Activity.RESULT_CANCELED) {
// Handle cancel
}
} else {
// Handle other intents
}
}
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml中
<activity android:name="com.google.zxing.client.android.CaptureActivity"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
我的Android Studio说无法解析符号:.android.CaptureActivity如果我运行它,就会发生错误
java.lang.ClassNotFoundException:未找到类"com.google.zxing.client.android.CaptureActivity"
发生了什么?
Lir*_*hen 46
虽然已经得到了解答,但对于那些想要在不使用任何自动安装方法(如ANT)的情况下将ZXing用作库的人来说,我觉得这是一个深入的解决方案.
ZXing是一个开源项目允许(如ZXing网站所述)您将其功能用作项目库而不是强制在用户设备上安装第三方应用程序,在我看来,这是大多数项目的首选.
重要的是要注意,这种方式是创作者可以接受的,并且不会违反他们的任何使用条款,因为我们不会复制他们的代码,只是将其用作我们项目的库.
一般过程是这样的:
这是一个使用Eclipse的完全详细的过程(也可以很容易地为Studio实现):
下载ZXing源代码和核心文件
将ZXing项目导入Eclipse
您现在应该在工作区中看到捕获活动.根据您所拥有的ZXing版本,您可能需要将core.jar文件分配给ZXing项目,以检查您的版本是否是这种情况,打开captureActivity项目的libs文件夹并检查core.jar文件是否存在,如果不存在比你必须自己添加,否则跳到下一章.
5.1.要添加core.jar文件,请右键单击Eclipse工作区中的captureActivity项目,然后选择"构建路径">"配置构建路径"
5.2.从侧面菜单中选择Java Build Path,然后单击Libraries选项卡.
5.3.单击"添加外部JAR"并导航到最初导出ZXing zip的位置.
5.4.输入核心文件夹并选择core.jar(名称可能因ZXing版本而异)并单击"打开"
5.5.您现在应该在构建路径列表中看到core.jar,单击"Order and Export"选项卡并检查core.jar
将ZXing定义为库并修复代码
现在根据您所拥有的ZXing版本,您可能会看到Eclipse标记了一些包含错误的java文件,如果是这种情况,您将不得不修复代码以匹配android java版本,否则只需转到下一章
8.1.在编辑器中打开每个破碎的java文件,用一串If Else语句替换损坏的Switch语句,这是一个我知道的烦人部分.
将ZXing添加为您自己项目的库
添加应用程序所需的ZXing方法
这部分不是真正的指南,而是我发现迄今为止我发现对我自己的需求有用的东西,我主要使用的两种方法是:
运行扫描仪并捕获QR码:
Intent intent = new Intent(getApplicationContext(),CaptureActivity.class);
intent.setAction("com.google.zxing.client.android.SCAN");
intent.putExtra("SAVE_HISTORY", false);
startActivityForResult(intent, 0);
Run Code Online (Sandbox Code Playgroud)
从onActivityResult()中的扫描中检索结果:
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
Log.d(TAG, "contents: " + contents);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
Log.d(TAG, "RESULT_CANCELED");
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,就像Android捕获活动中的任何其他活动一样,也需要在项目清单中定义,同时请注意,您必须授予项目使用相机的权限,如下所示:
<uses-permission android:name="android.permission.CAMERA" />
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
android:screenOrientation="landscape" >
</activity>
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助其他人节省一些好时间的研究.
作为额外的奖励,我建议阅读:https: //biocram.wordpress.com/2013/06/11/integrate-zxing-as-a-library-inside-an-android-project/
小智 39
由于我到目前为止找到的所有答案都是基于Eclipse的,而且截至目前已经过时,我正在添加我的答案,将ZXing添加为Android Studio(2.1.2)上的独立库.
我把已编译的项目放在Github上https://github.com/tarun0/ZXing-Standalone-library只需zxing_standalone在项目中添加模块就可以了.有关详细信息或要为较新版本编译它,请继续阅读.
这可能看起来很长,但是一旦你完成它,你会发现它非常容易和简短.
android目录.因此,如果您恰好拥有此文件夹,则无需下载大小约为126MB的整个分支.core-x.x.x.jar从http://repo1.maven.org/maven2/com/google/zxing/core/3.2.1/下载最新版本Project从Project Explorer(参考图片)New> Module> Import Gradle Project.现在,android从下载的源代码中选择目录(在步骤1中).(参见下面的绝对新手).(可选)更改名称并设置导入的默认设置.

libs并将core.jar步骤2中下载的文件放在此文件夹中.然后Project Structure从文件菜单中打开并将此core.jar文件添加为依赖项.
,因为这是一个库,现在不应该在启动器上(如果你不删除它,你将最终有两个启动器活动).
在项目的build.gradle文件中,在dependencies块中添加此行在compile project (':android')此处,android在步骤4中导入项目时替换您选择的名称.同步并清理项目.你会在switch陈述中看到一些错误.单击这些开关案例,然后选择replace with ifAndroid Studio提供的选项选项.
而已.现在,您可以在自己的应用程序中使用ZXing库.:)
要使用添加的库,只需使用Intents上面第一个答案中所述的内容(仅复制相同的代码):
扫描时(如点击按钮),发送意图:
Intent intent = new Intent(getApplicationContext(),CaptureActivity.class);
intent.setAction("com.google.zxing.client.android.SCAN");
intent.putExtra("SAVE_HISTORY", false);
startActivityForResult(intent, 0);
Run Code Online (Sandbox Code Playgroud)
然后,在OnActivityResult:
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
Log.d(TAG, "contents: " + contents);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
Log.d(TAG, "RESULT_CANCELED");
}
}
Run Code Online (Sandbox Code Playgroud)
我试图尽可能地描述.我希望人们觉得它很有用.
请阅读此代码的作者之一,将代码复制到您自己的应用程序中:https://stackoverflow.com/a/9942761
参考文献:https : //stackoverflow.com/a/29818279 /sf/answers/2097225301/ 以及其他一些博客/ SO答案.
Ish*_*eur 16
我已经很晚了,但是我想回复一下,以便其他人以后再帮忙.这并不是说上述方法和解决方案都是错误的,它只是一个额外的信息,因此,对于开发者他/她将选择更好的方式.拥有一条路比拥有一条路还好.
那么,让我们开始我们的gradle并添加
compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
compile 'com.google.zxing:core:3.2.1'
Run Code Online (Sandbox Code Playgroud)
像调用模块一样(例如:点击按钮):
IntentIntegrator integrator = new IntentIntegrator(Home.this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan Code");
integrator.setCameraId(0);
integrator.setBeepEnabled(true);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
Run Code Online (Sandbox Code Playgroud)
获得如下结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(intentResult != null) {
if(intentResult.getContents() == null) {
Log.d("MainActivity", "Cancelled");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + intentResult.getContents(), Toast.LENGTH_LONG).show();
}
}
Run Code Online (Sandbox Code Playgroud)
}
有关详细信息,请查看链接 https://github.com/pethoalpar/ZxingExample
| 归档时间: |
|
| 查看次数: |
80004 次 |
| 最近记录: |