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:org.jfree.graphics2d.demo.BufferedImageDemo.java

/**
 * Starting point for the demo./*  w  w w  . ja va2s .  c o m*/
 * 
 * @param args  ignored.
 * 
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    BufferedImage image = new BufferedImage(600, 400, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    ImageIcon icon = new ImageIcon(BufferedImageDemo.class.getResource("jfree_chart_1.jpg"));
    g2.rotate(Math.PI / 12);
    g2.setStroke(new BasicStroke(2.0f));
    g2.setPaint(Color.WHITE);
    g2.fill(new Rectangle(0, 0, 600, 400));
    g2.setPaint(Color.RED);
    g2.draw(new Rectangle(0, 0, 600, 400));
    g2.drawImage(icon.getImage(), 10, 20, null);
    ImageIO.write(image, "png", new File("image-test.png"));
}

From source file:jtrace.scenes.GlassSphere.java

public static void main(String[] args) throws IOException {

    Camera camera = new Camera(new Vector3D(0, 0, 2.0), new Vector3D(0, 0, 0), Vector3D.PLUS_J, 1.0,
            800.0 / 600.0);/*from ww  w.java2 s . com*/

    Scene scene = new Scene();
    scene.setCamera(camera);

    scene.addLightSource(new LightSource(new Vector3D(-1, 3, 1), 4));

    FlatTexture sphereTexture = (new FlatTexture(new SolidPigment(new Colour(1, 1, 1))))
            .addFinish(new TransparentFinish(1.1));

    Sphere sphere = new Sphere(new Vector3D(0, 0, 0), 0.4);
    sphere.addTexture(sphereTexture);
    scene.addObject(sphere);

    FlatTexture floorTexture = (new FlatTexture(
            new CheckeredPigment(new Colour(.5, .5, .5), new Colour(1, 1, 1), 1)))
                    .addFinish(new DiffuseFinish(1.0)).addFinish(new AmbientFinish(0.05));

    Plane plane = new Plane(new Vector3D(0, -0.4, 0), Vector3D.PLUS_J, Vector3D.PLUS_K);
    plane.addTexture(floorTexture);
    scene.addObject(plane);

    BufferedImage image = scene.render(800, 600, 10);
    ImageIO.write(image, "PNG", new File("out.png"));
}

From source file:jtrace.scenes.JustAPlane.java

public static void main(String[] args) throws IOException {
    Scene scene = new Scene();
    //scene.setBackground(Colour.cyan.mix(0.8, Colour.white));

    Camera camera = new Camera(new Vector3D(0, -3, 1), new Vector3D(0, 0, 0), Vector3D.PLUS_K, 1.0,
            1440.0 / 900.0);//from  w w w. j  a v a  2 s .c  om
    scene.setCamera(camera);

    LightSource light = new LightSource(new Vector3D(-3, -3, 3), 4);
    scene.addLightSource(light);

    FlatTexture floorTexture = new FlatTexture(new ImagePigment(new File("wood.jpg"), 1.0));
    floorTexture.addFinish(new DiffuseFinish(1.0));
    floorTexture.addFinish(new AmbientFinish(0.1));

    Plane plane = new Plane();
    plane.addTexture(floorTexture);
    plane.addTransformation(new Scale(2, 1, 1));
    //        plane.addTransformation(new Rotation(Vector3D.PLUS_K, Math.PI/16));
    plane.addTransformation(new Rotation(Vector3D.PLUS_J, Math.PI / 16));
    //        plane.addTransformation(new Translation(0,-0.5,0));

    scene.addObject(plane);

    BufferedImage image = scene.renderWireFrame(1440, 900, 10);

    ImageIO.write(image, "PNG", new File("out.png"));
}

From source file:jtrace.scenes.MirroredSphere.java

