简体   繁体   English

无需安装BarCode扫描仪即可集成ZXing QR码扫描仪

[英]Integrate ZXing QR code scanner without installing BarCode Scanner

I am trying to Integrate ZXing QR Code into my android app without installing BarCode Scanner app, I have followed the steps as: 我正在尝试将ZXing QR Code集成到我的Android应用程序而不安装BarCode Scanner应用程序,我按照以下步骤操作:

1) Firstly I have downloaded ZXing.zip file and extract it 1)首先我下载了​​ZXing.zip文件并解压缩

2)open the ZXing project as an android existing project and then go to android folder and open the android folder and also include core.jar file into the ZXing project named CaptureActivity. 2)打开ZXing项目作为Android现有项目,然后转到android文件夹并打开android文件夹,并将core.jar文件包含到名为CaptureActivity的ZXing项目中。

3)I have used the CaptureActivity project as a library in my project named 'QRCodeSample'. 3)我在我的项目名为“QRCodeSample”中使用了CaptureActivity项目作为库。 (Problem in including CaptureActivity as a library) (将CaptureActivity包含为库的问题)

4)My code is as below3: 4)我的代码如下:

   public class QRCodeSampleActivity extends Activity {
Button b1;
static String contents;

public static final int REQUEST_CODE = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            /*Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE",
                    "QR_CODE_MODE");
            startActivityForResult(intent, 0);*/
            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) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            Log.i("Barcode Result", contents);
            Intent i1 = new Intent(QRCodeSampleActivity.this, webclass.class);
            startActivity(i1);
            // Handle successful scan
        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel
            Log.i("Barcode Result","Result canceled");
        }
    }
}

 }

The manifest file is : 清单文件是:

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

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

     <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="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:label="@string/app_name"
        android:name=".QRCodeSampleActivity" >
       <intent-filter >
            <action android:name="android.intent.action.MAIN" />

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

    </activity>


    <activity  android:name=".webclass"></activity>

</application>

</manifest>

and its not adding library also 它也没有添加库

LibraryInformation

When I am trying to run my project,the error msg is: 当我尝试运行我的项目时,错误消息是:

Unable to instantiate activity ComponentInfo{com.qr.code/com.qr.code}: java.lang.ClassNotFoundException: com.qr.code in loader dalvik.system.PathClassLoader[/data/app/com.qr.code-1.apk] 无法实例化活动ComponentInfo {com.qr.code / com.qr.code}:java.lang.ClassNotFoundException:load dalvik.system.PathClassLoader中的com.qr.code [/data/app/com.qr.code-1 apk文件]

Finally I got the answer, 最后我得到了答案,

As of ADT 14,the resource fields(such as R.id.decode) are no longer constants when defined in library projects 从ADT 14开始,资源字段(例如R.id.decode)在库项目中定义时不再是常量

So in the ZXing library->android->com.google.zxing.client.android.CaptureActivityHandler.java and DecodeHandler.java 所以在ZXing库中 - > android-> com.google.zxing.client.android.CaptureActivityHandler.java和DecodeHandler.java

Replace both of these classes switch case statements with if-else,and then import this ZXing library into your project.. 用if-else替换这两个类切换case语句,然后将这个ZXing库导入到你的项目中。

Rest of the coding of my own project is same...just the problem with the library classes as these are not updated as according to ADT 14.. 我自己的项目的其余编码是相同的...只是库类的问题,因为这些没有根据ADT 14更新。

Kanika

For all those Android Studio/Gradle users out there 适用于所有Android Studio / Gradle用户

Okay guys, as my task today was to integrate ZXING into an Android application and there were no good sources for input all over, I will give you a hint what made my be successful - cause it turned out to be very easy (on version 2.*). 好吧,伙计们,今天我的任务是将ZXING集成到一个Android应用程序中,并且没有很好的输入来源,我会给你一个暗示是什么让我成功 - 因为它变得非常简单 (在版本2上) 。*)。

There is a real handy git repository that provides the zxing android library project as an AAR archive. 有一个真正方便的git存储库,提供zxing android库项目作为AAR存档。

All you have to do is add this to your build.gradle 您所要做的就是将它添加到build.gradle中

repositories {
    mavenCentral()

    maven {
        url "https://raw.github.com/embarkmobile/zxing-android-minimal/mvn-repo/maven-repository/"
    }
}

dependencies {
    compile 'com.google.zxing:core:2.2'
    compile 'com.embarkmobile:zxing-android-minimal:1.2.1@aar'
}

and Gradle does all the magic to compile the code and makes it accessible in your app. 和Gradle完成了编译代码并使其在您的应用程序中可访问的神奇功能。

To start the Scanner afterwards, use this class/method: 要在以后启动扫描仪,请使用以下类/方法:

IntentIntegrator.initiateScan(this);    // `this` is the current Activity

If you already visited the link you gonna see that i just copy&pasted the code from there the git readme. 如果您已经访问过该链接,您将看到我只是从那里复制并粘贴代码git自述文件。 If not go there to get some more insight and code examples! 如果没有去那里获得更多的见解和代码示例!

Hope to be helpful for future readers. 希望对未来的读者有所帮助。 Peace :) 和平:)

You need to follow step as given by the link 您需要按照链接给出的步骤进行操作

