Search in sources :

Example 86 with GraphicsEnvironment

use of java.awt.GraphicsEnvironment in project karaf by apache.

the class ScreenshotDumpProvider method createDump.

/**
	 * {@inheritDoc}
	 */
public void createDump(DumpDestination destination) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    // get all graphic devices attached to computer
    GraphicsDevice[] gs = ge.getScreenDevices();
    // create dump entry for each device  
    for (int i = 0; i < gs.length; i++) {
        DisplayMode mode = gs[i].getDisplayMode();
        Rectangle bounds = new Rectangle(0, 0, mode.getWidth(), mode.getHeight());
        BufferedImage screenshot = new Robot(gs[i]).createScreenCapture(bounds);
        // to attach your entry to destination you have to execute this line
        OutputStream outputStream = destination.add("screenshot/display_" + i + ".png");
        ImageIO.write(screenshot, "PNG", outputStream);
        outputStream.close();
    }
}
Also used : DisplayMode(java.awt.DisplayMode) GraphicsDevice(java.awt.GraphicsDevice) OutputStream(java.io.OutputStream) Rectangle(java.awt.Rectangle) GraphicsEnvironment(java.awt.GraphicsEnvironment) Robot(java.awt.Robot) BufferedImage(java.awt.image.BufferedImage)

Example 87 with GraphicsEnvironment

use of java.awt.GraphicsEnvironment in project poi by apache.

the class TestFontRendering method bug55902mixedFontWithChineseCharacters.

// @Ignore2("This fails on some systems because fonts are rendered slightly different")
@Test
public void bug55902mixedFontWithChineseCharacters() throws IOException, FontFormatException {
    // font files need to be downloaded first via
    // ant test-scratchpad-download-resources
    String[][] fontFiles = { // Calibri is not available on *nix systems, so we need to use another similar free font
    { "build/scratchpad-test-resources/Cabin-Regular.ttf", "mapped", "Calibri" }, // for the junit test not all chars are rendered
    { "build/scratchpad-test-resources/mona.ttf", "fallback", "Cabin" } };
    // setup fonts (especially needed, when run under *nix systems)
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Map<String, String> fontMap = new HashMap<String, String>();
    Map<String, String> fallbackMap = new HashMap<String, String>();
    for (String[] fontFile : fontFiles) {
        File f = new File(fontFile[0]);
        assumeTrue("necessary font file " + f.getName() + " not downloaded.", f.exists());
        Font font = Font.createFont(Font.TRUETYPE_FONT, f);
        ge.registerFont(font);
        Map<String, String> map = ("mapped".equals(fontFile[1]) ? fontMap : fallbackMap);
        map.put(fontFile[2], font.getFamily());
    }
    InputStream is = slTests.openResourceAsStream("bug55902-mixedFontChineseCharacters.ppt");
    HSLFSlideShow ss = new HSLFSlideShow(is);
    is.close();
    Dimension pgsize = ss.getPageSize();
    HSLFSlide slide = ss.getSlides().get(0);
    // render it
    double zoom = 1;
    AffineTransform at = new AffineTransform();
    at.setToScale(zoom, zoom);
    BufferedImage imgActual = new BufferedImage((int) Math.ceil(pgsize.width * zoom), (int) Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D graphics = imgActual.createGraphics();
    graphics.setRenderingHint(Drawable.FONT_FALLBACK, fallbackMap);
    graphics.setRenderingHint(Drawable.FONT_MAP, fontMap);
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    graphics.setTransform(at);
    graphics.setPaint(Color.white);
    graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
    slide.draw(graphics);
    BufferedImage imgExpected = ImageIO.read(slTests.getFile("bug55902-mixedChars.png"));
    DataBufferByte expectedDB = (DataBufferByte) imgExpected.getRaster().getDataBuffer();
    DataBufferByte actualDB = (DataBufferByte) imgActual.getRaster().getDataBuffer();
    byte[] expectedData = expectedDB.getData(0);
    byte[] actualData = actualDB.getData(0);
    // allow to find out what the actual difference is in CI where this fails currently
    if (!Arrays.equals(expectedData, actualData)) {
        ImageIO.write(imgActual, "PNG", TempFile.createTempFile("TestFontRendering", ".png"));
    }
    assertArrayEquals("Expected to have matching raster-arrays, but found differences", expectedData, actualData);
    ss.close();
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) Rectangle2D(java.awt.geom.Rectangle2D) Dimension(java.awt.Dimension) GraphicsEnvironment(java.awt.GraphicsEnvironment) DataBufferByte(java.awt.image.DataBufferByte) Font(java.awt.Font) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) AffineTransform(java.awt.geom.AffineTransform) TempFile(org.apache.poi.util.TempFile) File(java.io.File) Test(org.junit.Test)

Aggregations

GraphicsEnvironment (java.awt.GraphicsEnvironment)87 GraphicsDevice (java.awt.GraphicsDevice)44 GraphicsConfiguration (java.awt.GraphicsConfiguration)41 BufferedImage (java.awt.image.BufferedImage)22 VolatileImage (java.awt.image.VolatileImage)21 Dimension (java.awt.Dimension)16 Graphics2D (java.awt.Graphics2D)16 Point (java.awt.Point)11 Font (java.awt.Font)10 Rectangle (java.awt.Rectangle)9 Insets (java.awt.Insets)7 File (java.io.File)7 Frame (java.awt.Frame)6 Toolkit (java.awt.Toolkit)6 AffineTransform (java.awt.geom.AffineTransform)6 HeadlessException (java.awt.HeadlessException)4 Color (java.awt.Color)3 Container (java.awt.Container)3 DisplayMode (java.awt.DisplayMode)3 GradientPaint (java.awt.GradientPaint)3