Search in sources :

Example 1 with FileImageOutputStream

use of javax.imageio.stream.FileImageOutputStream in project screenbird by adamhub.

the class PreviewPlayerTest method generateTestImageFile.

/**
     * Generates screenshot for JUnit testing
     * @return 
     *      File pointer to screen shot
     */
private File generateTestImageFile(Rectangle captureArea) {
    Robot awtRobot;
    String currentCaptureDir = Settings.SCREEN_CAPTURE_DIR;
    if (captureArea == null) {
        //Get full screen if no defined area of screen capture is defined
        captureArea = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    }
    try {
        BufferedImage bufferedImage = generateTestImage(captureArea);
        Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = (ImageWriter) iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(1.0F);
        File imageFile = new File(currentCaptureDir, "test.jpeg");
        FileImageOutputStream stream = new FileImageOutputStream(imageFile);
        //Set up file access
        writer.setOutput(stream);
        //Create image
        IIOImage image = new IIOImage(bufferedImage, null, null);
        //write image
        writer.write(null, image, iwp);
        //Close image stream
        stream.close();
        return imageFile;
    } catch (IOException e) {
        System.err.println(e);
    }
    return null;
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) Rectangle(java.awt.Rectangle) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) Robot(java.awt.Robot) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 2 with FileImageOutputStream

use of javax.imageio.stream.FileImageOutputStream in project screenbird by adamhub.

the class VideoCacheTest method generateTestImage.

/**
     * Generates screenshot for JUnit testing
     * @return 
     *      File pointer to screen shot
     */
private File generateTestImage() {
    Robot awtRobot;
    String currentCaptureDir = Settings.SCREEN_CAPTURE_DIR;
    try {
        awtRobot = new Robot();
        BufferedImage bufferedImage = awtRobot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = (ImageWriter) iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(1.0F);
        File imageFile = new File(currentCaptureDir, "test.jpeg");
        FileImageOutputStream stream = new FileImageOutputStream(imageFile);
        //Set up file access
        writer.setOutput(stream);
        //Create image
        IIOImage image = new IIOImage(bufferedImage, null, null);
        //write image
        writer.write(null, image, iwp);
        //Close image stream
        stream.close();
        return imageFile;
    } catch (AWTException e) {
        System.err.println(e);
    } catch (IOException e) {
        System.err.println(e);
    }
    return null;
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) Rectangle(java.awt.Rectangle) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) Robot(java.awt.Robot) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage) AWTException(java.awt.AWTException)

Example 3 with FileImageOutputStream

use of javax.imageio.stream.FileImageOutputStream in project screenbird by adamhub.

the class ImageUtilTest method generateTestImageFile.

/**
     * Generates screenshot for JUnit testing
     * @return 
     *      File pointer to screen shot
     */
private File generateTestImageFile(Rectangle captureArea) {
    if (captureArea == null) {
        //Get full screen if no defined area of screen capture is defined
        captureArea = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    }
    try {
        BufferedImage bufferedImage = generateTestImage(captureArea);
        Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = (ImageWriter) iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(1.0F);
        File imageFile = generateTempFile("jpeg");
        FileImageOutputStream stream = new FileImageOutputStream(imageFile);
        //Set up file access
        writer.setOutput(stream);
        //Create image
        IIOImage image = new IIOImage(bufferedImage, null, null);
        //write image
        writer.write(null, image, iwp);
        //Close image stream
        stream.close();
        return imageFile;
    } catch (IOException e) {
        System.err.println(e);
    }
    return null;
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) Rectangle(java.awt.Rectangle) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 4 with FileImageOutputStream

use of javax.imageio.stream.FileImageOutputStream in project Denizen-For-Bukkit by DenizenScript.

the class DenizenMapManager method downloadImage.

private static String downloadImage(URL url) {
    try {
        if (!imageDownloads.exists()) {
            imageDownloads.mkdirs();
        }
        String urlString = CoreUtilities.toLowerCase(url.toString());
        if (downloadedByUrl.containsKey(urlString)) {
            File image = new File(imageDownloads, downloadedByUrl.get(urlString));
            if (image.exists()) {
                return image.getPath();
            }
        }
        URLConnection connection = url.openConnection();
        BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
        int lastDot = urlString.lastIndexOf('.');
        String fileName = String.format("%0" + (6 - String.valueOf(downloadCount).length()) + "d", downloadCount) + (lastDot > 0 ? urlString.substring(lastDot) : "");
        File output = new File(imageDownloads, fileName);
        FileImageOutputStream out = new FileImageOutputStream(output);
        int i;
        while ((i = in.read()) != -1) {
            out.write(i);
        }
        out.flush();
        out.close();
        in.close();
        downloadedByUrl.put(urlString, fileName);
        downloadCount++;
        return output.getPath();
    } catch (IOException e) {
        dB.echoError(e);
    }
    return null;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) IOException(java.io.IOException) File(java.io.File) URLConnection(java.net.URLConnection)

