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.reydentx.core.common.JSONUtils.java

private static String encodeToString(BufferedImage image, String type) {
    String imageString = null;//from  ww w  . j  a  v a 2  s  .c  om
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        ImageIO.write(image, type, bos);
        byte[] imageBytes = bos.toByteArray();
        imageString = Base64.encodeBase64String(imageBytes);
        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return imageString;
}

From source file:com.google.maps.LocalTestServerContext.java

LocalTestServerContext(BufferedImage image) throws IOException {
    this.server = new MockWebServer();
    Buffer buffer = new Buffer();
    ImageIO.write(image, "png", buffer.outputStream());
    MockResponse response = new MockResponse();
    response.setHeader("Content-Type", "image/png");
    response.setBody(buffer);// w ww .j a  va  2s  .  c  o m
    server.enqueue(response);
    server.start();

    this.context = new GeoApiContext.Builder().apiKey("AIzaFakeKey")
            .baseUrlOverride("http://127.0.0.1:" + server.getPort()).build();
}

From source file:org.primefaces.showcase.view.multimedia.GraphicImageView.java

@PostConstruct
public void init() {
    try {/*from  w  w w .  j a v  a2 s .  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.createPieChart("Cities", createDataset(), true, true, false);
        File chartFile = new File("dynamichart");
        ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
        chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:eu.novait.imageresizer.helpers.ImageConverter.java

public void process() throws IOException {
    BufferedImage inImage = ImageIO.read(this.file);
    double ratio = (double) inImage.getWidth() / (double) inImage.getHeight();
    int w = this.width;
    int h = this.height;
    if (inImage.getWidth() >= inImage.getHeight()) {
        w = this.width;
        h = (int) Math.round(w / ratio);
    } else {//ww  w.ja  v  a 2  s  . c  o m
        h = this.height;
        w = (int) Math.round(ratio * h);
    }
    int type = inImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : inImage.getType();
    BufferedImage outImage = new BufferedImage(w, h, type);
    Graphics2D g = outImage.createGraphics();
    g.drawImage(inImage, 0, 0, w, h, null);
    g.dispose();
    String ext = FilenameUtils.getExtension(this.file.getAbsolutePath());
    String t = "jpg";
    switch (ext) {
    case "png":
        t = "png";
        break;
    }
    ImageIO.write(outImage, t, this.outputfile);
}

From source file:javafx1.Testpics.java

public static String encodeToString(BufferedImage image, String type) {
    String imageString = null;//from   www  . j  a v a2s.  c  om
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
        ImageIO.write(image, type, bos);
        byte[] imageBytes = bos.toByteArray();

        BASE64Encoder encoder = new BASE64Encoder();
        imageString = encoder.encode(imageBytes);

        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return imageString;
}

From source file:org.nekorp.workflow.desktop.data.access.rest.ImagenDAOImp.java

@Override
public ImagenMetadata saveImage(BufferedImage image) {
    try {/*www . j  a  va 2  s .  co  m*/
        ImagenMetadata r = factory.getTemplate().getForObject(factory.getRootUlr() + "/upload/url",
                ImagenMetadata.class);
        File file = new File("data/upload.jpg");
        ImageIO.write(image, "jpg", file);
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("myFile", new FileSystemResource(file));
        r = factory.getTemplate().postForObject(r.getUploadUrl(), form, ImagenMetadata.class);
        File cache = new File("data/" + r.getRawBlobKey());
        file.renameTo(cache);
        return r;
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.ningpai.common.util.CaptchaController.java

@RequestMapping("/captcha")
public void writeCaptcha(HttpServletRequest request, HttpServletResponse response) {
    byte[] captchaChallengeAsJpeg = null;
    // the output stream to render the captcha image as jpeg into
    ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
    try {/*w w  w.j ava 2  s  . co m*/
        // get the session id that will identify the generated captcha.
        // the same id must be used to validate the response, the session id is a good candidate!
        String captchaId = request.getSession().getId();
        BufferedImage challenge = captchaService.getImageChallengeForID(captchaId, request.getLocale());
        try {
            ImageIO.write(challenge, CAPTCHA_IMAGE_FORMAT, jpegOutputStream);
        } catch (IOException e) {
        }
    } catch (IllegalArgumentException e) {
        try {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        } catch (IOException e1) {
        }
        return;
    } catch (CaptchaServiceException e) {
        try {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } catch (IOException e1) {
        }
        return;
    }

    captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

    // flush it in the response
    response.setHeader("Cache-Control", "no-store");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/" + CAPTCHA_IMAGE_FORMAT);

    ServletOutputStream responseOutputStream;
    try {
        responseOutputStream = response.getOutputStream();
        responseOutputStream.write(captchaChallengeAsJpeg);
        responseOutputStream.flush();
        responseOutputStream.close();
    } catch (IOException e) {
    }

}

From source file:no.dusken.aranea.web.control.CaptchaController.java

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    String captchaID = ServletRequestUtils.getRequiredStringParameter(request, "captchaID");

    BufferedImage image = imageCaptchaService.getImageChallengeForID(captchaID);

    Map<String, Object> map = new HashMap<String, Object>();

    File tmpFile = File.createTempFile("temp", "jpg");

    // TODO: Is there a better way?
    ImageIO.write(image, "JPG", tmpFile);

    map.put("file", tmpFile);
    map.put("deleteFile", true);
    return new ModelAndView("fileView", map);
}

From source file:app.model.game.CoverUploadModel.java

public void uploadImage(HttpServletRequest req, Game g)
        throws FileUploadBase.SizeLimitExceededException, FileUploadException, SQLException, Exception {
    int width = 500;
    int height = 800;

    //Bild upload
    FileUpload upload = new FileUpload(2000 * 1024, 5000 * 1024,
            "C:/Users/Public/Arcade/Games/" + g.getGameID() + "/assets",
            "C:/Users/Public/Arcade/Games/" + g.getGameID() + "/tmp/");
    File fileStatus;/*from  ww w  .  j av  a 2s .  c  o m*/

    fileStatus = upload.uploadFile(req);
    File file = new File(fileStatus.getAbsolutePath());

    BufferedImage img = ImageIO.read(file);
    if (img.getWidth() != width || img.getHeight() != height) {
        img = Scalr.resize(img, Scalr.Method.SPEED, Scalr.Mode.FIT_EXACT, 500, 800);
    }
    File destFile = new File(fileStatus.getParent() + "/" + g.getGameID() + ".jpg");
    ImageIO.write(img, "jpg", destFile);
    g.updateState("coverupload", "complete");
    String state = g.stateToJSON();

    try (SQLHelper sql = new SQLHelper()) {
        sql.execNonQuery("UPDATE `games` SET editState='" + state + "' WHERE ID = " + g.getGameID());
    }

    fileStatus.delete();
}

From source file:org.primefaces.examples.view.DynamicImageController.java

public DynamicImageController() {
    try {//from  ww  w  .j a v  a 2 s .c om
        //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.createPieChart("Turkish Cities", createDataset(), true, true,
                false);
        File chartFile = new File("dynamichart");
        ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
        chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");

        //Barcode
        File barcodeFile = new File("dynamicbarcode");
        BarcodeImageHandler.saveJPEG(BarcodeFactory.createCode128("PRIMEFACES"), barcodeFile);
        barcode = new DefaultStreamedContent(new FileInputStream(barcodeFile), "image/jpeg");
    } catch (Exception e) {
        e.printStackTrace();
    }
}