Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZBar Scanner QR only

I'm using ZBar QR scanner for my android application. Everything is running fine and have no problems with the library and the settings. The problem is that I want to make ZBar to scan only QR codes and no barcodes.

Is there a way to do it? How?

This is my scanning Activity code:

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();
    }
}
like image 255
Boris Pawlowski Avatar asked Oct 01 '14 13:10

Boris Pawlowski


People also ask

Can you just scan a QR code without an app?

To scan a QR Code with Google Screen Search, you don't need an app. You can use the following steps to scan a QR Code: Point your camera at the QR Code. Hold down the “Home” button and swipe up to reveal the options at the bottom.

Can you scan a QR code without data?

No. Scanning a QR Code does not require the Internet. You can scan QR Codes without the Internet or network in general.

Can you make a website only accessible by QR code?

You can't ensure that the URL came from scanning the QR code, that isn't possible. QR codes are just a method of encoding text, once the user knows the text they can do whatever they want with it. You can, however, restrict the usefulness of the QR code so even if it is leaked it isn't useful.

How do I scan QR codes without downloading?

1. Google Screen Search: Google Screen Search allows consumers to scan QR Codes without an app instantly. All one has to do is point their camera at the QR Code, long-press the Home button and click on 'What's on my screen? ' The QR Code link will be available for consumers to open.


Video Answer


1 Answers

You can add these codes for ImageScanner

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

Besides, you can also use the intent action "org.magiclen.barcodescanner.SCAN" to use a zbar-based barcode scanner.

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
}

To get the scanning result, you must override onActivityResult method:

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
            }
    }
}
like image 69
user3382499 Avatar answered Oct 09 '22 06:10

user3382499