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.ewcms.component.checkcode.web.ImageCaptchaServlet.java

private void createCheckCodeImage(final String id, final Locale locale, final OutputStream stream)
        throws CaptchaServiceException, IOException {
    CaptchaService service = getCaptchaService();
    BufferedImage challenge = (BufferedImage) service.getChallengeForID(id, locale);
    ImageIO.write(challenge, "jpg", stream);
}

From source file:app.springapp.ImageConverter.java

public void saveImage(String filepath) {

    try {//from  w ww. jav a 2 s .  c o m
        ImageIO.write(new_image, "png", new File(filepath + "C"));
    } catch (IOException e) {

        System.out.println("Could not write image!");
    }

}

From source file:com.formkiq.core.util.Strings.java

/**
 * Converts Base64 String to byte[] image.
 * @param base64String {@link String}/*w w  w.j a  va2  s. c om*/
 * @return {@link Pair}
 * @throws IOException IOException
 */
public static Pair<byte[], String> base64StringToImg(final String base64String) throws IOException {

    int headerLen = BASE64_IMAGE_HEADER.length();

    if (base64String.startsWith(BASE64_IMAGE_HEADER) && base64String.length() > headerLen) {

        int semicolon = base64String.indexOf(";");
        int comma = base64String.indexOf(",", semicolon);

        String formatName = base64String.substring(headerLen, semicolon);
        int pos = formatName.lastIndexOf("/");
        if (pos != -1) {
            formatName = formatName.substring(pos + 1);
        }

        String base64Image = base64String.substring(comma + 1);

        BufferedImage img = ImageIO
                .read(new ByteArrayInputStream(java.util.Base64.getDecoder().decode(base64Image)));

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(img, formatName, baos);
        byte[] bytes = baos.toByteArray();
        return Pair.of(bytes, formatName);
    }

    throw new IllegalArgumentException("Invalid Image base64 String");
}

From source file:ThumbnailTools.java

/**
 * Save the thumbnail to the specified file, with the specified type
 * @param file the file/* w  w  w.j  a v a2s . c o m*/
 * @param imageType the image type
 */
public void saveThumbnail(File file, String imageType) {
    if (thumb != null) {
        BufferedImage bi = new BufferedImage(thumb.getIconWidth(), thumb.getIconHeight(),
                BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.getGraphics();
        g.drawImage(thumb.getImage(), 0, 0, null);
        try {
            ImageIO.write(bi, imageType, file);
        } catch (IOException ioe) {
            throw new RuntimeException("Error occured saving thumbnail");
        }
    } else {
        throw new RuntimeException("Thumbnail have to be created before.");
    }
}

From source file:org.chos.transaction.passport.controller.CaptchaController.java

@RequestMapping(value = "/captcha")
public void captcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
    BufferedImage bi = new BufferedImage(40, 18, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    String c = generate();/* w ww .  j  av a  2  s . c o  m*/
    g.setColor(Color.white);
    g.fillRect(1, 1, 38, 16);

    g.setColor(generateColor());
    g.drawString(c, 4, 13);
    response.setContentType("image/jpeg");
    ServletOutputStream os = response.getOutputStream();
    //      os.write(b)
    //      ImageIO.createImageOutputStream(os);
    ImageIO.write(bi, "jpg", os);
    os.flush();
    os.close();
    request.getSession().setAttribute("captcha", c);
}

From source file:com.googlecode.fightinglayoutbugs.helpers.ImageHelper.java

public static void imageToPngFile(BufferedImage image, File pngFile) {
    try {//from w ww. ja  va 2 s  . co  m
        ImageIO.write(image, "png", pngFile);
    } catch (IOException e) {
        throw new RuntimeException("Failed to write image to file: " + pngFile, e);
    }
}

From source file:com.fusesource.forge.jmstest.persistence.rrd.RrdGraphPostProcessor.java

private void createThumbnail(BufferedImage image, String name, int thumbWidth, int thumbHeight) {

    double thumbRatio = (double) thumbWidth / (double) thumbHeight;
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);
    double imageRatio = (double) imageWidth / (double) imageHeight;

    if (thumbRatio < imageRatio) {
        thumbHeight = (int) (thumbWidth / imageRatio);
    } else {/*from  w  w  w .j  a va2s .  co  m*/
        thumbWidth = (int) (thumbHeight * imageRatio);
    }

    BufferedImage thumbImage = new BufferedImage(thumbWidth,

            thumbHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = thumbImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

    File thumbFile = new File(getWorkDir().getAbsolutePath() + "/" + name + "-thumb.png");

    try {
        ImageIO.write(thumbImage, "PNG", thumbFile);
    } catch (IOException ioe) {
        log().error("Error creating thumbnail for: " + name);
    }
}

From source file:com.ables.pix.utility.PictureOps.java

private void rotatePicture(Picture pic, double rotation) {
    ResourceLoader loader = new FileSystemResourceLoader();
    Resource resource = loader.getResource(repositoryRoot + pic.getLocation());
    BufferedImage image;//from  www.  j  a v a2  s  .  c o  m
    try {
        image = ImageIO.read(resource.getFile());
        BufferedImage result = tilt(image, rotation);
        String fileName = pic.getFileName();
        ImageIO.write(result, fileName.substring(fileName.indexOf('.' + 1)),
                new File(resource.getFile().toURI()));
    }

    catch (IOException io) {
        throw new RuntimeException("Failed to rotate image", io);
    }
}

From source file:com.alta189.deskbin.services.image.ImgurService.java

@Override
public String upload(BufferedImage image) throws ImageServiceException {
    try {/*www.  j  ava2 s .  c om*/
        File file = File.createTempFile("screenshot-", ".png");
        ImageIO.write(image, "png", file);
        return upload(file);
    } catch (ImageServiceException e) {
        throw e;
    } catch (IOException e) {
        throw new ImageServiceException(e);
    } catch (Exception e) {
        throw new ImageServiceException(e);
    }
}

From source file:com.iiitd.qre.CreateZSignedQR.java

public static void writeToStream(ByteMatrix matrix, String format, OutputStream stream) throws IOException {
    BufferedImage image = toBufferedImage(matrix);
    /*System.out.println("m: " + matrix + ", f: " + format + ", s: " + stream
    + ", i: " + image);*//*www.j a va  2  s  .c  o m*/
    ImageIO.write(image, format, stream);
}