http://www.androidaz.com/development/zxing-qr-reader-direct-integration http://www.androidaz.com/development/zxing-qr-reader-direct-integration

you can download core.jar from 你可以从下载core.jar

http://repo1.maven.org/maven2/com/google/zxing/core/2.2/ http://repo1.maven.org/maven2/com/google/zxing/core/2.2/

The above is working for me, if your program still just put the core-2.2.jar in libs and clean your project 如果你的程序仍然只是将core-2.2.jar放在libs中并清理你的项目,那么上面的内容对我有用

I had the same Problem and after hours struggling with it I finally managed to solve it like this. 我有同样的问题,经过几个小时的努力,我终于设法解决了这个问题。 as Rubiraj is pointing 正如Rubiraj指出的那样

* Right Click on your project > Properties > Android > (under tab library) Add the Zxing project * * 右键单击您的项目>属性> Android>(在选项卡库下)添加Zxing项目*

Assuming you were able to correctly create Zxing project as a Library as it is explained here : http://damianflannery.wordpress.com/2011/06/13/integrate-zxing-barcode-scanner-into-your-android-app-natively-using-eclipse/ 假设您能够正确地创建Zxing项目作为库,如下所述: http ://damianflannery.wordpress.com/2011/06/13/integrate-zxing-barcode-scanner-into-your-android-app- 本地-使用-蚀/

MaterialBarcodeScanner: Easy to use barcode reader for your Android Project (Uses Google Mobile Vision API). MaterialBarcodeScanner:易于使用的条形码阅读器,适用于您的Android项目(使用Google Mobile Vision API)。

  1. Provide gradle dependency 提供gradle依赖

    compile 'com.edwardvanraak:MaterialBarcodeScanner:0.0.6-ALPHA' 编译'com.edwardvanraak:MaterialBarcodeScanner:0.0.6-ALPHA'

  2. Build a MaterialBarcodeScanner 构建MaterialBarcodeScanner

     private void startScan() { /** * Build a new MaterialBarcodeScanner */ final MaterialBarcodeScanner mBarcodeScanner = new MaterialBarcodeScannerBuilder() .withActivity(MainActivity.this) .withEnableAutoFocus(true) .withBleepEnabled(true) .withBackfacingCamera() .withText("Scanning...") .withResultListener(new MaterialBarcodeScanner.OnResultListener() { @Override public void onResult(Barcode barcode) { barcodeResult = barcode; result.setText(barcode.rawValue); } }) .build(); mBarcodeScanner.startScan(); } 
  3. Hook it up to a button 将它连接到一个按钮

     fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startScan(); } }); 
  4. Start scanning! 开始扫描!

Step by step to setup zxing 3.2.1 in eclipse 一步一步在eclipse中设置zxing 3.2.1

  1. Download zxing-master.zip from " https://github.com/zxing/zxing " 从“ https://github.com/zxing/zxing ”下载zxing-master.zip
  2. Unzip zxing-master.zip, Use eclipse to import "android" project in zxing-master 解压缩zxing-master.zip,使用eclipse在zxing-master中导入“android”项目
  3. Download core-3.2.1.jar from " http://repo1.maven.org/maven2/com/google/zxing/core/3.2.1/ " 从“ http://repo1.maven.org/maven2/com/google/zxing/core/3.2.1/ ”下载core-3.2.1.jar
  4. Create "libs" folder in "android" project and paste cor-3.2.1.jar into the libs folder 在“android”项目中创建“libs”文件夹,并将cor-3.2.1.jar粘贴到libs文件夹中
  5. Click on project: choose "properties" -> "Java Compiler" to change level to 1.7. 单击项目:选择“属性” - >“Java编译器”将级别更改为1.7。 Then click on "Android" change "Project build target" to android 4.4.2+, because using 1.7 requires compiling with Android 4.4 然后点击“Android”将“Project build target”更改为android 4.4.2+,因为使用1.7需要使用Android 4.4进行编译
  6. If "CameraConfigurationUtils.java" don't exist in "zxing-master/android/app/src/main/java/com/google/zxing/client/android/camera/". 如果“zxing-master / android / app / src / main / java / com / google / zxing / client / android / camera /”中不存在“CameraConfigurationUtils.java”。 You can copy it from "zxing-master/android-core/src/main/java/com/google/zxing/client/android/camera/" and paste to your project. 你可以从“zxing-master / android-core / src / main / java / com / google / zxing / client / android / camera /”复制它并粘贴到你的项目中。
  7. Clean and build project. 清理并构建项目。 If your project show error about "switch - case", you should change them to "if - else". 如果您的项目显示有关“switch - case”的错误,则应将其更改为“if - else”。
  8. Completed. 已完成。 Clean and build project. 清理并构建项目。 You can click on "Proprties" > "Android" > click on "Is Libraries" to use for your project 您可以单击“Proprties”>“Android”>单击“Is Libraries”以用于您的项目
  1. After importing Zxing as existing project, Properties > Java Buildpath > Check "is library" (check button) and then try to add Zxing as library. 将Zxing导入为现有项目后,Properties> Java Buildpath> Check“is library”(检查按钮),然后尝试将Zxing添加为库。

  2. Make sure webclass.class exist in your QRcodesampleActivity.java 确保您的QRcodesampleActivity.java存在webclass.class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM