Barcode reading on a single screen

Asked

Viewed 1,326 times

4

My friends and I are developing an application in which in one of the functions the smartphone should scan the barcode of a product and search for it in a database (ours). As this can be done for several products, we chose to keep the reader on the screen always active and, with each scan, the product (with its data) is added to a list below.

The first library we found was Zxing, but its workings are different than what we need. It, when activating the scanner, opens a new Activity and returns the scan result for the previous Activity (onActivityResult). We look for other libraries, but all that we find, besides being based on Zxing, work practically the same way.

inserir a descrição da imagem aqui

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

  • Try Google. https://developers.google.com/vision/

2 answers

2

There is a google library, this is a codelab hers.

To use just add this dependency to your Gradle

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

Example class of Barcode

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" />

  • I’m using this code, it reads the code, but not all... An ITF code of a phone account is divided into 4 sectors, each sector with 11 numbers and 1 digit at the end (XXXXXXXXXXX-D XXXXXXXXXXX-D XXXXXXXXXXX-D XXXXXXXXXXX-D). These digits are not being read. Would you help me? Barcode: https://ibb.co/rZxsM8L

1

In addition to Uzziah’s answer: The code reads tickets, but if it is dealer accounts (phone, light, etc.), one should calculate the check digit of each block (Barcode example: https://ibb.co/rZxsM8L).

Using the Module 10, you have that 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;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.