public static void main(String[] args) throws IOException {

    Camera camera = new Camera(new Vector3D(-1, 1, 3), new Vector3D(0, 0, 0), Vector3D.PLUS_J, 1.0, 1.6);

    Scene scene = new Scene();
    scene.setCamera(camera);/*from  ww  w.  j a  v a 2s.  c o m*/

    scene.addLightSource(new LightSource(new Vector3D(-3, 3, 3), 4));

    FlatTexture sphereTexture = (new FlatTexture(new SolidPigment(new Colour(0, 1, 0))))
            .addFinish(new AmbientFinish(0.1)).addFinish(new MirrorFinish(0.2))
            .addFinish(new DiffuseFinish(0.8)).addFinish(new SpecularFinish(1.0, 100));

    Sphere sphere = new Sphere(new Vector3D(0, 0, 0), 0.4);
    sphere.addTexture(sphereTexture);
    scene.addObject(sphere);

    FlatTexture floorTexture = (new FlatTexture(
            new CheckeredPigment(new Colour(.5, .5, .5), new Colour(1, 1, 1), 1)))
                    .addFinish(new DiffuseFinish(1.0)).addFinish(new AmbientFinish(0.1));

    Plane plane = new Plane(new Vector3D(0, -0.4, 0), Vector3D.PLUS_J, Vector3D.PLUS_K);
    plane.addTexture(floorTexture);
    scene.addObject(plane);

    BufferedImage image = scene.render(1440, 900, 10);
    ImageIO.write(image, "PNG", new File("out.png"));
}

From source file:jtrace.scenes.OneCube.java

public static void main(String[] args) throws IOException {

    Camera camera = new Camera(new Vector3D(0, -2, 1), new Vector3D(0, 0, 0), Vector3D.PLUS_K, 1.0,
            1440.0 / 900.0);/* www. j  a  v  a  2s  .  c om*/

    Scene scene = new Scene();
    scene.setCamera(camera);

    scene.addLightSource(new LightSource(new Vector3D(-3, 3, 3), 4));
    scene.addLightSource(new LightSource(new Vector3D(3, -3, 1), 4));

    FlatTexture cubeTexture = (new FlatTexture(new SolidPigment(new Colour(0, 0, 1))));
    cubeTexture.addFinish(new DiffuseFinish(1.0));
    cubeTexture.addFinish(new AmbientFinish(0.1));
    //cubeTexture.addFinish(new MirrorFinish(0.2));

    Cube cube = new Cube();
    //cube.addTransformation(new Scale(0.5));
    cube.addTexture(cubeTexture);
    scene.addObject(cube);

    FlatTexture floorTexture = new FlatTexture(new ImagePigment(new File("wood.jpg"), 1.0));
    floorTexture.addFinish(new DiffuseFinish(1.0));

    Plane plane = new Plane();
    plane.addTransformation(new Translation(0, 0, -0.5));
    plane.addTexture(floorTexture);
    scene.addObject(plane);

    BufferedImage image = scene.render(1440, 900, 10);

    ImageIO.write(image, "PNG", new File("out.png"));
}

From source file:jtrace.scenes.OneCubeAnimate.java

public static void main(String[] args) throws IOException {

    Scene scene = new Scene();

    scene.addLightSource(new LightSource(new Vector3D(-3, 3, -3), 4));
    scene.addLightSource(new LightSource(new Vector3D(6, 6, -3), 4));

    FlatTexture cubeTexture = (new FlatTexture(new SolidPigment(new Colour(0, 0, 1))));
    cubeTexture.addFinish(new DiffuseFinish(1.0));
    cubeTexture.addFinish(new AmbientFinish(0.1));
    cubeTexture.addFinish(new MirrorFinish(0.2));

    Cube cube = new Cube(new Vector3D(0, 0, 0), 0.5);
    cube.addTexture(cubeTexture);//from   w ww .j a  v a 2  s . co m
    scene.addObject(cube);

    //        FlatTexture floorTexture = new FlatTexture(new CheckeredPigment(
    //                new Colour(.5,.5,.5), new Colour(1,1,1), 1));
    FlatTexture floorTexture = new FlatTexture(new ImagePigment(new File("wood.jpg"), 1.0));
    floorTexture.addFinish(new DiffuseFinish(1.0));

    Plane plane = new Plane(new Vector3D(0, -0.25, 0), Vector3D.PLUS_J, Vector3D.PLUS_K);
    plane.addTexture(floorTexture);
    scene.addObject(plane);

    Vector3D pointAt = new Vector3D(0, 0, 0);

    int steps = 100;
    for (int i = 0; i < steps; i++) {
        double R = 2.0;
        double x = R * Math.sin(i * 2 * Math.PI / steps);
        double z = -R * Math.cos(i * 2 * Math.PI / steps);
        Camera camera = new Camera(new Vector3D(x, 1, z), pointAt, Vector3D.PLUS_J, 1.0, 800.0 / 600.0);
        scene.setCamera(camera);
        BufferedImage image = scene.render(800, 600, 10);
        ImageIO.write(image, "PNG", new File(String.format("out_%02d.png", i)));
    }
}

