Zbar integration into Android app

Rilcon42 picture Rilcon42 · Jan 24, 2013 · Viewed 30.1k times · Source

I am using the Android ADT Bundle for dev work. After reading multiple guides online I have added the package com.dm.zbar.android.scanner to my project. I have included the files CameraPreview.java, ZBarConstants.java, and ZBarScannerActivity.java in the package. Despite all this the ZBAR_SCANNER_REQUEST var in the class ScanActivity.java (created by me, but using zbar methods) cannot be resolved to a variable. Everything except this variable is accepted. Any idea why this is occurring? Note: My libs folder contains everything in here:

https://github.com/DushyanthMaguluru/ZBarScanner/tree/master/ZBarScannerLibrary/libs

and zbar.jar is included on the build path.

ScanActivity:

package com.xx.xxx;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
import com.dm.zbar.android.scanner.ZBarConstants;
import com.dm.zbar.android.scanner.ZBarScannerActivity;
import net.sourceforge.zbar.Symbol;

public class ScanActivity extends Activity {

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

public void launchScanner(View v) {
    if (isCameraAvailable()) {
        Intent intent = new Intent(this, ZBarScannerActivity.class);
        startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
    } else {
        Toast.makeText(this, "Rear Facing Camera Unavailable", Toast.LENGTH_SHORT).show();
    }
}

public void launchQRScanner(View v) {
    if (isCameraAvailable()) {
        Intent intent = new Intent(this, ZBarScannerActivity.class);
        intent.putExtra(ZBarConstants.SCAN_MODES, new int[]{Symbol.QRCODE});
        startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
    } else {
        Toast.makeText(this, "Rear Facing Camera Unavailable", Toast.LENGTH_SHORT).show();
    }
}

public boolean isCameraAvailable() {
    PackageManager pm = getPackageManager();
    return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case ZBAR_SCANNER_REQUEST:
        case ZBAR_QR_SCANNER_REQUEST:
            if (resultCode == RESULT_OK) {
                Toast.makeText(this, "Scan Result = " + data.getStringExtra(ZBarConstants.SCAN_RESULT), Toast.LENGTH_SHORT).show();
            }
            break;
    }
}

}

Just in case it is relevant: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xx.xxx"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

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

<uses-feature
    android:name="android.hardware.camera"
    android:required="false" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="xx"
    android:theme="@style/AppTheme" >
    <activity android:name="com.xx.xxx.MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.xx.xxx.WvActivity" />
    <activity android:name="com.xx.xxx.ScanActivity" />
    <activity
        android:name="com.dm.zbar.android.scanner.ZBarScannerActivity"
        android:screenOrientation="landscape" />
</application>

Answer

Dixit Patel picture Dixit Patel · Jan 24, 2013

I checked the ZBar Library Example. You made a mistake.

This error occurred because you did not declare these two variables in your activity

private static final int ZBAR_SCANNER_REQUEST = 0;
private static final int ZBAR_QR_SCANNER_REQUEST = 1;

you need to declare these variables above your onCreate(..) Method.

See the ZBar Example.