Scanner un codigo QR con primefaces
Proyecto:
https://github.com/avbravo/foto
Usaremos el componente <p:photocam> para obtener una foto
Usamos Firefox, Google Chrome no permite si no es un sitio https
Dar clic en Capture QR para capturar la imagen
si logra detectrarlo
Dependencias
<dependency>
<groupId>com.github.avbravo</groupId>
<artifactId>avbravoutils</artifactId>
<version>0.27.1</version>
</dependency>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
pagina .xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
<script type="text/javascript"> function captureWebCam() {
Webcam.set({width: 600, height: 450, image_format: 'png', jpeg_quality: 90});
Webcam.attach('#my_camera');
}</script>
</h:head>
<h:body>
<h:form>
<p:messages id="msg" autoUpdate="true"/>
<h:panelGrid>
<p:photoCam widgetVar="pcQR" listener="#{webQR.oncapture}" update="photoQR"
/>
<p:commandButton type="button" value="Capture QR" onclick="PF('pcQR').capture()"/>
<p:outputPanel id="photoQR">
<p:graphicImage name="/home/avbravo/Descargas/#{webQR.filename}.png" rendered="#{not empty webQR.filename}"/>
</p:outputPanel>
</h:panelGrid>
</h:form>
</h:body>
</html>
Controller
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.avbravo.foto;
import com.avbravo.avbravoutils.JsfUtil;
import com.avbravo.avbravoutils.QR;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import javax.inject.Named;
import javax.faces.FacesException;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.imageio.stream.FileImageOutputStream;
import org.primefaces.event.CaptureEvent;
/**
*
* @author avbravo
*/
@Named
@ViewScoped
public class WebQR implements Serializable {
private String filename;
public String getFilename() {
return filename;
}
public void oncapture(CaptureEvent captureEvent) {
filename = "";
byte[] data = captureEvent.getData();
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
// String newFileName = externalContext.getRealPath("") + File.separator + "resources" + File.separator + "demo" +
// File.separator + "images" + File.separator + "photocam" + File.separator + filename + ".jpeg";
String name = JsfUtil.getUUIDMinusculas();
String newFileName = "/home/avbravo/Descargas/" + name + ".png";
this.filename=newFileName;
FileImageOutputStream imageOutput;
try {
imageOutput = new FileImageOutputStream(new File(newFileName));
imageOutput.write(data, 0, data.length);
imageOutput.close();
JsfUtil.successMessage("Imagen guardada " + name + ".png");
scanQR(this.filename);
} catch (IOException e) {
throw new FacesException("Error in writing captured image.", e);
// JsfUtil.errorMessage("Error() "+e.getLocalizedMessage());
}
}
public String scanQR(String name) {
try {
File file = new File(name);
String decodedText = QR.decodificarQRCode(name, false);
if (decodedText == null) {
JsfUtil.errorDialog("Error", "No QR Code found in the imageÑ");
//System.out.println("No QR Code found in the image");
} else {
JsfUtil.successMessage("Decoded text = " + decodedText);
// System.out.println("Decoded text = " + decodedText);
}
} catch (Exception e) {
JsfUtil.errorMessage("Could not decode QR Code, IOException " + e.getLocalizedMessage());
System.out.println("Could not decode QR Code, IOException :: " + e.getMessage());
}
return "";
}
}
Last updated