From source file:jtrace.scenes.FourSpheres.java

public static void main(String[] args) throws IOException {

    Camera camera = new Camera(new Vector3D(-1, 2, 5), new Vector3D(0, 0, 0), Vector3D.PLUS_J, 1.0, 1.6);

    Scene scene = new Scene();
    scene.setCamera(camera);/*from w ww.  java  2  s  . c o  m*/

    scene.addLightSource(new LightSource(new Vector3D(-3, 3, 3), 4));

    FlatTexture glossRed = (new FlatTexture(new SolidPigment(new Colour(1, 0, 0))))
            .addFinish(new DiffuseFinish(1.0)).addFinish(new AmbientFinish(0.1))
            .addFinish(new SpecularFinish(1.0, 100));

    FlatTexture glossGreen = (new FlatTexture(new SolidPigment(new Colour(0, 1, 0))))
            .addFinish(new DiffuseFinish(1.0)).addFinish(new AmbientFinish(0.1))
            .addFinish(new SpecularFinish(1.0, 100));

    FlatTexture glossBlue = (new FlatTexture(new SolidPigment(new Colour(0, 0, 1))))
            .addFinish(new DiffuseFinish(1.0)).addFinish(new AmbientFinish(0.1))
            .addFinish(new SpecularFinish(1.0, 100));

    FlatTexture glossYellow = (new FlatTexture(new SolidPigment(new Colour(1, 1, 0))))
            .addFinish(new DiffuseFinish(1.0)).addFinish(new AmbientFinish(0.1))
            .addFinish(new SpecularFinish(1.0, 100));

    Sphere redSphere = new Sphere(new Vector3D(-1, 0, 0), 0.4);
    redSphere.addTexture(glossRed);
    scene.addObject(redSphere);

    Sphere greenSphere = new Sphere(new Vector3D(0, 0, 1), 0.4);
    greenSphere.addTexture(glossGreen);
    scene.addObject(greenSphere);

    Sphere blueSphere = new Sphere(new Vector3D(0, 0, -1), 0.4);
    blueSphere.addTexture(glossBlue);
    scene.addObject(blueSphere);

    Sphere yellowSphere = new Sphere(new Vector3D(1, 0, 0), 0.4);
    yellowSphere.addTexture(glossYellow);
    scene.addObject(yellowSphere);

    FlatTexture floorTexture = new FlatTexture(
            new CheckeredPigment(new Colour(.5, .5, .5), new Colour(1, 1, 1), 1));
    floorTexture.addFinish(new DiffuseFinish(1.0));
    floorTexture.addFinish(new AmbientFinish(0.05));

    Plane plane = new Plane(new Vector3D(0, -0.4, 0), Vector3D.PLUS_J, Vector3D.PLUS_K);
    plane.addTexture(floorTexture);
    scene.addObject(plane);

    BufferedImage image = scene.render(1440, 900, 10);
    ImageIO.write(image, "PNG", new File("out.png"));
}

From source file:ShowImage.java

public static void main(String args[]) throws IOException {
    List<File> files = ImageIOUtils.getFiles(args, new String[] { "ntf", "nsf" });

    for (Iterator iter = files.iterator(); iter.hasNext();) {
        try {/*  ww  w .  j a  v a 2 s.  com*/
            File file = (File) iter.next();
            log.debug("Reading: " + file.getAbsolutePath());
            NITFReader imageReader = (NITFReader) ImageIOUtils.getImageReader("nitf", file);

            for (int i = 0; i < imageReader.getRecord().getImages().length; ++i) {
                log.debug(file.getName() + "[" + i + "]");

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                ImageSubheader subheader = imageReader.getRecord().getImages()[i].getSubheader();
                subheader.print(new PrintStream(stream));
                log.debug(stream.toString());

                try {
                    int numBands = subheader.getBandCount();
                    String irep = subheader.getImageRepresentation().getStringData().trim();
                    int bitsPerPixel = subheader.getNumBitsPerPixel().getIntData();
                    int nBytes = (bitsPerPixel - 1) / 8 + 1;

                    if (irep.equals("RGB") && numBands == 3) {
                        BufferedImage image = imageReader.read(i);
                        ImageIOUtils.showImage(image, file.getName() + "[" + i + "]", true);
                    } else {
                        // read each band, separately
                        for (int j = 0; j < numBands; ++j) {
                            if (nBytes == 1 || nBytes == 2 || nBytes == 4 || nBytes == 8) {
                                ImageReadParam readParam = imageReader.getDefaultReadParam();
                                readParam.setSourceBands(new int[] { j });
                                BufferedImage image = imageReader.read(i, readParam);
                                ImageIOUtils.showImage(image, file.getName() + "[" + i + "][" + j + "]", true);

                                ImageIO.write(image, "jpg",
                                        new FileOutputStream("image" + i + "_" + j + ".jpg"));

                                // downsample
                                // readParam.setSourceSubsampling(2, 2, 0,
                                // 0);
                                // BufferedImage smallerImage = imageReader
                                // .read(i, readParam);
                                //
                                // ImageIOUtils.showImage(smallerImage,
                                // "DOWNSAMPLED: " + file.getName());

                            }
                        }
                    }
                } catch (Exception e) {
                    System.out.println(ExceptionUtils.getStackTrace(e));
                    log.error(ExceptionUtils.getStackTrace(e));
                }
            }
        } catch (Exception e) {
            log.debug(ExceptionUtils.getStackTrace(e));
        }
    }
}

