EAN13 Bar Code
The EAN13 barcode belongs to the GS1 System which is an official model for the standardization of product identification and commercial management processes, which has been in existence since 2006 and consists of a 13-digit sequence and its symbology represents the following items :
- Identification of the product's country of origin.
- Name of manufacturer.
- Product identification number.
- Type checker.
See an image that illustrates its symbology:
Calculation
Oneofthemainrequirementstocheckifabarcodeisvalidistodotheverificationdigitcalculation,seehowthecalculationisdone:
Supposeweareusingthebarcode:789162731405and wewanttoknowwhatthefinaldigitis.(Verifier)
Addallthedigitsofoddpositions(digits7,9,6,7,1and0): 7+9+6+7+1+0=30
Addallthedigitsofevennumbers(digits8,1,2,3,4and5): 8+1+2+3+4+5=23
Multiplythesumofthedigitsofevennumbersby3,see:23*3=69
Addthetworesultsoftheprevioussteps,see:30+69=99
Determinethenumberthatshouldbeaddedtothesumresultfor ifyoucreateamultipleof10,see:99+1=100
Therefore,thecheckdigitis1.
Implementation
UsingObjectOrientationIcreatedtheclassCodigoBarraEAN
responsibleforbarcodevalidation,theclassdiagrambelowshowsitsstructure.
DiagramofclassCodigoBarraEAN
:
TheclasshasonlythecodigoBarra
attribute,itwillreceivethe13digitsofthebarcode.Theclasshastwoconstructorsoneemptyconstructorandtheotheronethatreceivesthebarcode.
Explanationofmethods.
Allmethodsbelowareresponsibleforbarcodevalidation,andonlyonemethodcanbeaccessedwhichisvalidar()
,itreturnsastringsayingwhetherthebarcodeisvalidorinvalid.
- Method
getCodigoBarra()
andsetCodigoBarra()
public:providesaccesstothecodigoBarra
attribute. - Public%method:isthemethodresponsibleforvalidatingthebarcode,itimplementstheotherprivatemethodsandcomparesthecheckerdigitwiththecheckerdigitreturnedbythecalculation,ifbothareequalthecodeisvalid.
- Method%private%:getsthenumbersofthepositionsoddorevennumbersthatwillbeusedinthecalculation.
- Method
validar()
private:addsallthenumbersinalistoftypenumbersobterNumeroPosicao()
. - Method%withprivate%:removesthelastdigitofthebarcodethatisthescanningdigit.
- Method%private%:obtainstheverifierdigitofthebarcode.
- Method%withprivate%:checkstoseeifthebarcodeiswithintheEAN13standardcontainingthe13digits.
- Method
somarNumeros()
private:calculatesandreturnsthebarcodescanner,tocalculatethedigitverifieritisnecessarytopassonlythe12digitswithoutthedigitchecker.
FollowthewholecodeofclassList<Integer>
below:
packagecodigobarraeanverificador;importjava.util.ArrayList;importjava.util.List;/***@authorDener*/publicclassCodigoBarraEAN{privateStringcodigoBarra;publicCodigoBarraEAN(StringcodigoBarra){this.codigoBarra=codigoBarra;}publicCodigoBarraEAN(){}publicStringgetCodigoBarra(){returncodigoBarra;}publicvoidsetCodigoBarra(StringcodigoBarra){this.codigoBarra=codigoBarra;}//Métodosdeverificaçãoevalidaçãodocodigodebarras.publicStringvalidar(CodigoBarraEANcodigoBarra){Stringvalido;if(validarEAN(codigoBarra.getCodigoBarra())){intdigitoVerificador=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 use of class removerDigitoVerificador()
:
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 check the
source code and does not validate the product code or the
manufacturer.
Fonts :
Unraveling the mysteries of bar codes:
link
EAN-13:
link