本文介绍了JPEGImageEncoder在JDK 7中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用JCaptcha组件制作验证码,并且我需要在相关的servlet中对图像进行编码.因此,根据JCaptcha教程,我应该使用JPEGImageEncoder,它与JDK7不兼容.我还能做什么?
这是代码的相关部分:
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;
}
推荐答案
使用 ImageIO :
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);
这将替换内部和不推荐使用的JPEG功能.
This replaces the internal and deprecated JPEG functions.
这篇关于JPEGImageEncoder在JDK 7中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!