You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中如何在主应用类中调用导入的ZBarScanner库类?

解决Android中调用ZBarScanner库类的问题

Hey there! Let's get your ZBarScanner library working properly step by step. The core issue right now is that your project hasn't actually added the library's dependency to your build configuration, which is why you can't reference its classes.

第一步:添加库依赖到Build.gradle

Open your app-level build.gradle file, and add the ZBarScanner library dependency to the dependencies block. Here's how your updated dependencies section should look:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    compile 'com.android.support:design:25.0.1'
    testCompile 'junit:junit:4.12'
    // 添加ZBarScanner库依赖
    compile 'me.dm7.barcodescanner:zbar:1.9'
}

After adding this line, click the Sync Now button in Android Studio to let Gradle download and integrate the library into your project.

第二步:添加相机权限

Since the scanner needs access to the camera, you have to declare the permission in your AndroidManifest.xml:

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

For Android 6.0 (API level 23) and above, you also need to request this permission dynamically at runtime (since it's a dangerous permission). We'll handle this in the next step.

第三步:编写调用扫描功能的代码

Now you can reference the library's classes (like ZBarScannerActivity) in your button click event. Here's a complete example:

1. 按钮点击事件与权限检查

In your main Activity, set up the button click listener to handle permission checks and launch the scanner:

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import me.dm7.barcodescanner.zbar.ZBarScannerActivity;
import me.dm7.barcodescanner.zbar.ZBarScannerResult;

public class MainActivity extends AppCompatActivity {
    private static final int SCAN_REQUEST_CODE = 100;
    private static final int CAMERA_PERMISSION_REQUEST = 200;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button scanButton = findViewById(R.id.scan_btn);
        scanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 检查相机权限
                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED) {
                    // 请求权限
                    ActivityCompat.requestPermissions(MainActivity.this,
                            new String[]{Manifest.permission.CAMERA},
                            CAMERA_PERMISSION_REQUEST);
                } else {
                    // 权限已授予,启动扫描界面
                    launchScanner();
                }
            }
        });
    }

    private void launchScanner() {
        Intent intent = new Intent(this, ZBarScannerActivity.class);
        startActivityForResult(intent, SCAN_REQUEST_CODE);
    }
}

2. 处理权限请求结果

Add this method to handle the user's permission response:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == CAMERA_PERMISSION_REQUEST) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // 用户同意权限,启动扫描
            launchScanner();
        } else {
            Toast.makeText(this, "需要相机权限才能使用扫描功能", Toast.LENGTH_SHORT).show();
        }
    }
}

3. 处理扫描结果

Override onActivityResult to get and handle the scanned data:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SCAN_REQUEST_CODE && resultCode == RESULT_OK) {
        if (data != null) {
            ZBarScannerResult result = data.getParcelableExtra(ZBarScannerActivity.SCAN_RESULT);
            if (result != null) {
                String scannedText = result.getContents();
                // 在这里处理扫描到的内容,比如显示Toast或者更新UI
                Toast.makeText(this, "扫描结果:" + scannedText, Toast.LENGTH_LONG).show();
            }
        }
    }
}

最后验证

After completing all these steps, rebuild your project and test the button click. You should now be able to launch the scanner and get the scanned results without any class reference issues.

内容的提问来源于stack exchange,提问作者JoDee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:40:36