JPEGImageEncoder don't work in JDK 7

不想你离开。 提交于 2019-12-01 08:57:26

问题


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;
     }

回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!