仅限ZBar Scanner QR

时间:2014-10-01 13:15:29

标签: java android qr-code zbar

我使用ZBar QR扫描仪为我的Android应用程序。一切运行正常,图书馆和设置没有问题。问题是我想让ZBar只扫描QR码而不扫描条形码。

有办法吗?怎么样?

这是我的扫描活动代码:

import net.sourceforge.zbar.Config;
import net.sourceforge.zbar.Image;
import net.sourceforge.zbar.ImageScanner;
import net.sourceforge.zbar.Symbol;
import net.sourceforge.zbar.SymbolSet;


import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.Color;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

public class QRScanActivity extends Activity {
    private Camera mCamera;
    private CameraPreview mPreview;
    private Handler autoFocusHandler;

    TextView scanText;
    Button scanButton;

    ImageScanner scanner;

    private boolean barcodeScanned = false;
    private boolean previewing = true;

    static {
        System.loadLibrary("iconv");
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_qrscan);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        autoFocusHandler = new Handler();
        mCamera = getCameraInstance();

        // Instance barcode scanner
        scanner = new ImageScanner();
        scanner.setConfig(0, Config.X_DENSITY, 3);
        scanner.setConfig(0, Config.Y_DENSITY, 3);

        mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
        FrameLayout preview = (FrameLayout) findViewById(R.id.cameraPreview);
        preview.addView(mPreview);



    }

    public void onPause() {
        super.onPause();
        releaseCamera();
    }

    /** A safe way to get an instance of the Camera object. */
    public static Camera getCameraInstance() {
        Camera c = null;
        try {
            c = Camera.open();
        } catch (Exception e) {
        }
        return c;
    }

    private void releaseCamera() {
        if (mCamera != null) {
            previewing = false;
            mCamera.setPreviewCallback(null);
            mCamera.release();
            mCamera = null;
        }
    }

    private Runnable doAutoFocus = new Runnable() {
        public void run() {
            if (previewing)
                mCamera.autoFocus(autoFocusCB);
        }
    };

    PreviewCallback previewCb = new PreviewCallback() {
        public void onPreviewFrame(byte[] data, Camera camera) {
            Camera.Parameters parameters = camera.getParameters();
            Size size = parameters.getPreviewSize();

            Image barcode = new Image(size.width, size.height, "Y800");
            barcode.setData(data);

            int result = scanner.scanImage(barcode);
            Log.d("ZbarScanner",result+"");

            if (result != 0) {
                previewing = false;
                mCamera.setPreviewCallback(null);
                mCamera.stopPreview();

                SymbolSet syms = scanner.getResults();
                for (Symbol sym : syms) {
                    barcodeScanned = true;

                    Intent returnIntent = new Intent();
                    returnIntent.putExtra("result",sym.getData());
                    setResult(RESULT_OK,returnIntent);
                    finish();

                }
            }
        }
    };

    // Mimic continuous auto-focusing
    AutoFocusCallback autoFocusCB = new AutoFocusCallback() {
        public void onAutoFocus(boolean success, Camera camera) {
            autoFocusHandler.postDelayed(doAutoFocus, 1000);
        }
    };
    public void backToMain(View v){
        Intent returnIntent = new Intent();
        setResult(RESULT_CANCELED, returnIntent);
        finish();
    }
}

1 个答案:

答案 0 :(得分:6)

您可以为ImageScanner添加这些代码

scanner.setConfig(0, Config.ENABLE, 0); //Disable all the Symbols
scanner.setConfig(Symbol.QRCODE, Config.ENABLE, 1); //Only QRCODE is enable

此外,您还可以使用意图动作" org.magiclen.barcodescanner.SCAN"使用基于zbar的条形码扫描仪。

final Intent intent = new Intent("org.magiclen.barcodescanner.SCAN");
final List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);
if (list.size() > 0) {
    intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); // Can also use PRODUCT_MODE, SCAN_MODE,     QR_CODE_MODE
    startActivityForResult(intent, 0);
} else {
    // You may ask your user to install Easy Barcode Scanner
}

要获得扫描结果,您必须覆盖onActivityResult方法:

public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    if (requestCode == 0) {
            if (resultCode == Activity.RESULT_OK) {
                    final String result = data.getStringExtra("SCAN_RESULT"); // Get scanning result
                    final String type = data.getStringExtra("code_type"); // Get code type
            } else {
                    // Not scan any code yet
            }
    }
}