How can I save the image of a fingerprint reader in Mysql?

4

I would like to save the image, (NOT the template) of a fingerprint image, the code to convert it according to the DigitalPersona manual is this:

public Image CrearImagenHuella(DPFPSample sample) 
{
 return 
DPFPGlobal.getSampleConversionFactory().createImage(sample);
}

And the code I use to save Mysql is this:

try 
{   
     Connection c=cn.conectar();
     PreparedStatement guardarStmt = c.prepareStatement("INSERT INTO somhue(hnombre,identificacion, huehuella) values(?,?,?)");
     guardarStmt.setString(1,nombre);
     guardarStmt.setInt(2, Id);
     guardarStmt.setBinaryStream(3,in);
     //Ejecuta la sentencia
     guardarStmt.execute();
     guardarStmt.close();
     JOptionPane.showMessageDialog(null,"Huella Guardada Correctamente");
     cn.desconectar();
     BtnGuardar.setEnabled(false);

}
 catch (SQLException ex)
{
     //Si ocurre un error lo indica en la consola
     System.err.println("Error al guardar los datos de la huella.");
     JOptionPane.showMessageDialog(null, "La persona ya se encuentra registrada por favor verificar los datos e intentar de nuevo ","VERIFICACION DE DATOS", JOptionPane.ERROR_MESSAGE);

}
finally
{
cn.desconectar();

}

However I could not use that variable "sample" inside the code to save in mysql, someone who can tell me how to store it?

    
asked by Jorge Herrera 23.08.2016 в 18:43
source

2 answers

3

setBinaryStream requires in the second parameter an object of type InputStream

But what you have is an object of type Image , since the code is not complete I guess java.awt.Image

If the object you have was of type BufferedImage it would be easy to convert it to a stream of bytes:

Image imagen = CrearImagenHuella(sample);
BufferedImage buffImg = convertirABufferedImage( imagen );
ByteArrayOutputStream streamWrite = new ByteArrayOutputStream();
ImageIO.write(image,"png", streamWrite); // Puede que quieras otro formato distinto
InputStream in = new ByteArrayInputStream(streamWrite.toByteArray());

/// ... Y ya puedes usar .....

guardarStmt.setBinaryStream(3,in);

Now we just need to convert the image to Buffered image, which I have taken from this English answer from Sri Harsha Chilakapati

/**
 * Convierte una imagen en BufferedImage
 *
 * @param img La Image a convertir
 * @return El BufferedImage convertido
 */
public static BufferedImage convertirABufferedImage(Image img)
{
    if (img instanceof BufferedImage)
    {
        return (BufferedImage) img;
    }

    // Crear imagen buffered con transparencia
    BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);

    // Dibujar la imagen en la buffered imagen
    Graphics2D bGr = bimage.createGraphics();
    bGr.drawImage(img, 0, 0, null);
    bGr.dispose();

    // Devolver la buffered image
    return bimage;
}
    
answered by 24.08.2016 / 13:52
source
2

Thanks to the response of Jose Antonio Dura Olmos , I was able to solve it. Publish the final solution in case a user serves.

In this case by means of BufferedImage I did the following:

  • Declare a variable called Ejemplo

    private DPFPCapture Lector = DPFPGlobal.getCaptureFactory().createCapture();
    
    private DPFPEnrollment Reclutador = DPFPGlobal.getEnrollmentFactory().createEnrollment();
    
    private DPFPVerification Verificador = DPFPGlobal.getVerificationFactory().createVerification();
    
    private DPFPTemplate template;
    
    public BufferedImage Ejemplo;
    
  • Then this variable is assigned image , which is what the reader draws me.

    Image image=CrearImagenHuella(sample);
    DibujarHuella(image);
    Ejemplo =  (BufferedImage) image;
    
  • Inside the Guardar method I have the following code:

    public void guardarHuella(DPFPSample sample)
        {
        try
         {
    
          //Obtiene los datos del template de la huella actual
    
          String formato = "JPEG";
    
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          if(Ejemplo !=null)
    
           ImageIO.write((RenderedImage) Ejemplo, formato, out);
            DibujarHuella1( Ejemplo);
    
          InputStream in = new ByteArrayInputStream(out.toByteArray());
          ByteArrayInputStream datosHuella = new ByteArrayInputStream(template.serialize());
          Integer tamañoHuella=template.serialize().length;
    
          //Pregunta el nombre de la persona a la cual corresponde dicha huella
          String nombre;
          String Identificacion;
          nombre = JOptionPane.showInputDialog("Nombre:");
          Identificacion = JOptionPane.showInputDialog(null,"NUMERO DE IDENTIFICACION:"+"\n" +"NO colocar puntos ni guiones","DATOS PERSONALES",JOptionPane.INFORMATION_MESSAGE);
          //se creó la variable tipo entero llamada Id
          int Id;
          //Convierte la variable Identificacion la cual esta de tipo String a entero por medio de la variable Id
          Id =Integer.parseInt(Identificacion);
            {
              try
            {                    
                  //Establece los valores para la sentencia SQL
                  Connection c=cn.conectar();
                  PreparedStatement guardarStmt = c.prepareStatement("INSERT INTO somhue(hnombre,identificacion, huehuella) values(?,?,?)");
                  guardarStmt.setString(1,nombre);
                  guardarStmt.setInt(2, Id);
                  guardarStmt.setBinaryStream(3,in);
                  //Ejecuta la sentencia
                  guardarStmt.execute();
                  guardarStmt.close();
                  JOptionPane.showMessageDialog(null,"Huella Guardada Correctamente");
                  cn.desconectar();
                  BtnGuardar.setEnabled(false);
    
                  }
               catch (SQLException ex) 
                 {
                  //Si ocurre un error lo indica en la consola
                  System.err.println("Error al guardar los datos de la huella.");
                  JOptionPane.showMessageDialog(null, "La persona ya se encuentra registrada por favor verificar los datos e intentar de nuevo ","VERIFICACION DE DATOS", JOptionPane.ERROR_MESSAGE);     
              }
              finally
              {
                  cn.desconectar();   
              }      
          }
      } catch (IOException ex) {
          Logger.getLogger(Huella.class.getName()).log(Level.SEVERE, null, ex);
           }
       }
    
  • And at the end when I put the visualization in the label, I could already see the image of the fingerprint and store it in the database. It weighs 60KB which is not much.

        
    answered by 24.08.2016 в 18:40