From source file:com.tonygalati.sprites.SpriteGenerator.java

public static void main(String[] args) throws IOException {

    //        if (args.length != 3)
    //        {//from  ww  w.  j  ava 2  s .  c o m
    //           System.out.print("Usage: com.tonygalati.sprites.SpriteGenerator {path to images} {margin between images in px} {output file}\n");
    //           System.out.print("Note: The max height should only be around 32,767px due to Microsoft GDI using a 16bit signed integer to store dimensions\n");
    //           System.out.print("going beyond this dimension is possible with this tool but the generated sprite image will not work correctly with\n");
    //           System.out.print("most browsers.\n\n");
    //           return;
    //        }

    //        Integer margin = Integer.parseInt(args[1]);
    Integer margin = Integer.parseInt("1");
    String spriteFile = "icons-" + RandomStringUtils.randomAlphanumeric(10) + ".png";
    SpriteCSSGenerator cssGenerator = new SpriteCSSGenerator();

    ClassLoader classLoader = SpriteGenerator.class.getClassLoader();
    URL folderPath = classLoader.getResource(NAIC_SMALL_ICON);
    if (folderPath != null) {
        File imageFolder = new File(folderPath.getPath());

        // Read images
        ArrayList<BufferedImage> imageList = new ArrayList<BufferedImage>();
        Integer yCoordinate = null;

        for (File f : imageFolder.listFiles()) {
            if (f.isFile()) {
                String fileName = f.getName();
                String ext = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());

                if (ext.equals("png")) {
                    System.out.println("adding file " + fileName);
                    BufferedImage image = ImageIO.read(f);
                    imageList.add(image);

                    if (yCoordinate == null) {
                        yCoordinate = 0;
                    } else {
                        yCoordinate += (image.getHeight() + margin);
                    }

                    // add to cssGenerator
                    cssGenerator.addSpriteCSS(fileName.substring(0, fileName.indexOf(".")), 0, yCoordinate);
                }
            }
        }

        // Find max width and total height
        int maxWidth = 0;
        int totalHeight = 0;

        for (BufferedImage image : imageList) {
            totalHeight += image.getHeight() + margin;

            if (image.getWidth() > maxWidth)
                maxWidth = image.getWidth();
        }

        System.out.format("Number of images: %s, total height: %spx, width: %spx%n", imageList.size(),
                totalHeight, maxWidth);

        // Create the actual sprite
        BufferedImage sprite = new BufferedImage(maxWidth, totalHeight, BufferedImage.TYPE_INT_ARGB);

        int currentY = 0;
        Graphics g = sprite.getGraphics();
        for (BufferedImage image : imageList) {
            g.drawImage(image, 0, currentY, null);
            currentY += image.getHeight() + margin;
        }

        System.out.format("Writing sprite: %s%n", spriteFile);
        ImageIO.write(sprite, "png", new File(spriteFile));
        File cssFile = cssGenerator.getFile(spriteFile);
        System.out.println(cssFile.getAbsolutePath() + " created");
    } else {
        System.err.println("Could not find folder: " + NAIC_SMALL_ICON);

    }

}

From source file:akori.AKORI.java

public static void main(String[] args) throws IOException, InterruptedException {
    System.out.println("esto es AKORI");

    URL = "http://www.mbauchile.cl";
    PATH = "E:\\NetBeansProjects\\AKORI\\";
    NAME = "mbauchile.png";
    // Extrar DOM tree

    Document doc = Jsoup.connect(URL).timeout(0).get();

    // The Firefox driver supports javascript 
    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    System.out.println(driver.manage().window().getSize().toString());
    System.out.println(driver.manage().window().getPosition().toString());
    int xmax = driver.manage().window().getSize().width;
    int ymax = driver.manage().window().getSize().height;

    // Go to the URL page
    driver.get(URL);//from  w w w  .  j  ava 2s.co m

    File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(screen, new File(PATH + NAME));

    BufferedImage img = ImageIO.read(new File(PATH + NAME));
    //Graphics2D graph = img.createGraphics();

    BufferedImage img1 = new BufferedImage(xmax, ymax, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graph1 = img.createGraphics();
    double[][] matrix = new double[ymax][xmax];
    BufferedReader in = new BufferedReader(new FileReader("et.txt"));
    String linea;
    double max = 0;
    graph1.drawImage(img, 0, 0, null);
    HashMap<String, Integer> lista = new HashMap<String, Integer>();
    int count = 0;
    for (int i = 0; (linea = in.readLine()) != null && i < 10000; ++i) {
        String[] datos = linea.split(",");
        int x = (int) Double.parseDouble(datos[0]);
        int y = (int) Double.parseDouble(datos[2]);
        long time = Double.valueOf(datos[4]).longValue();
        if (x >= xmax || y >= ymax)
            continue;
        if (time < 691215)
            continue;
        if (time > 705648)
            break;
        if (lista.containsKey(x + "," + y))
            lista.put(x + "," + y, lista.get(x + "," + y) + 1);
        else
            lista.put(x + "," + y, 1);
        ++count;
    }
    System.out.println(count);
    in.close();
    Iterator iter = lista.entrySet().iterator();
    Map.Entry e;
    for (String key : lista.keySet()) {
        Integer i = lista.get(key);
        if (max < i)
            max = i;
    }
    System.out.println(max);
    max = 0;
    while (iter.hasNext()) {
        e = (Map.Entry) iter.next();
        String xy = (String) e.getKey();
        String[] datos = xy.split(",");
        int x = Integer.parseInt(datos[0]);
        int y = Integer.parseInt(datos[1]);
        matrix[y][x] += (int) e.getValue();
        double aux;
        if ((aux = normalMatrix(matrix, y, x, ((int) e.getValue()) * 4)) > max) {
            max = aux;
        }
        //normalMatrix(matrix,x,y,20);
        if (matrix[y][x] > max)
            max = matrix[y][x];
    }
    int A, R, G, B, n;
    for (int i = 0; i < xmax; ++i) {
        for (int j = 0; j < ymax; ++j) {
            if (matrix[j][i] != 0) {
                n = (int) Math.round(matrix[j][i] * 100 / max);
                R = Math.round((255 * n) / 100);
                G = Math.round((255 * (100 - n)) / 100);
                B = 0;
                A = Math.round((255 * n) / 100);
                ;
                if (R > 255)
                    R = 255;
                if (R < 0)
                    R = 0;
                if (G > 255)
                    G = 255;
                if (G < 0)
                    G = 0;
                if (R < 50)
                    A = 0;
                graph1.setColor(new Color(R, G, B, A));
                graph1.fillOval(i, j, 1, 1);
            }
        }
    }
    //graph1.dispose();

    ImageIO.write(img, "png", new File("example.png"));
    System.out.println(max);

    graph1.setColor(Color.RED);
    // Extraer elementos
    Elements e1 = doc.body().getAllElements();
    int i = 1;
    ArrayList<String> tags = new ArrayList<String>();
    for (Element temp : e1) {

        if (tags.indexOf(temp.tagName()) == -1) {
            tags.add(temp.tagName());

            List<WebElement> query = driver.findElements(By.tagName(temp.tagName()));
            for (WebElement temp1 : query) {
                Point po = temp1.getLocation();
                Dimension d = temp1.getSize();
                if (d.width <= 0 || d.height <= 0 || po.x < 0 || po.y < 0)
                    continue;
                System.out.println(i + " " + temp.nodeName());
                System.out.println("  x: " + po.x + " y: " + po.y);
                System.out.println("  width: " + d.width + " height: " + d.height);
                graph1.draw(new Rectangle(po.x, po.y, d.width, d.height));
                ++i;
            }
        }
    }

    graph1.dispose();
    ImageIO.write(img, "png", new File(PATH + NAME));

    driver.quit();

}