Example usage for javax.imageio ImageIO write

List of usage examples for javax.imageio ImageIO write

Introduction

In this page you can find the example usage for javax.imageio ImageIO write.

Prototype

public static boolean write(RenderedImage im, String formatName, OutputStream output) throws IOException 

Source Link

Document

Writes an image using an arbitrary ImageWriter that supports the given format to an OutputStream .

Usage

From source file:com.ace.erp.filter.jcaptcha.JCaptchaFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {

    response.setDateHeader("Expires", 0L);
    response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    response.addHeader("Cache-Control", "post-check=0, pre-check=0");
    response.setHeader("Pragma", "no-cache");
    response.setContentType("image/jpeg");

    ServletOutputStream out = response.getOutputStream();

    try {//w  ww .  ja  v  a2 s .  co  m
        String id = request.getSession(true).getId();
        //String id = request.getRequestedSessionId();
        BufferedImage bi = JCaptcha.captchaService.getImageChallengeForID(id);

        ImageIO.write(bi, "jpg", out);
        out.flush();
    } finally {
        out.close();
    }
}

From source file:com.fanya.p2p.front.user.jcaptcha.JCaptchaFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {

    response.setDateHeader("Expires", 0L);
    response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    response.addHeader("Cache-Control", "post-check=0, pre-check=0");
    response.setHeader("Pragma", "no-cache");
    response.setContentType("image/jpeg");

    String id = request.getRequestedSessionId();
    BufferedImage bi = JCaptcha.captchaService.getImageChallengeForID(id);

    ServletOutputStream out = response.getOutputStream();

    ImageIO.write(bi, "jpg", out);
    try {/*  www  .  j a  va 2s . c o m*/
        out.flush();
    } finally {
        out.close();
    }
}

From source file:ImageProc.java

public static void imagesc(File file, int[][] classificationMat, int nClass, int imgDim1, int imgDim2) {
    BufferedImage image = new BufferedImage(imgDim2, imgDim1, BufferedImage.TYPE_INT_RGB);
    int index, rgb;
    float[][] jet = colormapJet(nClass);
    for (int i = 0; i < imgDim1; i++) {
        for (int j = 0; j < imgDim2; j++) {
            index = classificationMat[i][j];
            if (index == -1) {
                image.setRGB(j, i, Color.BLACK.getRGB());
            } else {
                rgb = new Color(jet[index][0], jet[index][1], jet[index][2]).getRGB();
                image.setRGB(j, i, rgb);
            }/*from w w  w  . j a  v  a2 s  . co m*/
        }
    }
    try {
        ImageIO.write(image, "png", file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.l1j5.web.common.utils.ImageUtils.java

public static void getImageThumbnail(BufferedInputStream stream_file, String save, String type, int w, int h) {

    try {//from w  w w. j a  v  a2s  .c o  m
        File file = new File(save);
        BufferedImage bi = ImageIO.read(stream_file);

        int width = bi.getWidth();
        int height = bi.getHeight();
        if (w < width) {
            width = w;
        }
        if (h < height) {
            height = h;
        }

        BufferedImage bufferIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Image atemp = bi.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);

        Graphics2D g2 = bufferIm.createGraphics();
        g2.drawImage(atemp, 0, 0, width, height, null);
        ImageIO.write(bufferIm, type, file);
    } catch (Exception e) {
        log.error(e);
    }

}

From source file:com.github.carlomicieli.nerdmovies.utility.ImageUtils.java

private static byte[] convertToArray(BufferedImage image, String contentType) throws IOException {
    byte[] imageInByte;

    String typeName = "jpg";
    if (contentType.equals(MediaType.IMAGE_PNG))
        typeName = "png";

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, typeName, baos);
    baos.flush();/*  w w  w.j  av a2  s. c om*/
    imageInByte = baos.toByteArray();
    baos.close();

    return imageInByte;
}

From source file:com.dianping.imcaptcha.strategy.ImageReverseStrategy.java

/**
 * (non-Jsdoc)/*from   w  w w  . ja va  2 s. c o  m*/
 * 
 * @see com.dianping.imcaptcha.strategy.ImageProcessStrategy#getImage()
 */
@Override
public ImageAnswerPair getImage() {
    int answer = 0;
    ReverseWaveFilter filter = new ReverseWaveFilter();
    BufferedImage bufferedImage = imageStorageService.getModified();
    filter.filter(bufferedImage, bufferedImage);
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    try {
        ImageIO.write(bufferedImage, "jpg", bs);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new ImageAnswerPair(new ByteArrayInputStream(bs.toByteArray()), answer);
}

From source file:controle.JfreeChartController.java

@PostConstruct
public void init() {
    try {//from   w ww . j  a va  2s. c o m
        //Graphic Text
        BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bufferedImg.createGraphics();
        g2.drawString("This is a text", 0, 10);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImg, "png", os);
        graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");

        //Chart
        JFreeChart jfreechart = ChartFactory.createLineChart("cosseno", "X", "Y", createDataset());
        File chartFile = new File("dynamichart");
        ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 800, 400);
        chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.spstudio.common.image.ImageUtils.java

public static String zoom(String sourcePath, String targetPath, int width, int height) throws IOException {
    File imageFile = new File(sourcePath);
    if (!imageFile.exists()) {
        throw new IOException("Not found the images:" + sourcePath);
    }// ww  w.j a  v a 2s.c  o m
    if (targetPath == null || targetPath.isEmpty()) {
        targetPath = sourcePath;
    }
    String format = sourcePath.substring(sourcePath.lastIndexOf(".") + 1, sourcePath.length());
    BufferedImage image = ImageIO.read(imageFile);
    image = zoom(image, width, height);
    ImageIO.write(image, format, new File(targetPath));
    return targetPath;
}

From source file:berlin.iconn.persistence.InOutOperations.java

public static void exportAsImage(float[][] data, Date date, String suffix) throws IOException {
    String path = imageExportFolder + "/" + getDirectoryNameByDate(date, suffix);
    mkdir(path);// w  w  w. j ava  2s  .  c o m
    for (int i = 0; i < data.length; i++) {
        BufferedImage image = DataConverter.pixelDataToImage(data[i], 0.0f, false);
        File file = new File(path + "/" + i + ".png");
        ImageIO.write(image, "png", file);
    }
}

From source file:costumetrade.common.verify.CapchaController.java

@EscapeLogin
@RequestMapping("/capcha/get")
public void get(String businessKey, HttpServletResponse response) throws IOException {

    if (StringUtils.isBlank(businessKey)) {
        throw new IllegalArgumentException("key?");
    }//from w w  w  . j a  v  a2  s  .c  o  m

    response.setContentType("image/png");
    response.setDateHeader("exprise", -1);
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    Pair<String, BufferedImage> codeImage = ImageVerification.create();
    captchaService.save(businessKey, codeImage.first());
    OutputStream outputStream = response.getOutputStream();
    ImageIO.write(codeImage.second(), "png", outputStream);
    outputStream.flush();
}