如何压缩要通过HttpUrlConnection从applet发送到控制器的图像?

时间:2016-05-31 16:33:21

标签: java spring applet http-compression

我解决了将通过HttpUrlConnection从小程序发送到控制器的ScuGen Finger print扫描图像的问题,代码似乎正在运行,但我需要提供建议。

当我点击Verify FingerPrint Image Button时,这是applet中的代码:

    BufferedImage img1gray = new BufferedImage(deviceInfo.imageWidth, deviceInfo.imageHeight, BufferedImage.TYPE_BYTE_GRAY);
    float imageQuality = 0.5f;

    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
    if (!writers.hasNext()){
        throw new IllegalStateException("No writers found");
    }else{
        ImageWriter writer = (ImageWriter) writers.next();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
        writer.setOutput(ios);
        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(imageQuality);
        writer.write(null, new IIOImage(img1gray, null, null), param);
        byte[] data = baos.toByteArray();
        HttpURLConnection connection = getHttpUrlConnection(data);
        PrintWriter outputStream = new PrintWriter(connection.getOutputStream());
        outputStream.write("image="+Base64.encodeBase64String(data)+"&index="+String.valueOf(jComboBoxFPIndex.getSelectedItem()));
        outputStream.flush();
        outputStream.close();
        int responseCode = connection.getResponseCode();
        if(responseCode == HttpURLConnection.HTTP_OK){
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            response = new StringBuffer();
            while ((inputLine = reader.readLine()) != null) {
                 response.append(inputLine);
             }
            this.jLabelStatus.setText(response.toString());
        }else{
            this.jLabelStatus.setText("Response Code = "+responseCode);
        }
    }
    /*OutputStream outputStream = connection.getOutputStream();
    outputStream.write(data);*/
} catch (Exception e) {
    System.err.println(e.getMessage());
}

以下是我收到图片的控制器:

@RequestMapping(value = "/verifyfingerprint", method = RequestMethod.POST)
public @ResponseBody String verifyFingerPrint(HttpServletRequest request) throws BusinessException, IOException {
    Loggers.logger.info("Verify Finger Print Image Start");
    Loggers.logger.debug("Usertype = "+attribute.getType());
    String message;

    String fingerPrintImage = request.getParameter("image");
    String fingerPrintIndex = request.getParameter("index");
    if(fingerPrintImage == null || "".equals(fingerPrintImage)){
        message = getResourceMessageHandler().getMessage("verify.finger.print.image.invalid", new String[]{}, Locale.ENGLISH);
        Loggers.logger.error(message);
        return message;
    }

    if(fingerPrintIndex == null || "".equals(fingerPrintIndex)){
        message = getResourceMessageHandler().getMessage("verify.finger.print.index.invalid", new String[]{}, Locale.ENGLISH);
        Loggers.logger.error(message);
        return message;
    }

    FingerPrintValidationRequest fingerPrintValidationRequest = getRegistrationService().prepearFingerPrintValidationRequest(attribute, fingerPrintImage, fingerPrintIndex);
    RestBuilder builder = RestBuilder.getInstance(null);
    AjaxResult ajaxResult = builder.validateFingerPrintImage(fingerPrintValidationRequest,attribute,getRegistrationService(),getResourceMessageHandler());
    Loggers.logger.debug(ajaxResult.toString());
    message = ajaxResult.getMessage().get(0);
    Loggers.logger.info("Verify Finger Print Image End");
    return message;
}

0 个答案:

没有答案