将ZXing库直接集成到我的Android应用程序中
-
23-10-2019 - |
题
我只是在绝望中写这篇文章:)我被分配给Android 1.6手机制作独立的条形码扫描仪(作为概念证明)。
为此,我发现了Zxing库。
我已经搜索过,在Stackoverflow上阅读相关主题使用了常见的态度等。似乎没有任何帮助,我只是无法在这个Mentale封锁上打个洞:/
我知道可以使用LIB并创建自己的独立条形码扫描仪。我已经读到,使用Zxing人提供的“条形码扫描仪”是迄今为止最简单的解决方案(通过意图)。不幸的是,这不是一个选择,并且需要独立的应用程序。
因此总结我的问题:
- 如何通过Eclipse将Zxing源液体集成到我的Android代码项目中?
- 集成...如何利用LIB,“加载”扫描功能?
- 几乎选择了步骤指南,因为我刚刚开始在Eclipse工作。
我试图从ZXing源文件夹中使我的代码项目取决于Android文件夹。当我这样做时,会出现一个稍有错误的错误,主要是关于'org.apache'(??)
我只是无法弄清楚...所以一些提示将是最有帮助的。
提前,谢谢:)
解决方案
更新! - 解决 +指南
我已经设法弄清楚了:)在下面您可以阅读分步指南,以便它可以帮助他人解决与我相同的问题的帮助;)
- 安装Apache Ant-((查看此YouTube视频以获取配置帮助)
- 从zxing主页下载ZXing源并提取它
- 使用Windows Commandline(Run-> CMD)导航到已下载的根目录
zxing src
. - 在命令行窗口中 - 类型
ant -f core/build.xml
按Enter并让Apache起作用它是魔术[有问题?] - 基于您刚提取的目录中的Android文件夹,输入Eclipse->新的Android项目
- 右键单击项目文件夹 - >属性 - > java构建路径 - >库 - >添加外部罐子...
- 导航到新提取的文件夹并打开核心目录并选择
core.jar
...点击输入!
现在,您只需要在翻译和androidManifest.xml文件中纠正一些错误:)现在您可以愉快地编译,现在您将基于ZXing源具有一个可行的独立条形码扫描仪应用程序;)
愉快的编码人员 - 希望它能帮助别人:)
其他提示
这是有关如何使用ZXing库生成和显示QR码的逐步指南,而无需安装第三方应用程序。 笔记: 您不必使用ANT或任何其他构建工具来构建ZXing。文件 core.jar
可在已发布的Zip档案中使用(如下阅读)。
- 下载 ZXing的最新版本. -- (
ZXing-*.zip
) - 提取这个拉链档案并找到
core.jar
在下面core/
目录。 - 如果您使用的是Eclipse IDE,请拖放
core.jar
到libs
您的Android项目目录。当被问到时,选择 复制. - 复制下面给出的两个类(
Contents.java
&QRCodeEncoder.java
)到您的Android项目的主要包装。 - 创建一个
ImageView
如果您还没有活动,则会在活动中显示生成的QR码。下面给出了一个示例: - 使用下面的代码代码段以位于位图格式生成QR代码,并将其显示在
ImageView
.
这是一个 ImageView
元素要添加到您的活动布局XML文件:
<ImageView
android:id="@+id/qrCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"/>
代码段:
// ImageView to display the QR code in. This should be defined in
// your Activity's XML layout file
ImageView imageView = (ImageView) findViewById(R.id.qrCode);
String qrData = "Data I want to encode in QR code";
int qrCodeDimention = 500;
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(qrData, null,
Contents.Type.TEXT, BarcodeFormat.QR_CODE.toString(), qrCodeDimention);
try {
Bitmap bitmap = qrCodeEncoder.encodeAsBitmap();
imageView.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
这是 Contents.java
//
// * Copyright (C) 2008 ZXing authors
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
//
import android.provider.ContactsContract;
public final class Contents {
private Contents() {
}
public static final class Type {
// Plain text. Use Intent.putExtra(DATA, string). This can be used for URLs too, but string
// must include "http://" or "https://".
public static final String TEXT = "TEXT_TYPE";
// An email type. Use Intent.putExtra(DATA, string) where string is the email address.
public static final String EMAIL = "EMAIL_TYPE";
// Use Intent.putExtra(DATA, string) where string is the phone number to call.
public static final String PHONE = "PHONE_TYPE";
// An SMS type. Use Intent.putExtra(DATA, string) where string is the number to SMS.
public static final String SMS = "SMS_TYPE";
public static final String CONTACT = "CONTACT_TYPE";
public static final String LOCATION = "LOCATION_TYPE";
private Type() {
}
}
public static final String URL_KEY = "URL_KEY";
public static final String NOTE_KEY = "NOTE_KEY";
// When using Type.CONTACT, these arrays provide the keys for adding or retrieving multiple phone numbers and addresses.
public static final String[] PHONE_KEYS = {
ContactsContract.Intents.Insert.PHONE, ContactsContract.Intents.Insert.SECONDARY_PHONE,
ContactsContract.Intents.Insert.TERTIARY_PHONE
};
public static final String[] PHONE_TYPE_KEYS = {
ContactsContract.Intents.Insert.PHONE_TYPE,
ContactsContract.Intents.Insert.SECONDARY_PHONE_TYPE,
ContactsContract.Intents.Insert.TERTIARY_PHONE_TYPE
};
public static final String[] EMAIL_KEYS = {
ContactsContract.Intents.Insert.EMAIL, ContactsContract.Intents.Insert.SECONDARY_EMAIL,
ContactsContract.Intents.Insert.TERTIARY_EMAIL
};
public static final String[] EMAIL_TYPE_KEYS = {
ContactsContract.Intents.Insert.EMAIL_TYPE,
ContactsContract.Intents.Insert.SECONDARY_EMAIL_TYPE,
ContactsContract.Intents.Insert.TERTIARY_EMAIL_TYPE
};
}
和 QRCodeEncoder.java
/*
* Copyright (C) 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.provider.ContactsContract;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import java.util.Collection;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
public final class QRCodeEncoder {
private static final int WHITE = 0xFFFFFFFF;
private static final int BLACK = 0xFF000000;
private int dimension = Integer.MIN_VALUE;
private String contents = null;
private String displayContents = null;
private String title = null;
private BarcodeFormat format = null;
private boolean encoded = false;
public QRCodeEncoder(String data, Bundle bundle, String type, String format, int dimension) {
this.dimension = dimension;
encoded = encodeContents(data, bundle, type, format);
}
public String getContents() {
return contents;
}
public String getDisplayContents() {
return displayContents;
}
public String getTitle() {
return title;
}
private boolean encodeContents(String data, Bundle bundle, String type, String formatString) {
// Default to QR_CODE if no format given.
format = null;
if (formatString != null) {
try {
format = BarcodeFormat.valueOf(formatString);
} catch (IllegalArgumentException iae) {
// Ignore it then
}
}
if (format == null || format == BarcodeFormat.QR_CODE) {
this.format = BarcodeFormat.QR_CODE;
encodeQRCodeContents(data, bundle, type);
} else if (data != null && data.length() > 0) {
contents = data;
displayContents = data;
title = "Text";
}
return contents != null && contents.length() > 0;
}
private void encodeQRCodeContents(String data, Bundle bundle, String type) {
if (type.equals(Contents.Type.TEXT)) {
if (data != null && data.length() > 0) {
contents = data;
displayContents = data;
title = "Text";
}
} else if (type.equals(Contents.Type.EMAIL)) {
data = trim(data);
if (data != null) {
contents = "mailto:" + data;
displayContents = data;
title = "E-Mail";
}
} else if (type.equals(Contents.Type.PHONE)) {
data = trim(data);
if (data != null) {
contents = "tel:" + data;
displayContents = PhoneNumberUtils.formatNumber(data);
title = "Phone";
}
} else if (type.equals(Contents.Type.SMS)) {
data = trim(data);
if (data != null) {
contents = "sms:" + data;
displayContents = PhoneNumberUtils.formatNumber(data);
title = "SMS";
}
} else if (type.equals(Contents.Type.CONTACT)) {
if (bundle != null) {
StringBuilder newContents = new StringBuilder(100);
StringBuilder newDisplayContents = new StringBuilder(100);
newContents.append("MECARD:");
String name = trim(bundle.getString(ContactsContract.Intents.Insert.NAME));
if (name != null) {
newContents.append("N:").append(escapeMECARD(name)).append(';');
newDisplayContents.append(name);
}
String address = trim(bundle.getString(ContactsContract.Intents.Insert.POSTAL));
if (address != null) {
newContents.append("ADR:").append(escapeMECARD(address)).append(';');
newDisplayContents.append('\n').append(address);
}
Collection<String> uniquePhones = new HashSet<String>(Contents.PHONE_KEYS.length);
for (int x = 0; x < Contents.PHONE_KEYS.length; x++) {
String phone = trim(bundle.getString(Contents.PHONE_KEYS[x]));
if (phone != null) {
uniquePhones.add(phone);
}
}
for (String phone : uniquePhones) {
newContents.append("TEL:").append(escapeMECARD(phone)).append(';');
newDisplayContents.append('\n').append(PhoneNumberUtils.formatNumber(phone));
}
Collection<String> uniqueEmails = new HashSet<String>(Contents.EMAIL_KEYS.length);
for (int x = 0; x < Contents.EMAIL_KEYS.length; x++) {
String email = trim(bundle.getString(Contents.EMAIL_KEYS[x]));
if (email != null) {
uniqueEmails.add(email);
}
}
for (String email : uniqueEmails) {
newContents.append("EMAIL:").append(escapeMECARD(email)).append(';');
newDisplayContents.append('\n').append(email);
}
String url = trim(bundle.getString(Contents.URL_KEY));
if (url != null) {
// escapeMECARD(url) -> wrong escape e.g. http\://zxing.google.com
newContents.append("URL:").append(url).append(';');
newDisplayContents.append('\n').append(url);
}
String note = trim(bundle.getString(Contents.NOTE_KEY));
if (note != null) {
newContents.append("NOTE:").append(escapeMECARD(note)).append(';');
newDisplayContents.append('\n').append(note);
}
// Make sure we've encoded at least one field.
if (newDisplayContents.length() > 0) {
newContents.append(';');
contents = newContents.toString();
displayContents = newDisplayContents.toString();
title = "Contact";
} else {
contents = null;
displayContents = null;
}
}
} else if (type.equals(Contents.Type.LOCATION)) {
if (bundle != null) {
// These must use Bundle.getFloat(), not getDouble(), it's part of the API.
float latitude = bundle.getFloat("LAT", Float.MAX_VALUE);
float longitude = bundle.getFloat("LONG", Float.MAX_VALUE);
if (latitude != Float.MAX_VALUE && longitude != Float.MAX_VALUE) {
contents = "geo:" + latitude + ',' + longitude;
displayContents = latitude + "," + longitude;
title = "Location";
}
}
}
}
public Bitmap encodeAsBitmap() throws WriterException {
if (!encoded) return null;
Map<EncodeHintType, Object> hints = null;
String encoding = guessAppropriateEncoding(contents);
if (encoding != null) {
hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
// All are 0, or black, by default
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
private static String guessAppropriateEncoding(CharSequence contents) {
// Very crude at the moment
for (int i = 0; i < contents.length(); i++) {
if (contents.charAt(i) > 0xFF) { return "UTF-8"; }
}
return null;
}
private static String trim(String s) {
if (s == null) { return null; }
String result = s.trim();
return result.length() == 0 ? null : result;
}
private static String escapeMECARD(String input) {
if (input == null || (input.indexOf(':') < 0 && input.indexOf(';') < 0)) { return input; }
int length = input.length();
StringBuilder result = new StringBuilder(length);
for (int i = 0; i < length; i++) {
char c = input.charAt(i);
if (c == ':' || c == ';') {
result.append('\\');
}
result.append(c);
}
return result.toString();
}
}
这
compile 'com.google.zxing:core:2.3.0'
不幸的是对我没有工作。
这对我有用:
dependencies {
compile 'com.journeyapps:zxing-android-embedded:3.0.1@aar'
compile 'com.google.zxing:core:3.2.0'
}
请在此处找到链接:https://github.com/journeyapps/zxing-android-embedded
与蚂蚁建立问题?继续阅读
如果 ant -f core/build.xml
说类似:
Unable to locate tools.jar. Expected to find it in
C:\Program Files\Java\jre6\lib\tools.jar
然后设置您的 JAVA_HOME
适当的Java文件夹的环境变量。我在我的(Windows)中找到了工具。
C:\Program Files\Java\jdk1.6.0_21\lib
所以我设置了我的 JAVA_HOME
至:
C:\Progra~1\Java\jdk1.6.0_25
我在某个网站上发现的较短语法的原因,该网站上说:
“强烈建议您选择一个安装目录,该目录在路径名中不包含空格(例如,请勿在C: Program Files中安装)。如果在此目录中安装Java,则设置Java_home至关重要。环境变量到不包括空格的路径(例如,C: progra〜1);未能执行此操作将导致某些依赖Java_home值的程序引发的例外。”
然后,我重新启动了CMD(重要的是,因为DOS Shell仅在启动时读取Env vars,因此更改env var将需要您使用新的外壳来获取更新值)
最后是 ant -f core/build.xml
工作。
由于某些答案已经过时,我想提供自己的答案 -
按照建议将ZXing库集成到您的Android应用程序中 他们的Wiki, ,您需要在项目中添加2个Java文件:
然后进 Android Studio 将以下行添加到 build.gradle 文件:
dependencies {
....
compile 'com.google.zxing:core:3.2.1'
}
或者仍在使用 Eclipse与ADT-Plugin 添加 Core.jar 文件到 libs 您项目的子目录(在这里 全屏窗户 和 全屏Mac):
最后将此代码添加到您的 MainActivity.java:
public void scanQRCode(View v) {
IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
integrator.initiateScan(IntentIntegrator.QR_CODE_TYPES);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult result =
IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (result != null) {
String contents = result.getContents();
if (contents != null) {
showDialog(R.string.result_succeeded, result.toString());
} else {
showDialog(R.string.result_failed,
getString(R.string.result_failed_why));
}
}
}
private void showDialog(int title, CharSequence message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton(R.string.ok_button, null);
builder.show();
}
最终的应用程序将要求安装和启动 条形码扫描仪应用程序通过ZXing (扫描后将自动返回您的应用程序):
此外,如果您想建造并运行 ZXING测试应用程序 作为您自己应用的灵感:
然后,您需要4个Java文件 github:
- 基准性
- Benchmarkasynctask.java
- Benchmarkitem.java
- zxingtestactivity.java
和3个JAR文件 Maven存储库:
- Core.jar
- android-core.jar
- android-Integration.jar
(您可以自己构建JAR文件 mvn package
- 如果您从github签出zxing并安装 蚂蚁 和 小牛 计算机上的工具)。
注意:如果您的项目无法识别JAR文件,则可能需要在项目属性中添加Java版本:
你看过 Wiki页面 在ZXING网站上?看来你可能会发现 入门, 开发 和 ScanningViaintent 有帮助。
放
compile 'com.google.zxing:core:2.3.0'
进入您的Gradle依赖性。那样容易。在使用Android Studio和Gradle Build System之前。
如果您只需要Zxing中的Core.jar,则可以跳过该过程并从 加入Wiki页面
最新的ZXing(2.2)没有Core.jar在Core文件夹下,但是您可以从ZXing获得Core.jar Maven存储库在这里
逐步设置ZXing 3.2.1在Eclipse中
- 从“下载zxing-master.zip”https://github.com/zxing/zxing"
- unzip zxing-master.zip,使用日食在ZXing-Master中导入“ Android”项目
- 下载core-3.2.1.Jarhttp://repo1.maven.org/maven2/com/google/zxing/core/3.2.1/"
- 在“ Android”项目中创建“ libs”文件夹,然后将cor-3.2.1.jar粘贴到libs文件夹中
- 单击项目:选择“属性” - >“ Java编译器”以将级别更改为1.7。然后单击“ Android”更改“项目构建目标”对Android 4.4.2+,因为使用1.7需要使用Android 4.4编译
- 如果“ cameraconfigurationutils.java”中不存在“ zxing-Master/android/app/src/src/main/java/java/com/google/zxing/zxing/client/android/camera/camera/”。您可以从“ Zxing-Master/Android核/SRC/MAIN/JAVA/COM/GOOGLE/ZXING/ZXING/CLIENT/ANDROID/CAMERA/”中复制它,并粘贴到您的项目中。
- 清洁和构建项目。如果您的项目显示有关“开关 - 情况”的错误,则应将其更改为“如果 - else”。
- 完全的。清洁和构建项目。
- 参考链接: 使用ZXING创建Android条形码扫描应用程序
我尝试了所有可能的方法来实现这一目标,然后我发现了Youtherapps的Xzing Xzing。我已经将其用于日食,并在Github上共享。
如果您使用的是日食,请使用此项目: -
https://github.com/hiteshsahu/xzing-barcode-scanner-minified-eclipse
如果您使用的是工作室,请使用此项目: -
https://github.com/journeyapps/zxing-android-embedded
优点
您的应用中的内置条形码扫描仪不需要使用PlayStore安装第三方应用程序。
您无需在核心,Android客户端等之间感到困惑,只是将这些软件包和相关布局放在项目中,您就可以了。唯一需要的罐子是 com.google.zxing:核心:3.2.0 您可以从中下载
http://mvnrepository.com/artifact/com.google.zxing/core/3.2.0
无需添加大量的包裹 请参阅下图以获取比较
前 :-
后 :-
最重要的部分是 高度定制 IE。您可以添加闪光灯,将其用于片段和支撑方向更改。
您可以在此中使用此捕获活动 科尔多瓦应用 用于条形码扫描。
您在应用清单中的捕获活动看起来像这样
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:clearTaskOnLaunch="true"
android:configChanges="orientation|keyboardHidden"
android:exported="false"
android:screenOrientation="fullSensor"
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>
插件看起来像这样
public class BarcodeScanner extends CordovaPlugin {
public static final int REQUEST_CODE = 0x0ba7c0de;
private static final String SCAN = "scan";
private static final String CANCELLED = "cancelled";
private static final String FORMAT = "format";
private static final String TEXT = "text";
private static final String SCAN_INTENT = "com.google.zxing.client.android.SCAN";
private static final String LOG_TAG = "BarcodeScanner";
private CallbackContext callbackContext;
/**
* Constructor.
*/
public BarcodeScanner() {
}
/**
* Executes the request.
*
* This method is called from the WebView thread. To do a non-trivial amount of work, use:
* cordova.getThreadPool().execute(runnable);
*
* To run on the UI thread, use:
* cordova.getActivity().runOnUiThread(runnable);
*
* @param action The action to execute.
* @param args The exec() arguments.
* @param callbackContext The callback context used when calling back into JavaScript.
* @return Whether the action was valid.
*
* @sa https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaPlugin.java
*/
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
this.callbackContext = callbackContext;
if (action.equals(SCAN)) {
scan(args);
} else {
return false;
}
return true;
}
/**
* Starts an intent to scan and decode a barcode.
*/
public void scan(JSONArray args) {
Intent intentScan = new Intent(SCAN_INTENT);
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
// add config as intent extras
if(args.length() > 0) {
JSONObject obj;
JSONArray names;
String key;
Object value;
for(int i=0; i<args.length(); i++) {
try {
obj = args.getJSONObject(i);
} catch(JSONException e) {
Log.i("CordovaLog", e.getLocalizedMessage());
continue;
}
names = obj.names();
for(int j=0; j<names.length(); j++) {
try {
key = names.getString(j);
value = obj.get(key);
if(value instanceof Integer) {
intentScan.putExtra(key, (Integer)value);
} else if(value instanceof String) {
intentScan.putExtra(key, (String)value);
}
} catch(JSONException e) {
Log.i("CordovaLog", e.getLocalizedMessage());
continue;
}
}
}
}
// avoid calling other phonegap apps
intentScan.setPackage(this.cordova.getActivity().getApplicationContext().getPackageName());
this.cordova.startActivityForResult((CordovaPlugin) this, intentScan, REQUEST_CODE);
}
/**
* Called when the barcode scanner intent completes.
*
* @param requestCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its setResult().
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
JSONObject obj = new JSONObject();
try {
obj.put(TEXT, intent.getStringExtra("SCAN_RESULT"));
obj.put(FORMAT, intent.getStringExtra("SCAN_RESULT_FORMAT"));
obj.put(CANCELLED, false);
} catch (JSONException e) {
Log.d(LOG_TAG, "JSONException "+e.getMessage());
}
this.callbackContext.success(obj);
} else if (resultCode == Activity.RESULT_CANCELED) {
this.callbackContext.success("");
} else {
this.callbackContext.error("Technical Problem");
}
}
}
}
快乐的整合!
ZXING的家伙使创建1.7的Android项目变得更加容易。它不像以前那样痛苦。对于任何想快速为Android创建ZXING项目的人来说,这是一个快速的博客。
- 从zxing.org查看zxing源
- 在您的日食上创建一个Android项目
- 删除main.xml
- 右键单击“ SRC”目录并点击导入。根据上述顺序浏览以下目录。当您将它们添加为一个接一个地导入时,请确保您在导入向导的编辑字段中具有SRC目录。并且您仅在左目录树上选择“ com”目录。请勿选择SRC。
- 核
- Android融合
- 安卓
- 确保您的Android SDK版本为9,任何较小的东西和AndroidManifest.xml都会哭泣。
- strings.xml中的一种语言会婴儿crib,只需将a /放在'角色之前
ZXing 1.7(6月20日结帐)的Android项目。
http://www.4shared.com/file/bfx8y5ys/zxingjune2010.html (不再可用)
当Google Play服务时,为什么要使用外部LIB(由于版本 7.8.0)包括条形码解码器。
我刚刚写了一种方法,该方法解码生成的条形码, Bitmap
至 String
.
它确实可以完成要求的内容,只是没有 CaptureActivity
...
因此,可以跳过 android-integration
图书馆 build.gradle
:
dependencies {
// https://mvnrepository.com/artifact/com.google.zxing
compile('com.google.zxing:core:3.3.0')
compile('com.google.zxing:android-core:3.3.0')
}
如以下方法(在JUNIT测试中实际上是解码生成的条形码):
import android.graphics.Bitmap;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.Result;
protected String decode(Bitmap bitmap) {
MultiFormatReader reader = new MultiFormatReader();
String barcode = null;
int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray);
BinaryBitmap binary = new BinaryBitmap(new HybridBinarizer(source));
try {
Result result = reader.decode(binary);
// BarcodeFormat format = result.getBarcodeFormat();
// ResultPoint[] points = result.getResultPoints();
// byte[] bytes = result.getRawBytes();
barcode = result.getText();
} catch (NotFoundException e) {
e.printStackTrace();
}
return barcode;
}
我最近在iOS和Android中都使用了Google移动视觉。我强烈建议使用Google条形码扫描。任何方向的响应性非常灵敏,处理时间非常快。它称为Google移动视觉。
条形码扫描仪API以任何方向实时检测条形码。您还可以同时以不同格式检测和解析几个条形码。
https://developers.google.com/vision/
https://codelabs.developers.google.com/codelabs/bar-codes/#0
方法要容易得多。
只需在您的应用程序级别Gradle文件中包含依赖项
compile 'com.journeyapps:zxing-android-embedded:3.0.1@aar'
compile 'com.google.zxing:core:3.2.0'
在XML文件中定义一个按钮,然后在onCreate()中的Java文件中写下以下代码,并在按钮的OnClick侦听器中
new IntentIntegrator(this).initiateScan();
并在java文件的ongreate()之后写下以下代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
String st_scanned_result = result.getContents();
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
}
}