如何使用primefaces photocam功能检测QR码,拍摄快照并在java中解码

时间:2017-05-12 05:40:32

标签: java jsf primefaces zxing

我一直在尝试使用zxing和primefaces扫描QR码

    filename = getRandomImageName();
    byte[] data = captureEvent.getData();

    ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext()
            .getContext();
    String filePathToImage = servletContext.getRealPath("") + filename + ".jpeg";

    FileImageOutputStream imageOutput;
    try {
        imageOutput = new FileImageOutputStream(new File(filePathToImage));
        imageOutput.write(data, 0, data.length);
        imageOutput.close();
        String filePath = filePathToImage;
        String charset = "UTF-8"; // or "ISO-8859-1"
        Map hintMap = new HashMap();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        String scannedDetails = readQRCode(filePath, charset, hintMap);
        System.out.println("Data read from QR Code: " + scannedDetails);



        RequestContext.getCurrentInstance().execute("PF('QRcodeDialog').hide();");
        RequestContext.getCurrentInstance().update("baseTemplateForm:receiver");
    } catch (IOException e) {
        showMessage(Constants.SUCCESS_MESSAGE_ID, FacesMessage.SEVERITY_ERROR,
                "QR code not scanned properly. Please scan again.", null);
        throw new FacesException("Error in writing captured image.", e);

    } catch (NotFoundException e) {

        showMessage(Constants.MESSAGE_ID_FOR_QR, FacesMessage.SEVERITY_ERROR,
                "QR code not scanned properly. Please scan again.", null);
    } catch (CommandCenterException e) {
        logger.error("Error in fetching receiver details");
    }

我想要的是连续扫描图像并自行检测QR码,而不是单击按钮调用此功能。

1 个答案:

答案 0 :(得分:0)

要进行连续扫描是不可能的,而不是您想要做的事情,请记住带宽和服务器负载。您可以做的是使用p:poll每隔几秒钟拍摄一次照片。使用photoWidthphotoHeightjpegQuality来节省带宽。尽可能地减少它们。另外,不要立即开始投票,而是在点击按钮开始时,一旦收到QR码就立即停止投票。