EAN13 Barcode
The EAN13 barcode belongs to the GS1 System which is an official model of standardization of the processes of product identification and commercial management, which exists since 2006, it consists of a sequence of 13 digits and its symbology represents the following items:
- Identification of the country of origin of the product.
- Name of manufacturer company.
- Identification number of the product.
- Type.
See an image that illustrates its symbology:
Calculation
One of the main requirements to check if a barcode is valid is to calculate the checker digit, see how the calculation is done:
Suppose we are using the barcode : 789162731405 and
we want to know the final digit. (Checker)
Add all digits of odd positions (digits 7, 9, 6, 7, 1 and 0):
7 + 9 + 6 + 7 + 1 + 0 = 30
Add all digits of even positions (digits 8, 1, 2, 3, 4 and 5):
8 + 1 + 2 + 3 + 4 + 5 = 23
Multiply the sum of the digits of even positions by 3, see: 23 * 3 = 69
Add the two results of the previous steps, see: 30 + 69 = 99
Determine the number that should be added to the sum result for
if you create a multiple of 10, see: 99 + 1 = 100
Therefore, the check digit is 1.
Implementation
Using Object Orientation I created the class CodigoBarraEAN
responsible for validating the barcode, the class diagram below shows its structure.
Class diagram CodigoBarraEAN
:
Class has only the attribute codigoBarra
, it will receive the 13 digits of the barcode. The class has two constructs an empty constructor and the other one that receives the barcode.
Explanation of methods.
All of the methods below are responsible for the validation of the barcode, and only one method can be accessed which is the validar()
, it returns a string saying whether the barcode is valid or invalid.
- Method
getCodigoBarra()
and setCodigoBarra()
public: provides access
to the attribute codigoBarra
.
- Method
validar()
public: is the method responsible for the validation of
barcode, it implements the other private methods and compares the checker digit with the checker digit returned by the calculation, if both are equal the code is validated.
- Method
obterNumeroPosicao()
private: get position numbers
odd or even that will be used in the calculation.
- Method
somarNumeros()
private: sum all numbers in a list
of type numbers List<Integer>
.
- Method
removerDigitoVerificador()
private: removes last digit
bar code which is the digit checker.
- Method
obterDigitoVerificador()
private: get the type checker
of the bar code.
- Method
validarEAN()
private: checks whether the barcode is
within the EAN13 standard containing the 13 digits.
- Method
calcularDigitoVerificador()
private: calculates and returns the
type barcode checker, to calculate the digit
checker it is necessary to pass only the 12 digits without the digit
verifier.
Follow all class code CodigoBarraEAN
down below:
package codigobarraeanverificador;
import java.util.ArrayList;
import java.util.List;
/**
* @author Dener
*/
public class CodigoBarraEAN{
private String codigoBarra;
public CodigoBarraEAN(String codigoBarra){
this.codigoBarra = codigoBarra;
}
public CodigoBarraEAN(){
}
public String getCodigoBarra(){
return codigoBarra;
}
public void setCodigoBarra(String codigoBarra){
this.codigoBarra = codigoBarra;
}
//Métodos de verificação e validação do codigo de barras.
public String validar(CodigoBarraEAN codigoBarra){
String valido;
if (validarEAN(codigoBarra.getCodigoBarra())){
int digitoVerificador = obterDigitoVerificador(codigoBarra.getCodigoBarra());
valido = (calcularDigitoVerificador(removerDigitoVerificador(codigoBarra.getCodigoBarra())) == digitoVerificador) ? "OK" : "Inválido";
}
else
valido = "Inválido";
return valido;
}
private List<Integer> obterNumeroPosicao(String codigoBarra, String imparOuPar){
List<Integer> numeros = new ArrayList<>();
for (int i = 0, posicao = 1; i < codigoBarra.length() - 1; i++){
if ((posicao % 2 != 0))
numeros.add(Integer.parseInt(String.valueOf(codigoBarra.charAt(imparOuPar.equals("impar") ? posicao - 1 : posicao))));
posicao++;
}
return numeros;
}
private int somarNumeros(List<Integer> numeros){
return numeros.stream().reduce(0, Integer::sum);
}
private String removerDigitoVerificador(String codigoBarra){
return codigoBarra.substring(0, codigoBarra.length() -1);
}
private int obterDigitoVerificador(String codigoBarra){
return Integer.parseInt(String.valueOf(codigoBarra.charAt(codigoBarra.length() - 1)));
}
private boolean validarEAN(String codigoBarra){
return (codigoBarra.length() == 13);
}
private int calcularDigitoVerificador(String codigoBarra){
int somaPar = somarNumeros(obterNumeroPosicao(codigoBarra, "par")),
somaImpar = somarNumeros(obterNumeroPosicao(codigoBarra, "impar"));
int multiplicaPar = somaPar * 3;
int resultado = somaImpar + multiplicaPar;
int digitoVerificador = 0;
int auxiliar = 0;
while ((resultado % 10) != 0){
digitoVerificador++;
resultado += digitoVerificador - auxiliar;
auxiliar = digitoVerificador;
}
return digitoVerificador;
}
}
Example of use of the class CodigoBarraEAN
:
package codigobarraeanverificador;
import java.util.Scanner;
/**
* @author Dener
*/
public class CodigoBarraEANVerificador{
public static void main(String[] args){
System.out.println("Informa o código de barra: ");
String codigo = new Scanner(System.in).nextLine();
CodigoBarraEAN codigoBarra = new CodigoBarraEAN(codigo);
System.out.println("Codigo de barra: " + codigoBarra.validar(codigoBarra));
System.out.println("Numero do codigo de barras: " + codigoBarra.getCodigoBarra());
}
}
Note:
The class meets the requirements of the question, does not verify the country of
origin and does not validate the product code or company number
manufacturer.
Sources:
Unraveling the mysteries of barcodes:
http://www.revistacliche.com.br/2013/10/desvendando-os-misterios-dos-codigos-de-barras/
EAN-13:
https://en.wikipedia.org/wiki/International_Article_Number
That method of Validation doesn’t suit you?
– Marcelo de Andrade
@Marcelodeandrade used the method of this link but performs the wrong calculation.
– gato
@Bacco is for college, must create a class that makes the validation, do not worry I’ll answer because I have already solved here. I will edit the question and specify it better in detail.
– gato
@It’s just, as you put it in the question, I was wondering what OO was going to do to help with the EAN problem. It’s just that I don’t know. If you solved it, it’s good to post, in case someone else needs it.
– Bacco
@Bacco tranquilow :) So I reply you can help me by improving the code or giving an answer if you want :D.
– gato
@I denigrate how I do not use Java, I just invented not responding, but after you post, if there is something I can opine on, I comment. I’m finishing a bit here, then I do a little research on the EAN calculation.
– Bacco
@Bacco answered the question.
– gato