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:Main.java

public static String imageDigest(BufferedImage img) {

    // write image to byte stream
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {/*w ww  .j ava  2  s . co  m*/
        ImageIO.write(img, "png", os);
        os.flush();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    byte[] data = os.toByteArray();

    // compute md5 hash
    byte[] hash = null;
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(data);
        hash = md.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }

    // convert to string
    String hexString = "";
    for (int i = 0; i < hash.length; i++) {
        hexString += Integer.toString((hash[i] & 0xff) + 0x100, 16).substring(1);
    }
    return hexString;
}

From source file:Main.java

public static void saveToFile(Image image) {
    File outputFile = new File("C:/JavaFX/");
    BufferedImage bImage = SwingFXUtils.fromFXImage(image, null);
    try {/*from  w w w . j ava  2s .  co  m*/
        ImageIO.write(bImage, "png", outputFile);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static byte[] getCompressedImage(byte[] rgb) {

    BufferedImage image;//from   w w w  .  j  a  v a2s.c o  m
    int width;
    int height;

    try {
        image = ImageIO.read(new ByteArrayInputStream(rgb));
        width = image.getWidth();
        height = image.getHeight();

        for (int i = 0; i < height; i++) {

            for (int j = 0; j < width; j++) {

                Color c = new Color(image.getRGB(j, i));
                int red = (int) (c.getRed() * 0.299);
                int green = (int) (c.getGreen() * 0.587);
                int blue = (int) (c.getBlue() * 0.114);
                Color newColor = new Color(red + green + blue,

                        red + green + blue, red + green + blue);

                image.setRGB(j, i, newColor.getRGB());
            }
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", baos);
        baos.flush();
        byte[] imageInByte = baos.toByteArray();
        baos.close();

        return imageInByte;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.optaplanner.benchmark.impl.statistic.common.GraphSupport.java

public static void writeChartToImageFile(JFreeChart chart, File chartFile) {
    BufferedImage chartImage = chart.createBufferedImage(1024, 768);
    OutputStream out = null;/*from  ww  w  . ja  v a  2  s.c o m*/
    try {
        out = new FileOutputStream(chartFile);
        ImageIO.write(chartImage, "png", out);
    } catch (IOException e) {
        throw new IllegalArgumentException("Problem writing chartFile: " + chartFile, e);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:fr.free.divde.webcam.image.DataImageUrl.java

public static String imageToDataURL(BufferedImage image, String format, String mimeType) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ImageIO.write(image, format, out);
    byte[] imageBytes = out.toByteArray();
    String base64 = Base64.encodeBase64String(imageBytes);
    StringBuilder res = new StringBuilder("data:");
    res.append(mimeType);//from www  .  j  a va 2  s .c o m
    res.append(";base64,");
    res.append(base64);
    return res.toString();
}

From source file:com.buddycloud.mediaserver.business.util.ImageUtils.java

public static File storeImageIntoFile(BufferedImage image, String imageFormat, String pathToStore)
        throws IOException {

    // Store into provided path
    File output = new File(pathToStore);
    ImageIO.write(image, imageFormat, FileUtils.openOutputStream(output));

    return output;
}

From source file:WaterMark.java

public static String execute(String src, String dest, String text, Color color, Font font) throws Exception {
    BufferedImage srcImage = ImageIO.read(new File(src));

    int width = srcImage.getWidth(null);
    int height = srcImage.getHeight(null);
    BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = destImage.getGraphics();

    g.drawImage(srcImage, 0, 0, width, height, null);
    g.setColor(color);/*from w  w  w.j  a  v a 2  s .  c  om*/
    g.setFont(font);
    g.fillRect(0, 0, 50, 50);
    g.drawString(text, width / 5, height - 10);
    g.dispose();

    ImageIO.write(destImage, DEFAULT_FORMAT, new File("dest.jpg"));
    return dest;
}

From source file:ImageUtil.java

public static ByteArrayOutputStream crop(ByteArrayInputStream bais, int width, int height) throws IOException {
    BufferedImage src = ImageIO.read(bais);
    BufferedImage clipping = crop(src, width, height);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(clipping, "JPG", baos);
    return baos;/*from   w  w  w  . j  a  v  a  2  s  . c o m*/
}

From source file:Main.java

public void createThumbnail(File file) throws Exception {
    BufferedImage img = ImageIO.read(file);
    BufferedImage thumb = new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2d = (Graphics2D) thumb.getGraphics();
    g2d.drawImage(img, 0, 0, thumb.getWidth() - 1, thumb.getHeight() - 1, 0, 0, img.getWidth() - 1,
            img.getHeight() - 1, null);//from w w  w  .j a  v a  2s .co m
    g2d.dispose();
    ImageIO.write(thumb, "PNG", new File("thumb.png"));
}

From source file:br.edu.ifpb.utils.PhotoUtils.java

public static byte[] getByteImage(BufferedImage imagem) throws IOException {
    if (imagem == null)
        return null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(imagem, "jpg", baos);
    byte[] bytes = baos.toByteArray();

    return bytes;
}