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:edworld.pdfreader4humans.PDFReaderTest.java

@Test
public void createPageImageWithStructure() throws IOException {
    File outputFile = new File("target/outputWithStructure.png");
    ImageIO.write(reader.createPageImage(1, 3, Color.WHITE, Color.BLACK, true), "png", outputFile);
    assertImagesAreSimilar(getClass().getResourceAsStream("/testcase1/outputWithStructure.png"),
            ImageIO.read(outputFile));
}

From source file:org.dineth.shooter.app.view.ImageController.java

@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody/*from  ww  w  .  ja v a 2s .c o  m*/
public String handleFormUpload(@RequestParam("file") MultipartFile file) {

    File destination = null;
    if (!file.isEmpty()) {

        try {
            BufferedImage src = ImageIO.read(new ByteArrayInputStream(file.getBytes()));

            destination = new File(homefilefolder + file.getOriginalFilename());
            destination.mkdirs();

            ImageIO.write(src, FilenameUtils.getExtension(file.getOriginalFilename()), destination);

            //Save the id you have used to create the file name in the DB. You can retrieve the image in future with the ID.
        } catch (IOException ex) {
            Logger.getLogger(ImageController.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        return "bad thing";
    }
    return destination.getName();
}

From source file:filters.collage.PaddingFilter.java

@Override
protected void doFilter(BufferedImage image, OutputStream output, FilterOptions options, String format)
        throws IOException {

    Integer size = options.get(FilterOption.SIZE, Integer.class);
    Color color = options.get(FilterOption.COLOR, Color.class);

    BufferedImage outputImage = Scalr.pad(image, size, color);
    ImageIO.write(outputImage, format, output);
}

From source file:com.portal.security.controller.JCaptchaController.java

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String captchaId = request.getSession().getId();
    BufferedImage challenge = captchaService.getImageChallengeForID(captchaId, request.getLocale());

    response.setHeader("Cache-Control", "no-store");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/jpeg");

    ImageIO.write(challenge, "jpeg", response.getOutputStream());

    /*/* w  w w  . j  ava2 s  . co  m*/
     * byte[] captchaChallengeAsJpeg = null; ByteArrayOutputStream jpegOutputStream = new
     * ByteArrayOutputStream();
     * 
     * JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);
     * jpegEncoder.encode(challenge);
     * 
     * captchaChallengeAsJpeg = jpegOutputStream.toByteArray(); ServletOutputStream
     * responseOutputStream = response.getOutputStream();
     * 
     * responseOutputStream.write(captchaChallengeAsJpeg); responseOutputStream.flush();
     * responseOutputStream.close();
     */
    return null;
}

From source file:com.openkm.applet.Util.java

/**
 * Upload scanned document to OpenKM/*w  w w  . j  av  a2  s. c om*/
 * 
 */
public static String createDocument(String token, String path, String fileName, String fileType, String url,
        List<BufferedImage> images) throws MalformedURLException, IOException {
    log.info("createDocument(" + token + ", " + path + ", " + fileName + ", " + fileType + ", " + url + ", "
            + images + ")");
    File tmpDir = createTempDir();
    File tmpFile = new File(tmpDir, fileName + "." + fileType);
    ImageOutputStream ios = ImageIO.createImageOutputStream(tmpFile);
    String response = "";

    try {
        if ("pdf".equals(fileType)) {
            ImageUtils.writePdf(images, ios);
        } else if ("tif".equals(fileType)) {
            ImageUtils.writeTiff(images, ios);
        } else {
            if (!ImageIO.write(images.get(0), fileType, ios)) {
                throw new IOException("Not appropiated writer found!");
            }
        }

        ios.flush();
        ios.close();

        if (token != null) {
            // Send image
            HttpClient client = new DefaultHttpClient();
            MultipartEntity form = new MultipartEntity();
            form.addPart("file", new FileBody(tmpFile));
            form.addPart("path", new StringBody(path, Charset.forName("UTF-8")));
            form.addPart("action", new StringBody("0")); // FancyFileUpload.ACTION_INSERT
            HttpPost post = new HttpPost(url + "/frontend/FileUpload;jsessionid=" + token);
            post.setHeader("Cookie", "jsessionid=" + token);
            post.setEntity(form);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            response = client.execute(post, responseHandler);
        } else {
            // Store in disk
            String home = System.getProperty("user.home");
            File dst = new File(home, tmpFile.getName());
            copyFile(tmpFile, dst);
            response = "Image copied to " + dst.getPath();
        }
    } finally {
        FileUtils.deleteQuietly(tmpDir);
    }

    log.info("createDocument: " + response);
    return response;
}

From source file:com.shazam.fork.ShazamImageSaver.java

@Override
public void process(BufferedImage image, Map<String, String> request) {
    try {/*from  w w w.  j  av a 2 s  .  c om*/
        File directoryToSaveIn = screenshotDirectory;
        String name = request.containsKey("name") ? request.get("name")
                : String.format("%04d", screenshotCount++);
        if (request.containsKey("dirName")) {
            directoryToSaveIn = new File(screenshotDirectory, request.get("dirName"));
            forceMkdir(directoryToSaveIn);

        }
        File screenshotFile = new File(directoryToSaveIn,
                (new StringBuilder()).append(name).append(".png").toString());

        ImageIO.write(image, "png", screenshotFile);
    } catch (IOException e) {
    }
}

From source file:com.qspin.qtaste.testapi.impl.generic.UtilityImpl.java

public void createScreenshot(String fileName) throws QTasteException {
    try {//  w  w w.  java  2s.  c  o  m
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle screenRectangle = new Rectangle(screenSize);
        Robot robot = new Robot();
        BufferedImage image = robot.createScreenCapture(screenRectangle);
        ImageIO.write(image, "png", new File(fileName));
    } catch (Exception e) {
        throw new QTasteException("Error in createScreenshot: " + e.getMessage());
    }
}

From source file:dk.netdesign.common.osgi.config.osgi.OCD.java

@Override
public InputStream getIcon(int iconDimension) throws IOException {
    BufferedImage img = null;//w  ww  .  ja  va 2  s .  co m
    img = ImageIO.read(iconFile);

    BufferedImage resizedImage = scaleImage(img, iconDimension, iconDimension, Color.DARK_GRAY);

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(resizedImage, "jpg", os);
    return new ByteArrayInputStream(os.toByteArray());

}

From source file:com.apitrary.orm.codec.image.ImageJPGCodec.java

/** {@inheritDoc} */
@Override/*from  w  ww  .  j  av  a 2  s . c o  m*/
public String encode(Image image) {
    if (image == null) {
        return null;
    }
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {
        ImageIO.write((BufferedImage) image, FORMAT, byteArrayOutputStream);
        byte[] imageData = byteArrayOutputStream.toByteArray();
        return Base64.encodeBase64String(imageData);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:ch.fork.AdHocRailway.ui.utils.ImageTools.java

/**
 * Encode image to string/*from   w  w  w  .  j  a  va  2  s .  c  o  m*/
 *
 * @param image The image to encode
 * @param type  jpeg, bmp, ...
 * @return encoded string
 */
public static String encodeToString(BufferedImage image, String type) {
    String imageString = null;
    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;
}