如何使用android中的zxing库从摄像头预览中解码条码?

时间:2014-04-18 14:42:40

标签: android barcode zxing

我想在我的android应用程序中实现独立扫描程序。我在我的项目中使用zxing的core.jar库。

我需要从相机预览中解码条形码。但我不知道如何实现它。因为没有官方文档。

你能给我一个关于以下事情的简单例子吗? 1.初始化相机并进行预览。 2.从预览中解码条形码。

是否有任何示例项目可以执行此操作?

3 个答案:

答案 0 :(得分:5)

看看我的简单实现: https://github.com/piobab/code-scanner

我使用ZBar库但是如果你想要你可以将ZBarScannerView.java实现更改为ZXingScannerView(其余代码没问题):

public class ZXingScannerView extends ScannerView {
public interface ResultHandler {
    public void handleResult(Result result);
}

private MultiFormatReader mMultiFormatReader;
private ResultHandler mResultHandler;

public ZXingScannerView(Context context) {
    super(context);
    setupScanner(null);
}

public ZXingScannerView(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);
    setupScanner(null);
}

/**
 * Specify recognized codes types.
 * @param codeTypes list of codes types from ZXing library
 */
public void setCodeTypes(List<com.google.zxing.BarcodeFormat> codeTypes) {
    setupScanner(codeTypes);
}

private void setupScanner(List<com.google.zxing.BarcodeFormat> symbols) {
    Map<DecodeHintType,Object> hints = new EnumMap<DecodeHintType,Object>(DecodeHintType.class);
    // Add specific formats
    hints.put(DecodeHintType.POSSIBLE_FORMATS, symbols);
    mMultiFormatReader = new MultiFormatReader();
    mMultiFormatReader.setHints(hints);
}

/**
 * Register callback in order to receive data from scanner.
 * @param resultHandler
 */
public void setResultHandler(ResultHandler resultHandler) {
    mResultHandler = resultHandler;
}

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
    Camera.Parameters parameters = camera.getParameters();
    Camera.Size size = parameters.getPreviewSize();
    int width = size.width;
    int height = size.height;

    Result rawResult = null;
    PlanarYUVLuminanceSource source = buildLuminanceSource(data, width, height);

    if(source != null) {
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        try {
            rawResult = mMultiFormatReader.decodeWithState(bitmap);
        } catch (ReaderException re) {

        } catch (NullPointerException npe) {

        } catch (ArrayIndexOutOfBoundsException aoe) {

        } finally {
            mMultiFormatReader.reset();
        }
    }

    if (rawResult != null) {
        stopCamera();
        if(mResultHandler != null) {
            mResultHandler.handleResult(rawResult);
        }
    } else {
        camera.setOneShotPreviewCallback(this);
    }
}

public PlanarYUVLuminanceSource buildLuminanceSource(byte[] data, int width, int height) {
    Rect rect = getFramingRectInPreview(width, height);
    if (rect == null) {
        return null;
    }
    // Go ahead and assume it's YUV rather than die.
    PlanarYUVLuminanceSource source = null;

    try {
        source = new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top,
                rect.width(), rect.height(), false);
    } catch(Exception e) {
    }

    return source;
}

}

如果您使用gradle,请将“com.google.zxing:core:2.2”添加到您的依赖项中。

答案 1 :(得分:0)

请看一下here。我建议在接受的答案中推荐“ZBar Android版本库”。似乎很容易实现。关注接受的答案的评论。

答案 2 :(得分:0)

您可以使用Zxing库,这些步骤假设您正在使用 Android studio

1-转到https://github.com/zxing/zxing并获取最新代码。

2-解压缩zip文件,并将android文件夹作为模块导入

3-在导入模块的生成的build.gradle中,更改

apply plugin: 'com.android.application'

apply plugin: 'com.android.library'

4-在同一个文件中删除第二行来自以下部分:

defaultConfig {
    applicationId "com.google.zxing.client.android"

5-转到http://repo1.maven.org/maven2/com/google/zxing/并下载最新的&#34; android-core&#34;和&#34; android-integration&#34; jar并将它们添加到 android 模块

的libs文件夹中

6-将您的模块的依赖关系添加到 android 模块

7-您的模块已准备好将ZXing用作库并调用其CaptureActivity,这很简单,使用以下XML和Java代码创建一个新活动

Java代码:

package bestteam.barcode;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.zxing.client.android.CaptureActivity;
import com.google.zxing.client.android.Intents;

public class MainActivity extends Activity implements View.OnClickListener, android.content.DialogInterface.OnClickListener {
private TextView tvScanResults;
private TextView tvStatus;
private Button btnScan;

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

    HandleClick hc = new HandleClick();
    tvScanResults = (TextView) findViewById(R.id.tvResult);
    tvStatus = (TextView) findViewById(R.id.tvStatus);
    btnScan = (Button) findViewById(R.id.butOther);
    btnScan.setOnClickListener(this);
    findViewById(R.id.butOther).setOnClickListener(hc);
}

private class HandleClick implements View.OnClickListener {
    public void onClick(View arg0) {
        Intent intent = new Intent(getApplicationContext(), CaptureActivity.class);
        intent.putExtra("SCAN_FORMATS", "QR_CODE,EAN_13,EAN_8,RSS_14,UPC_A,UPC_E,CODE_39,CODE_93,CODE_128,ITF,CODABAR,DATA_MATRIX");
        intent.setAction(Intents.Scan.ACTION);
        startActivityForResult(intent, 0);
    }
}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (resultCode == Activity.RESULT_OK) {
        // Handle successful scan
        String contents = intent.getStringExtra(Intents.Scan.RESULT);
        String formatName = intent.getStringExtra(Intents.Scan.RESULT_FORMAT);
        tvStatus.setText(formatName);
        tvScanResults.setText(contents + "\n\n" + formatName);
    } else if (resultCode == Activity.RESULT_CANCELED) {
        tvStatus.setText("Press a button to start a scan.");
        tvScanResults.setText("Scan cancelled.");
    }
}


@Override
public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
}
}

XML代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
    android:id="@+id/butOther"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.18"
    android:text="Scan"
    android:textSize="18sp" />

</LinearLayout>

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tvStatus"
android:text="Press a button to start a scan."
android:gravity="center"
android:textSize="18sp" />

<TextView
android:id="@+id/tvResult"
android:layout_width="346dp"
android:layout_height="394dp"
android:background="@android:color/white"
android:gravity="center"
android:text="Ready"
android:textColor="@android:color/black"
android:textSize="18sp" />

</LinearLayout>