Barcode scanning on a single screen

4

I and my friends are developing an application in which one of the functions the smartphone should scan the bar code of a product and get it in a database (ours). Since this can be done for several products, we have chosen to keep the reader on the screen always active and, with each scan, the product (with your data) is added to a list just below.

The first library we found was ZXing, but its operation is different from what we need. When it activates the scanner, it opens a new Activity and returns the scan result to the previous Activity ( onActivityResult ). We are looking for other libraries, but all we find, in addition to being based on ZXing, work pretty much the same way.

I wonder if anyone has ever faced a similar situation and can help me.

    
asked by anonymous 16.03.2015 / 22:37

2 answers

2

There is a google library, this is a codelab of it.

To use just add this dependency to your gradle

compile 'com.google.android.gms:play-services-vision:10.0.1'

Barcode example class

import android.content.Intent;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;

import java.io.IOException;


public class BarcodeScannerActivity extends Activity {

    SurfaceView cameraView;
    BarcodeDetector barcodeDetector;
    CameraSource cameraSource;
    SparseArray<Barcode> barcodes;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_barcodescanner);
        cameraView = (SurfaceView) findViewById(R.id.surface_camera);
        barcodeDetector = new BarcodeDetector.Builder(BarcodeScannerActivity.this)
        //Aqui você escolhe que tipo de leitura ele vai suportar
                .setBarcodeFormats(Barcode.ALL_FORMATS)
                .build();
        setupCamera();
        setupReader();
    }

    private void setupCamera() {

        CameraSource.Builder builder = new CameraSource.Builder(getApplicationContext(), barcodeDetector);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            builder = builder.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        }

        cameraSource = builder.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH).build();
        cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                try {
                    cameraSource.start(cameraView.getHolder());
                } catch (IOException | SecurityException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                cameraSource.stop();
            }
        });
    }

    private void setupReader() {
        barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {

            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections) {
                //Quando le algum codigo de barra ele cai aqui
                barcodes = detections.getDetectedItems();
                if (barcodes != null && barcodes.size() >= 1) {
                    String barcode = barcodes.valueAt(0).displayValue;

                }
            }
        });
    }

}

Layout:

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


<SurfaceView
    android:id="@+id/surface_camera"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:fitsSystemWindows="true" />


<TextView
    android:id="@+id/scanText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="ESCANEANDO"
    android:textSize="40sp" />

    
31.12.2016 / 05:27
1

Complementing Uzias' response: The code reads tickets, but if it is dealer accounts (phone, light, etc.), you should calculate the check digit for each block (Example barcode: link ).

Using the Module 10 , you have this verification code:

private String checksum(String result) {
        //String result = "8462000000046107010901100356713580601131937174";

        String[] sequencias = splitToNChar(result, 11); //divide em blocos de 11

        List<String[]> checked = new ArrayList<>();

        for(String seq : sequencias) { //uma sequência de 11 dígitos
            int soma = 0;

            for(int s = seq.length() -1; s >= 0; s--) { //para cada dígito, da direita para a esquerda

                int dig = Character.getNumericValue(seq.charAt(s)); //o dígito

                if(dig > 0) {

                    String tmp;

                    if(s % 2 == 0) {

                        tmp = String.valueOf(dig * 2);

                        if(tmp.length() > 1) {
                            for(int p = 0; p < tmp.length(); p++) {
                                soma += Character.getNumericValue(tmp.charAt(p));
                            }
                        } else {
                            soma += Integer.parseInt(tmp);
                        }

                    } else {
                        tmp = String.valueOf(dig);

                        soma += Integer.parseInt(tmp);
                    }
                }
            }

            int rest = soma % 10; //módulo 10

            checked.add( new String[]{seq, String.valueOf(10 - rest) } );
        }

        String retorno = "";
        for(String[] code : checked) {
            retorno += code[0] + code[1];
        }
        return retorno;
    }
    
26.12.2018 / 19:39