I try to make captcha using JCaptcha Component and i need to encode image in related servlet. So according to JCaptcha Tutorial, I should use JPEGImageEncoder and it's not compatible with JDK7.
What else can i do?
here is related part of code:
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
BufferedImage challenge =
CaptchaServiceSingleton.getInstance().getImageChallengeForID(captchaId,
httpServletRequest.getLocale());
JPEGImageEncoder jpegEncoder =
JPEGCodec.createJPEGEncoder(jpegOutputStream);
jpegEncoder.encode(challenge);
} catch (IllegalArgumentException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} catch (CaptchaServiceException e) {
httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
Use ImageIO:
// Image writer
ImageWriter imageWriter = (ImageWriter)ImageIO.getImageWritersBySuffix("jpeg").next();
ImageOutputStream ios = ImageIO.createImageOutputStream(jpegOutputStream);
imageWriter.setOutput(ios);
IIOMetadata imageMetaData = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(challenge), null);
imageWriter.write(imageMetaData, new IIOImage(challenge, null, null), null);
This is replacing the deprecated JPEG functions
来源:https://stackoverflow.com/questions/28579709/jpegimageencoder-dont-work-in-jdk-7