Android Open Source - barcodescanner Z Xing Scanner View






From Project

Back to project page barcodescanner.

License

The source code is released under:

Apache License

If you think the Android project barcodescanner listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.dm7.barcodescanner.zxing;
/*  www. ja  v a2s.  co  m*/
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.hardware.Camera;
import android.util.AttributeSet;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.PlanarYUVLuminanceSource;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

import me.dm7.barcodescanner.core.BarcodeScannerView;
import me.dm7.barcodescanner.core.DisplayUtils;

public class ZXingScannerView extends BarcodeScannerView {
    public interface ResultHandler {
        public void handleResult(Result rawResult);
    }

    private MultiFormatReader mMultiFormatReader;
    public static final List<BarcodeFormat> ALL_FORMATS = new ArrayList<BarcodeFormat>();
    private List<BarcodeFormat> mFormats;
    private ResultHandler mResultHandler;

    static {
        ALL_FORMATS.add(BarcodeFormat.UPC_A);
        ALL_FORMATS.add(BarcodeFormat.UPC_E);
        ALL_FORMATS.add(BarcodeFormat.EAN_13);
        ALL_FORMATS.add(BarcodeFormat.EAN_8);
        ALL_FORMATS.add(BarcodeFormat.RSS_14);
        ALL_FORMATS.add(BarcodeFormat.CODE_39);
        ALL_FORMATS.add(BarcodeFormat.CODE_93);
        ALL_FORMATS.add(BarcodeFormat.CODE_128);
        ALL_FORMATS.add(BarcodeFormat.ITF);
        ALL_FORMATS.add(BarcodeFormat.CODABAR);
        ALL_FORMATS.add(BarcodeFormat.QR_CODE);
        ALL_FORMATS.add(BarcodeFormat.DATA_MATRIX);
        ALL_FORMATS.add(BarcodeFormat.PDF_417);
    }

    public ZXingScannerView(Context context) {
        super(context);
        initMultiFormatReader();
    }

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

    public void setFormats(List<BarcodeFormat> formats) {
        mFormats = formats;
        initMultiFormatReader();
    }

    public void setResultHandler(ResultHandler resultHandler) {
        mResultHandler = resultHandler;
    }

    public Collection<BarcodeFormat> getFormats() {
        if(mFormats == null) {
            return ALL_FORMATS;
        }
        return mFormats;
    }

    private void initMultiFormatReader() {
        Map<DecodeHintType,Object> hints = new EnumMap<DecodeHintType,Object>(DecodeHintType.class);
        hints.put(DecodeHintType.POSSIBLE_FORMATS, getFormats());
        mMultiFormatReader = new MultiFormatReader();
        mMultiFormatReader.setHints(hints);
    }

    @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;

        if(DisplayUtils.getScreenOrientation(getContext()) == Configuration.ORIENTATION_PORTRAIT) {
            byte[] rotatedData = new byte[data.length];
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++)
                    rotatedData[x * height + height - y - 1] = data[x + y * width];
            }
            int tmp = width;
            width = height;
            height = tmp;
            data = rotatedData;
        }

        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) {
                // continue
            } catch (NullPointerException npe) {
                // This is terrible
            } 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;
    }
}




Java Source Code List

me.dm7.barcodescanner.core.BarcodeScannerView.java
me.dm7.barcodescanner.core.CameraPreview.java
me.dm7.barcodescanner.core.CameraUtils.java
me.dm7.barcodescanner.core.DisplayUtils.java
me.dm7.barcodescanner.core.ViewFinderView.java
me.dm7.barcodescanner.zbar.BarcodeFormat.java
me.dm7.barcodescanner.zbar.Result.java
me.dm7.barcodescanner.zbar.ZBarScannerView.java
me.dm7.barcodescanner.zbar.sample.FormatSelectorDialogFragment.java
me.dm7.barcodescanner.zbar.sample.MainActivity.java
me.dm7.barcodescanner.zbar.sample.MessageDialogFragment.java
me.dm7.barcodescanner.zbar.sample.ScannerActivity.java
me.dm7.barcodescanner.zbar.sample.ScannerFragmentActivity.java
me.dm7.barcodescanner.zbar.sample.ScannerFragment.java
me.dm7.barcodescanner.zbar.sample.SimpleScannerActivity.java
me.dm7.barcodescanner.zbar.sample.SimpleScannerFragmentActivity.java
me.dm7.barcodescanner.zbar.sample.SimpleScannerFragment.java
me.dm7.barcodescanner.zxing.ZXingScannerView.java
me.dm7.barcodescanner.zxing.sample.FormatSelectorDialogFragment.java
me.dm7.barcodescanner.zxing.sample.MainActivity.java
me.dm7.barcodescanner.zxing.sample.MessageDialogFragment.java
me.dm7.barcodescanner.zxing.sample.ScannerActivity.java
me.dm7.barcodescanner.zxing.sample.ScannerFragmentActivity.java
me.dm7.barcodescanner.zxing.sample.ScannerFragment.java
me.dm7.barcodescanner.zxing.sample.SimpleScannerActivity.java
me.dm7.barcodescanner.zxing.sample.SimpleScannerFragmentActivity.java
me.dm7.barcodescanner.zxing.sample.SimpleScannerFragment.java