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.hygenics.imaging.ImageCleanup.java

/**
 * Set the image type to jpg/*from  w  w  w  .j  a  v  a2 s  .  c  om*/
 */
public void setImageType(String inurl, byte[] ibytes) {
    String imgType = null;
    String urltst = inurl;
    // get the image type from the url

    if (inurl != null) {
        urltst = inurl.toLowerCase();
    }

    // get the image type from the url which should contain the MIME type
    if (!urltst.toLowerCase().contains("jpg") | !urltst.toLowerCase().contains("jpeg")) {
        ByteArrayInputStream bis = new ByteArrayInputStream(ibytes);

        if (bis != null) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            // convert to jpeg for compression
            try {
                // use apache to read to a buffered image with certain
                // metadata and then convert to a jpg
                BufferedImage image = Imaging.getBufferedImage(bis);
                ImageIO.write(image, "jpg", bos);
                ibytes = bos.toByteArray();

            } catch (ImageReadException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    imgType = "jpg";
}

From source file:net.easysmarthouse.cameras.ws.WebcamWebSocketHandler.java

@Override
public void newImage(Webcam webcam, BufferedImage image) {
    log.log(Level.INFO, "New image from camera [{0}]", new Object[] { webcam.getDevice().getName() });

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from w  ww . ja  v  a 2 s.co  m*/
        ImageIO.write(image, "JPG", baos);
    } catch (IOException e) {
        log.log(Level.SEVERE, "Error while getting the image", e);
    }

    String base64 = null;
    try {
        base64 = new String(Base64.encodeBase64(baos.toByteArray()), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        log.log(Level.SEVERE, "Error while encoding", e);
    }

    Map<String, Object> message = new HashMap<String, Object>();
    message.put("type", "image");
    message.put("webcam", webcam.getName());
    message.put("image", base64);

    send(message);
}

From source file:talkeeg.server.BarcodeProvider.java

@Override
public void handle(HttpRequest data, HttpAsyncExchange httpExchange, HttpContext context)
        throws HttpException, IOException {
    final BinaryData barcodeData = this.helloProvider.get().helloAsBinaryData();
    final BitMatrix matrix = this.barcodeServiceProvider.get().encode(barcodeData);
    final BufferedImage image = BarcodeUtilsSE.toBufferedImage(matrix);
    ByteArrayOutputStream tmp = new ByteArrayOutputStream();
    ImageIO.write(image, "png", tmp);
    final HttpResponse response = httpExchange.getResponse();
    response.setHeader("Content-Type", "image/png");
    response.setEntity(new ByteArrayEntity(tmp.toByteArray()));
    response.setStatusCode(HttpStatus.SC_OK);
    httpExchange.submitResponse(new BasicAsyncResponseProducer(response));
}

From source file:org.shredzone.cilla.service.resource.ImageProcessorImpl.java

@Override
@Cacheable(value = "processedImages", key = "#id + '-' + #process")
public ImageProcessorResult process(DataSource ds, long id, ImageProcessing process) throws IOException {
    ImageType type = process.getType();/*  w ww  . j  ava2 s  . c  o  m*/
    int width = process.getWidth();
    int height = process.getHeight();

    if (type == null) {
        if (width > 0 && width <= 256 && height > 0 && height <= 256) {
            type = ImageType.PNG;
        } else {
            type = ImageType.JPEG;
        }
    }

    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        Thumbnails.Builder<? extends InputStream> builder = Thumbnails.of(ds.getInputStream());
        if (width > 0 && height > 0) {
            builder.size(width, height);
        } else if (width > 0) {
            builder.width(width);
        } else if (height > 0) {
            builder.height(height);
        }

        BufferedImage scaled = builder.asBufferedImage();

        if (type == ImageType.JPEG_LOW) {
            jpegQualityWriter(scaled, new MemoryCacheImageOutputStream(out), type.getCompression());
        } else {
            ImageIO.write(scaled, type.getFormatName(), out);
        }

        ImageProcessorResult result = new ImageProcessorResult();
        result.setData(out.toByteArray());
        result.setContentType(type.getContentType());
        return result;
    }
}

From source file:de.adorsys.jwebsane.controller.WebsaneController.java

public void preview() {
    try {/*from  w  w  w.jav  a  2 s  . c  o  m*/
        openDevice();
        setPreviewDpi();

        // set max area
        setPreviewScanArea();

        // save preview image
        File outputfile = getFilePath(ScanOptionsBean.PREVIEW_FILE);
        BufferedImage image = scanOptionsBean.getDevice().acquireImage();
        ImageIO.write(image, "png", outputfile);
        scanOptionsBean.setImgFile(ScanOptionsBean.PREVIEW_FILE);
        log.debug("Preview image written in: " + outputfile.getAbsolutePath());
    } catch (Exception e) {
        log.error(e);
    } finally {
        closeDevice();
    }
}

From source file:es.eucm.eadventure.editor.plugin.vignette.VignetteCharacterPreview.java

private void updateBytes() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from   w ww.  j a va2  s  .  c o  m*/
        ImageIO.write(image, "png", baos);
        baos.close();
        imageBytes = baos.toByteArray();
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digest = md.digest(imageBytes);
        imageName = Hex.encodeHexString(digest) + ".png";
        System.err.println("Wrote " + imageBytes + " bytes to " + imageName);
    } catch (Exception e) {
        System.err.println("Error encoding & md5ing:");
        e.printStackTrace();
    }
}

From source file:info.magnolia.ui.mediaeditor.action.MediaEditorAction.java

protected InputStream createStreamSource(final BufferedImage img, final String formatName) {
    ByteArrayOutputStream out2 = null;
    try {//from  ww  w  .  jav  a2s  .  c o m
        if (img == null) {
            return null;
        }
        out2 = new ByteArrayOutputStream();
        ImageIO.write(img, formatName, out2);
        return new ByteArrayInputStream(out2.toByteArray());
    } catch (IOException e) {
        log.error("Error occurred while creating image stream: " + e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(out2);
    }
    return null;
}

From source file:ml.hsv.java

public static void HSV2File(hsv hsvImage[][], int width, int height) throws IOException //store img output as hsv2file.png
{
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    WritableRaster raster = image.getRaster();
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int RGB = Color.HSBtoRGB(hsvImage[i][j].h, hsvImage[i][j].s, hsvImage[i][j].v);
            Color c = new Color(RGB);
            int temp[] = { c.getRed(), c.getGreen(), c.getBlue() };
            raster.setPixel(j, i, temp);
        }//  w ww  . j a va  2  s  .  c  om
    }
    ImageIO.write(image, "PNG",
            new File("/home/shinchan/FinalProject/PaperImplementation/Eclipse/ML/output/hsv2file.png"));
}

From source file:Result3.java

public void createImage() {
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("Documents"));
    int retrival = chooser.showSaveDialog(null);
    if (retrival == JFileChooser.APPROVE_OPTION) {
        try {/*w  ww.j  a  v a2s.  c om*/
            ImageIO.write(bImage1, "png", new File(chooser.getSelectedFile() + ".png"));
        } catch (IOException ex) {
            Logger.getLogger(Result1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

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

@Override
public BufferedImage loadImage(ImagenMetadata data) {
    try {/*from www.  ja v a 2  s .  co m*/
        File file = new File("data/" + data.getRawBlobKey());
        if (file.exists()) {
            return ImageIO.read(file);
        }
        BufferedImage img = factory.getTemplate().getForObject(
                factory.getRootUlr() + "/upload/imagenes/" + data.getRawBlobKey(), BufferedImage.class);
        ImageIO.write(img, "jpg", file);
        return img;
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}