Example 5 with FileImageOutputStream

use of javax.imageio.stream.FileImageOutputStream in project OsmAnd-tools by osmandapp.

the class OsmAndImageRendering method main.

public static void main(String[] args) throws IOException, XmlPullParserException, SAXException, ParserConfigurationException, TransformerException {
    String nativeLib = null;
    String eyepiece = null;
    String dirWithObf = null;
    String websiteUrl = "";
    String gpxFile = null;
    String outputFiles = null;
    String stylesPath = null;
    args = setupDefaultAttrs(args);
    for (String a : args) {
        if (a.startsWith("-native=")) {
            nativeLib = a.substring("-native=".length());
        } else if (a.startsWith("-websiteUrl=")) {
            // https://jenkins.osmand.net/job/OsmAndTestRendering/ws/rendering-tests/
            websiteUrl = a.substring("-websiteUrl=".length());
        } else if (a.startsWith("-obfFiles=")) {
            dirWithObf = a.substring("-obfFiles=".length());
        } else if (a.startsWith("-gpxFile=")) {
            gpxFile = a.substring("-gpxFile=".length());
        } else if (a.startsWith("-output=")) {
            outputFiles = a.substring("-output=".length());
        } else if (a.startsWith("-eyepiece=")) {
            eyepiece = a.substring("-eyepiece=".length());
        } else if (a.startsWith("-stylesPath=")) {
            stylesPath = a.substring("-stylesPath=".length());
        }
    }
    String backup = null;
    if (nativeLib != null) {
        boolean old = NativeLibrary.loadOldLib(nativeLib);
        NativeLibrary nl = new NativeLibrary();
        nl.loadFontData(new File("fonts"));
        if (!old) {
            throw new UnsupportedOperationException("Not supported");
        }
    }
    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    org.w3c.dom.Document doc = docBuilder.parse(new File(gpxFile));
    Element de = doc.getDocumentElement();
    String defZoom = getAttribute(de, "zoom", "11");
    String defMapDensity = getAttribute(de, "mapDensity", "1");
    String defRenderingName = getAttribute(de, "renderingName", "default");
    String defRenderingProps = getAttribute(de, "renderingProperties", "");
    String defWidth = getAttribute(de, "width", "1366");
    String defHeight = getAttribute(de, "height", "768");
    NodeList nl = doc.getElementsByTagName("wpt");
    NodeList gen = doc.getElementsByTagName("html");
    File gpx = new File(gpxFile);
    HTMLContent html = null;
    if (gen.getLength() > 0) {
        String gpxName = gpx.getName().substring(0, gpx.getName().length() - 4);
        String outputFName = gpxName;
        if (eyepiece != null) {
        // outputFName += "_eye";
        }
        html = new HTMLContent(new File(gpx.getParentFile(), outputFName + ".html"), websiteUrl + gpxName);
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(gen.item(0)), new StreamResult(sw));
        html.setContent(sw.toString());
    }
    for (int i = 0; i < nl.getLength(); i++) {
        Element e = (Element) nl.item(i);
        double lat = Double.parseDouble(e.getAttribute("lat"));
        double lon = Double.parseDouble(e.getAttribute("lon"));
        int imageHeight = Integer.parseInt(getSubAttr(e, "height", defHeight));
        int imageWidth = Integer.parseInt(getSubAttr(e, "width", defWidth));
        String name = getSubAttr(e, "name", (lat + "!" + lon).replace('.', '_'));
        String maps = getSubAttr(e, "maps", "");
        String mapDensities = getSubAttr(e, "mapDensity", defMapDensity);
        String zooms = getSubAttr(e, "zoom", defZoom);
        String renderingNames = getSubAttr(e, "renderingName", defRenderingName);
        String renderingProperties = getSubAttr(e, "renderingProperties", defRenderingProps);
        if (maps.isEmpty()) {
            throw new UnsupportedOperationException("No maps element found for wpt " + name);
        }
        NativeSwingRendering nsr = nativeLib != null ? new NativeSwingRendering() : null;
        // nsr.initFilesInDir(new File(dirWithObf));
        ArrayList<File> obfFiles = new ArrayList<File>();
        initMaps(dirWithObf, backup, gpxFile, maps, nsr, obfFiles);
        List<ImageCombination> ls = getCombinations(name, zooms, mapDensities, renderingNames, renderingProperties);
        if (html != null) {
            html.newRow(getSubAttr(e, "desc", ""));
        }
        for (ImageCombination ic : ls) {
            System.out.println("Generate " + ic.generateName + " style " + ic.renderingStyle);
            if (eyepiece != null) {
                try {
                    String line = eyepiece;
                    double dx = ic.mapDensity;
                    line += " -latLon=" + lat + ";" + lon;
                    line += " -stylesPath=\"" + stylesPath + "\"";
                    String styleName = ic.renderingStyle;
                    if (styleName.endsWith(".render.xml")) {
                        styleName = styleName.substring(0, styleName.length() - ".render.xml".length());
                    }
                    line += " -styleName=" + styleName;
                    for (File f : obfFiles) {
                        line += " -obfFile=\"" + f.getAbsolutePath() + "\"";
                    }
                    String[] listProps = ic.renderingProperties.split(",");
                    for (String p : listProps) {
                        if (p.trim().length() > 0) {
                            line += " -styleSetting:" + p;
                        }
                    }
                    // line += " -useLegacyContext";
                    line += " -outputImageWidth=" + imageWidth;
                    line += " -outputImageHeight=" + imageHeight;
                    final String fileName = ic.generateName + ".png";
                    line += " -outputImageFilename=\"" + outputFiles + "/" + fileName + "\"";
                    line += " -referenceTileSize=" + (int) (dx * 256);
                    line += " -displayDensityFactor=" + dx;
                    line += " -zoom=" + ic.zoom;
                    line += " -outputImageFormat=png";
                    line += " -verbose";
                    System.out.println(line);
                    Process p = Runtime.getRuntime().exec(line);
                    BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                    while ((line = input.readLine()) != null) {
                        System.out.println(line);
                    }
                    input.close();
                    if (html != null) {
                        html.addFile(fileName);
                    }
                } catch (Exception err) {
                    err.printStackTrace();
                }
            }
            if (nativeLib != null) {
                nsr.loadRuleStorage(ic.renderingStyle, ic.renderingProperties);
                BufferedImage mg = nsr.renderImage(new RenderingImageContext(lat, lon, imageWidth, imageHeight, ic.zoom, ic.mapDensity));
                ImageWriter writer = ImageIO.getImageWritersBySuffix("png").next();
                final String fileName = ic.generateName + ".png";
                if (html != null) {
                    html.addFile(fileName);
                }
                writer.setOutput(new FileImageOutputStream(new File(outputFiles, fileName)));
                writer.write(mg);
            }
        }
    }
    if (html != null) {
        html.write();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ImageWriter(javax.imageio.ImageWriter) BufferedImage(java.awt.image.BufferedImage) RenderingImageContext(net.osmand.swing.NativeSwingRendering.RenderingImageContext) StringWriter(java.io.StringWriter) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) InputStreamReader(java.io.InputStreamReader) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) NodeList(org.w3c.dom.NodeList) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) BufferedReader(java.io.BufferedReader) NativeLibrary(net.osmand.NativeLibrary) File(java.io.File)

Aggregations

FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)21 File (java.io.File)18 ImageWriter (javax.imageio.ImageWriter)14 IOException (java.io.IOException)13 IIOImage (javax.imageio.IIOImage)13 ImageWriteParam (javax.imageio.ImageWriteParam)10 BufferedImage (java.awt.image.BufferedImage)9 ImageOutputStream (javax.imageio.stream.ImageOutputStream)8 Rectangle (java.awt.Rectangle)4 Iterator (java.util.Iterator)3 ImageReader (javax.imageio.ImageReader)3 FileImageInputStream (javax.imageio.stream.FileImageInputStream)3 Test (org.junit.Test)3 Graphics2D (java.awt.Graphics2D)2 Robot (java.awt.Robot)2 BufferedInputStream (java.io.BufferedInputStream)2 URLConnection (java.net.URLConnection)2 DataFormatException (java.util.zip.DataFormatException)2 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)2 ImageWriterSpi (javax.imageio.spi.ImageWriterSpi)2