Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Bar Code Scanner using Zbar library in android

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 6.94k
    Comment on it

    There are so many libraries like Zbar,Zxing Red-Scanner that are used for bar code scanning.

    I am explaining here Zbar library integration for bar code scanning. Zbar is open source. It is fast,simple as compared to Zxing. First of all you have to download the SDK of Zbar from this link:

    Download ZBar Sdk then import this to your code.

    add these to manifest file.

    Now Consider you open Scanner on click of a button. Then add CameraPreview class to your project. Now call this class.

    Intent ScanCodeIntent = new Intent(this,QrCodeScanner.class);
    startActivity(ScanCodeIntent);
    

    In QrCodeScanner , open a camera an here when detecting any barcode,it call PreviewCallback(), and getData() method of Scanner class returns barcode.


    public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
        private SurfaceHolder mHolder;
        private Camera mCamera;
        private PreviewCallback previewCallback;
        private AutoFocusCallback autoFocusCallback;
    
        public CameraPreview(Context context, Camera camera,
                             PreviewCallback previewCb,
                             AutoFocusCallback autoFocusCb) {
            super(context);
            mCamera = camera;
            previewCallback = previewCb;
            autoFocusCallback = autoFocusCb;
            // Set camera to continuous focus if supported, otherwise use
            // software auto-focus. Only works for API level >=9.
    
            // Install a SurfaceHolder.Callback so we get notified when the
            // underlying surface is created and destroyed.
            mHolder = getHolder();
            mHolder.addCallback(this);
    
            // deprecated setting, but required on Android versions prior to 3.0
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }
    
        public void surfaceCreated(SurfaceHolder holder) {
            // The Surface has been created, now tell the camera where to draw the preview.
            try {
                mCamera.setPreviewDisplay(holder);
            } catch (IOException e) {
                Log.d("DBG", "Error setting camera preview: " + e.getMessage());
            }
        }
    
        public void surfaceDestroyed(SurfaceHolder holder) {
            // Camera preview released in activity
        }
    
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            /*
             * If your preview can change or rotate, take care of those events here.
             * Make sure to stop the preview before resizing or reformatting it.
             */
            if (mHolder.getSurface() == null){
              // preview surface does not exist
              return;
            }
    
            // stop preview before making changes
            try {
                mCamera.stopPreview();
            } catch (Exception e){
              // ignore: tried to stop a non-existent preview
            }
    
            try {
                // Hard code camera surface rotation 90 degs to match Activity view in portrait
                mCamera.setDisplayOrientation(90);
    
                mCamera.setPreviewDisplay(mHolder);
                mCamera.setPreviewCallback(previewCallback);
                mCamera.startPreview();
                mCamera.autoFocus(autoFocusCallback);
            } catch (Exception e){
                Log.d("DBG", "Error starting camera preview: " + e.getMessage());
            }
        }
    }
    
    
    public class QrCodeScanner extends Activity
    {
        private Camera mCamera;
        private CameraPreview mPreview;
        private Handler autoFocusHandler;
        TextView cancelText;
        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_qr_code);
    
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            //scanText=(TextView)findViewById(R.id.scanText);
            iconForActionBar.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    finish();
                }
            });
            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);
            cancelText=(TextView)findViewById(R.id.FinishScan);
            //scanText = (TextView)findViewById(R.id.scanText);
            cancelText.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    finish();
                }
            });
    
        }
    
        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);
    
                if (result != 0) {
                    previewing = false;
                    mCamera.setPreviewCallback(null);
                    mCamera.stopPreview();
    
                    SymbolSet syms = scanner.getResults();
                    for (Symbol sym : syms) {
                        //cancelText.setText(sym.getData());
    
                        String id= sym.getData();
                        Toast.makeText(getApplicationContext(), "BAr code >> > >  > > > >>   "+id, Toast.LENGTH_LONG).show();
    
                    }
                }
            }
        };
    
        // Mimic continuous auto-focusing
        AutoFocusCallback autoFocusCB = new AutoFocusCallback() {
            public void onAutoFocus(boolean success, Camera camera) {
                autoFocusHandler.postDelayed(doAutoFocus, 1000);
            }
        };
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: