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:net.sourceforge.atunes.utils.TempFolder.java

@Override
public File addImage(final RenderedImage image, final String fileName) {
    try {/*from w w w. j a  va  2s.  co  m*/
        File file = new File(this.osManager.getTempFolder(), fileName);
        ImageIO.write(image, "png", file);
        return file;
    } catch (IOException e) {
        return null;
    }
}

From source file:com.smash.revolance.ui.model.helper.ImageHelper.java

public static String encodeToString(BufferedImage image) throws IOException {
    String imageString = "";
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {/*from  w w  w. j a v  a2  s.c om*/
        Base64 encoder = new Base64();

        ImageIO.write(image, "png", bos);
        byte[] bytes = bos.toByteArray();

        imageString = BASE64_IMAGE_PNG + encoder.encodeToString(bytes);

    } finally {
        IOUtils.closeQuietly(bos);
    }
    return imageString;
}

From source file:net.pkhsolutions.pecsapp.control.PictureFileStorage.java

/**
 * TODO Document me/* w  w  w  . j  a  va 2 s.c  om*/
 *
 * @param descriptor
 * @param layout
 * @param image
 * @throws IOException
 */
public void store(@NotNull PictureDescriptor descriptor, @NotNull Optional<PageLayout> layout,
        @NotNull BufferedImage image) throws IOException {
    final Path path = getDirectoryForLayout(layout)
            .resolve(String.format("%d.%s", descriptor.getId(), descriptor.getMimeType().getSubtype()));
    LOGGER.debug("Storing image in {}", path);
    try (OutputStream outputStream = Files.newOutputStream(path)) {
        ImageIO.write(image, descriptor.getMimeType().getSubtype(), outputStream);
    }
}

From source file:com.liusoft.dlog4j.util.ImageUtils.java

/**
 * ?/*  ww w . j  a  va 2s .c o m*/
 * @param orig_img
 * @param obj_filename
 * @param p_width
 * @param p_height
 * @throws IOException
 */
public static String createPreviewImage(InputStream orig_img, String obj_filename, int p_width, int p_height)
        throws IOException {
    String extendName = StringUtils.getFileExtend(obj_filename).toLowerCase();

    FileOutputStream newimage = null;
    InputStream fis = orig_img;
    try {
        if ("gif".equalsIgnoreCase(extendName)) {
            GifImage gifImage = GifDecoder.decode(fis);
            fis.close();
            fis = null;
            GifImage newGif = GifTransformer.resize(gifImage, p_width, p_height, false);
            newimage = new FileOutputStream(obj_filename);
            GifEncoder.encode(newGif, newimage);
        } else {
            BufferedImage orig_portrait = (BufferedImage) ImageIO.read(fis);
            fis.close();
            fis = null;
            // ?JPG?
            BufferedImage bi = new BufferedImage(p_width, p_height, BufferedImage.TYPE_INT_RGB);
            bi.getGraphics().drawImage(orig_portrait, 0, 0, p_width, p_height, null);
            if (!obj_filename.endsWith(".jpg"))
                obj_filename += ".jpg";
            newimage = new FileOutputStream(obj_filename);
            ImageIO.write(bi, "jpg", newimage);
        }
    } finally {
        if (newimage != null)
            newimage.close();
        if (fis != null)
            fis.close();
    }
    return obj_filename;
}

From source file:$.SayHiImage.java

private void writeImage(OutputStream out) throws IOException {
        BufferedImage img = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = img.createGraphics();

        g2d.setPaint(Color.red);/*  ww w  . ja  v  a  2 s . com*/
        g2d.setFont(new Font("Serif", Font.BOLD, 36));
        g2d.drawString("Hi there, how are you doing today?", 5, g2d.getFontMetrics().getHeight());
        g2d.dispose();

        ImageIO.write(img, "jpg", out);
    }

From source file:net.bafeimao.umbrella.web.controller.CaptchaController.java

@RequestMapping("")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setDateHeader("Expires", 0);
    // Set standard HTTP/1.1 no-cache headers.
    response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
    response.addHeader("Cache-Control", "post-check=0, pre-check=0");
    // Set standard HTTP/1.0 no-cache header.
    response.setHeader("Pragma", "no-cache");
    // return a jpeg
    response.setContentType("image/jpeg");
    // create the text for the image
    String capText = captchaProducer.createText();
    // store the text in the session
    request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
    // create the image with the text
    BufferedImage bi = captchaProducer.createImage(capText);
    ServletOutputStream out = response.getOutputStream();
    // write the data out
    ImageIO.write(bi, "jpg", out);

    try {/*ww  w  .  jav a 2  s .c om*/
        out.flush();
    } finally {
        out.close();
    }

    return null;
}

From source file:com.seleniumtests.util.imaging.ImageProcessor.java

/**
 * Agregate 2 pictures/*from   www .  j a  va  2s.c  o m*/
 * @param imgf1         first picture to agregate
 * @param imgf2         seconde picture to aggregate
 * @param finalImage   file to create
 * @param img2PosX      X coord where second picture will be but, relative to first one 
 * @param img2PosY      Y coord where second picture will be but, relative to first one 
 */
public static void concat(File imgf1, File imgf2, File finalImage, Integer img2PosX, Integer img2PosY) {

    try {
        BufferedImage img1 = ImageIO.read(imgf1);
        BufferedImage img2 = ImageIO.read(imgf2);
        ImageIO.write(concat(img1, img2, img2PosX, img2PosY), "jpg", finalImage);
    } catch (IOException e) {
    }
}

From source file:ImageUtil.java

public static ByteArrayOutputStream smartCrop(ByteArrayInputStream bais, int width, int height)
        throws IOException {
    BufferedImage src = ImageIO.read(bais);

    Float scale;//from w ww . j  a  v  a 2  s  . c o  m
    if (src.getWidth() > src.getHeight()) {
        scale = Float.valueOf(height) / Float.valueOf(src.getHeight());
        if (src.getWidth() * scale < width) {
            scale = Float.valueOf(width) / Float.valueOf(src.getWidth());
        }
    } else {
        scale = Float.valueOf(width) / Float.valueOf(src.getWidth());
        if (src.getHeight() * scale < height) {
            scale = Float.valueOf(height) / Float.valueOf(src.getHeight());
        }
    }
    //        
    //        System.out.println("--- " + src.getWidth() + " - " + width);
    //        System.out.println("--- " + src.getHeight() + " - " + height);
    //        System.out.println("--- " + scale + " -- " + Float.valueOf(src.getWidth() * scale).intValue() + " -- " + Float.valueOf(src.getHeight() * scale).intValue());
    //        
    BufferedImage temp = scale(src, Float.valueOf(src.getWidth() * scale).intValue(),
            Float.valueOf(src.getHeight() * scale).intValue());

    temp = crop(temp, width, height);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(temp, "JPG", baos);

    return baos;
}

From source file:com.joliciel.jochre.pdf.PdfImageSaverImpl.java

@Override
void visitImage(BufferedImage image, String imageName, int pageIndex, int imageIndex) {

    // Each page gets its own directory
    String pageDirectory = this.outputDirectory + SEPARATOR + pageIndex;

    File pagePath = new File(pageDirectory);
    if (pagePath.exists() == false)
        pagePath.mkdirs();//from w  ww  .j av a2  s.c o  m

    String fileName = this.getPdfFile().getName().substring(0, this.getPdfFile().getName().lastIndexOf('.'));
    fileName += "_" + pageIndex + "_" + imageIndex + "." + SUFFIX;
    try {
        ImageIO.write(image, SUFFIX, new File(pageDirectory + SEPARATOR + fileName));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:io.awacs.demo.TestController.java

@RequestMapping("/img")
public void img(HttpServletRequest request, HttpServletResponse response) throws Exception {
    try {/*from w w w  .ja va  2  s.com*/
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int r = random.nextInt(36);
            bis2();
            if (r < 10) {
                sb.append((char) ('0' + r));
            } else {
                sb.append((char) ('A' + r - 10));
            }
        }
        BufferedImage img = cage.drawImage(sb.toString());
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        ServletOutputStream responseOutputStream = response.getOutputStream();
        ImageIO.write(img, "JPEG", responseOutputStream);
    } catch (Exception e) {
        throw e;
    }
}