【问题标题】:Android Studio Zxing IntegrationAndroid Studio Zxing 集成
【发布时间】:2015-05-23 19:05:19
【问题描述】:

我正在尝试构建一个应用程序,该应用程序使用来自扫描条形码的输入并对其进行解析,以便将其用作数据库的输入。 我一直在尝试使用 Zxing 条码扫描仪,但我真的很难将它集成到我的应用程序中。 我遵循了许多教程,但没有一个给我任何成功。 我最接近实现所需功能的方法是遵循本教程:http://code.tutsplus.com/tutorials/android-sdk-create-a-barcode-reader--mobile-17162 但是,这需要在设备上安装 Barcode Scanner 应用程序,我希望我的应用程序具有它自己的扫描功能。 有人可以为我指出一个针对 Android Studio 的白痴教程吗?

【问题讨论】:

  • 您使用的是哪个版本的设备...??

标签: android android-studio zxing


【解决方案1】:

为了将 Zxing 库集成到您的应用程序中,请将以下依赖项添加到您的 android 项目的 build.gradle 文件中。

dependencies {
    compile 'me.dm7.barcodescanner:zxing:1.7.1'
}

【讨论】:

    【解决方案2】:

    我的项目是在 Eclipse 上开发的。

    首先下载ZXing lib

    然后将此库作为库添加到您的应用中... [右键单击您的项目->属性...]

    然后写这段代码:

    btnScan.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    try{
                        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                        intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE");
                        startActivityForResult(intent, 0);
                    } catch(Exception e){
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), "ERROR:" + e, Toast.LENGTH_LONG).show();
                    }
                }
            });
    

    还有:

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);
            if (requestCode == 0) {
    
                if (resultCode == RESULT_OK) {
                    textViewFormat.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
                    textViewData.setText(intent.getStringExtra("SCAN_RESULT"));
    
                    Uri imageURI = intent.getData();
                    Bitmap bitmap;
                    try{
                        bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageURI);
                        scannedBitmap.setImageBitmap(bitmap);
                    } catch(Exception e){
                        e.printStackTrace();
                    }
    
                    //Toast.makeText(getApplicationContext(), intent.getStringExtra("SCAN_RESULT_FORMAT") + ":" + intent.getStringExtra("SCAN_RESULT"), 5000).show();
                } else if (resultCode == RESULT_CANCELED) {
                    textViewFormat.setText("");
                    textViewData.setText("Cancelled By user");
                }
    
            }
    

    完整项目下载我的 git repo

    宣言: 您必须授予您的 android manifesto 文件权限:

    <uses-permission android:name="android.permission.CAMERA" />
        <uses-permission android:name="android.permission.FLASHLIGHT" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".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>
            <activity
                android:name=".encode.EncodeActivity"
                android:label="@string/app_name"
                android:stateNotNeeded="true" >
                <intent-filter>
                    <action android:name="com.google.zxing.client.android.ENCODE" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
                <!-- This allows us to handle the Share button in Contacts. -->
                <intent-filter>
                    <action android:name="android.intent.action.SEND" />
    
                    <category android:name="android.intent.category.DEFAULT" />
    
                    <data android:mimeType="text/x-vcard" />
                </intent-filter>
                <!-- This allows us to handle sharing any plain text . -->
                <intent-filter>
                    <action android:name="android.intent.action.SEND" />
    
                    <category android:name="android.intent.category.DEFAULT" />
    
                    <data android:mimeType="text/plain" />
                </intent-filter>
            </activity>
            <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>
        </application>
    

    【讨论】:

      猜你喜欢
      • 2013-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-07
      • 2013-12-18
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多