barras em Java
Veja neste artigo como usar a biblioteca Zxing em Java para
fazer leitura de cdigo de barras em imagens.
(1) (0)
Zxing
Nem sempre temos o leitor de cdigo de barras (scanner) em mos ou
temos situaes em que precisamos dispensar o seu uso e aqui que
entra a API Zxing. Para entender melhor, vamos a um cenrio real: O
seu sistema deve possuir um mdulo onde o usurio far o upload de
contratos assinados pelo seu cliente (imagens) e o sistema deve
associar automaticamente estas imagens com o cliente correto sem
nenhuma interveno humana. Na capa do contrato, que pode possuir
N folhas, h um cdigo de barras que identifica a numerao do
<AuxValues>
<AuxValue name="FormSettings_generateMnemonicsCode"
type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget"
type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle"
type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal"
type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier"
type="java.lang.Integer" value="2"/>
</AuxValues>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jLabel1" alignment="0" min="-2"
max="-2" attributes="0"/>
<Component id="jLabel2" alignment="0" min="-2"
max="-2" attributes="0"/>
<Component id="jTextFieldImagem" alignment="0"
min="-2" pref="329" max="-2" attributes="0"/>
<Component id="jTextFieldResultado" alignment="0"
min="-2" pref="329" max="-2" attributes="0"/>
<Group type="102" alignment="0" attributes="0">
<Component id="jButtonSelecionar" min="-2"
max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="jButtonProcessar" min="-2"
max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace pref="25" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
</Component>
<Component class="javax.swing.JTextField"
name="jTextFieldResultado">
</Component>
<Component class="javax.swing.JButton" name="jButtonSelecionar">
<Properties>
<Property name="text" type="java.lang.String"
value="Selecionar Imagem"/>
</Properties>
<Events>
<EventHandler event="actionPerformed"
listener="java.awt.event.ActionListener"
parameters="java.awt.event.ActionEvent"
handler="jButtonSelecionarActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="jButtonProcessar">
<Properties>
<Property name="text" type="java.lang.String"
value="Processar"/>
</Properties>
<Events>
<EventHandler event="actionPerformed"
listener="java.awt.event.ActionListener"
parameters="java.awt.event.ActionEvent"
handler="jButtonProcessarActionPerformed"/>
</Events>
</Component>
</SubComponents>
</Form>
private void
jButtonSelecionarActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_jButtonSelecionarActionPerformed
JFileChooser fChooser = new JFileChooser();
fChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (fChooser.showSaveDialog(this) != 1){
File fImage = fChooser.getSelectedFile();
try {
buffImage = ImageIO.read(fImage);
jTextFieldImagem.setText(fImage.getAbsolutePath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}//GEN-LAST:event_jButtonSelecionarActionPerformed
//1
LuminanceSource source = new
BufferedImageLuminanceSource(buffImage);
//2
BinaryBitmap bitmap = new BinaryBitmap(new
HybridBinarizer(source));
//3
Reader reader = new MultiFormatReader();
//4
Result result = reader.decode(bitmap);
jTextFieldResultado.setText(result.getText());
} catch (NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ChecksumException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//GEN-LAST:event_jButtonProcessarActionPerformed
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Imagem");
jLabel2.setText("Resultado");
jTextFieldImagem.setEnabled(false);
jButtonSelecionar.setText("Selecionar Imagem");
jButtonSelecionar.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jButtonSelecionarActionPerformed(evt);
}
});
jButtonProcessar.setText("Processar");
jButtonProcessar.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jButtonProcessarActionPerformed(evt);
}
});
.addComponent(jTextFieldResultado,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment
.BASELINE)
.addComponent(jButtonSelecionar)
.addComponent(jButtonProcessar))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void
jButtonProcessarActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_jButtonProcessarActionPerformed
try {
if (buffImage == null){
JOptionPane.showMessageDialog(this, "Voc deve
escolher a imagem !!");
return;
}
}//GEN-LAST:event_jButtonProcessarActionPerformed
private void
jButtonSelecionarActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_jButtonSelecionarActionPerformed
JFileChooser fChooser = new JFileChooser();
fChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (fChooser.showSaveDialog(this) != 1){
File fImage = fChooser.getSelectedFile();
try {
buffImage = ImageIO.read(fImage);
jTextFieldImagem.setText(fImage.getAbsolutePath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}//GEN-LAST:event_jButtonSelecionarActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FZxing().setVisible(true);
}
});
}
// Declarao de variveis - no modifique//GEN-BEGIN:variables
private javax.swing.JButton jButtonProcessar;
private javax.swing.JButton jButtonSelecionar;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextFieldImagem;