public static void main(String[] args) throws Exception { String cmd = args[0].intern(); if (cmd == "render") { PosixArgs opt = PosixArgs.getopt(args, 1, "aw:f:s:"); boolean aa = false; String font = "SansSerif"; int width = 100, size = 10; for (char c : opt.parsed()) { if (c == 'a') { aa = true; } else if (c == 'f') { font = opt.arg; } else if (c == 'w') { width = Integer.parseInt(opt.arg); } else if (c == 's') { size = Integer.parseInt(opt.arg); } } Foundry f = new Foundry(font, size); f.aa = aa; Text t = f.renderwrap(opt.rest[0], width); java.io.OutputStream out = new java.io.FileOutputStream(opt.rest[1]); javax.imageio.ImageIO.write(t.img, "PNG", out); out.close(); } }
public void saveFrametoPNG(String filename) { final int frameWidth = _canvas.getWidth(); final int frameHeight = _canvas.getHeight(); final ByteBuffer pixelsRGB = Direct.newByteBuffer(frameWidth * frameHeight * 3); _canvas.runWithContext( new Runnable() { public void run() { // glPushAttrib(GL_PIXEL_MODE_BIT); glReadBuffer(GL_BACK); glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(0, 0, frameWidth, frameHeight, GL_RGB, GL_UNSIGNED_BYTE, pixelsRGB); // glPopAttrib(); } }); int[] pixelInts = new int[frameWidth * frameHeight]; int p = frameWidth * frameHeight * 3; int q; // Index into ByteBuffer int i = 0; // Index into target int[] int w3 = frameWidth * 3; // Number of bytes in each row for (int row = 0; row < frameHeight; row++) { p -= w3; q = p; for (int col = 0; col < frameWidth; col++) { int iR = pixelsRGB.get(q++); int iG = pixelsRGB.get(q++); int iB = pixelsRGB.get(q++); pixelInts[i++] = 0xFF000000 | ((iR & 0x000000FF) << 16) | ((iG & 0x000000FF) << 8) | (iB & 0x000000FF); } } // Create a new BufferedImage from the pixeldata. BufferedImage bufferedImage = new BufferedImage(frameWidth, frameHeight, BufferedImage.TYPE_INT_ARGB); bufferedImage.setRGB(0, 0, frameWidth, frameHeight, pixelInts, 0, frameWidth); try { javax.imageio.ImageIO.write(bufferedImage, "PNG", new File(filename)); } catch (IOException e) { System.out.println("Error: ImageIO.write."); e.printStackTrace(); } /* End code taken from: http://www.felixgers.de/teaching/jogl/imagingProg.html */ /* final BufferedImage image = new BufferedImage( this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics gr = image.getGraphics(); this.printAll(gr); gr.dispose(); try { ImageIO.write(image, "PNG", new File(filename)); } catch (IOException e) { System.out.println( "Error: ImageIO.write." ); e.printStackTrace(); } */ }
/** * Make a BufferedImage from the contents of an image file. * * @param imageFileContents byte array containing the guts of a JPG, GIF, or PNG */ public BufferedImage makeBufferedImage(byte[] imageFileContents) { BufferedImage bi = null; try { InputStream in = new ByteArrayInputStream(imageFileContents); bi = javax.imageio.ImageIO.read(in); } catch (IOException ioe) { GLApp.err("GLImage.makeBufferedImage(): " + ioe); } return bi; }
private void seticon() { Image icon; try { InputStream data = MainFrame.class.getResourceAsStream("icon.png"); icon = javax.imageio.ImageIO.read(data); data.close(); } catch (IOException e) { throw (new Error(e)); } setIconImage(icon); }
/** * °´Â·¾¶±£´æ * * @param bufferImage * @param docNo * @throws Exception */ public static void saveImageToPriview( java.awt.image.BufferedImage bufferImage, String path, String docNo) throws Exception { StringBuffer temp = new StringBuffer(); java.io.File outFile = new java.io.File(path); if (!outFile.exists()) { outFile.mkdirs(); } java.io.File file = new java.io.File(outFile, temp.append(docNo).append(".png").toString()); if (!file.exists()) { java.io.FileOutputStream out = new java.io.FileOutputStream(file); javax.imageio.ImageIO.write(bufferImage, "png", out); out.close(); } }
/** Do screen capture and save it as image */ private static void captureScreenAndSave() { try { Robot robot = new Robot(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Rectangle rectangle = new Rectangle(0, 0, screenSize.width, screenSize.height); System.out.println("About to screen capture - " + rectangle); java.awt.image.BufferedImage image = robot.createScreenCapture(rectangle); javax.imageio.ImageIO.write(image, "jpg", new java.io.File("ScreenImage.jpg")); robot.delay(3000); } catch (Throwable t) { System.out.println("WARNING: Exception thrown while screen capture!"); t.printStackTrace(); } }
/** * Save an array of ARGB pixels to a PNG file. If flipY is true, flip the pixels on the Y axis * before saving. */ public static void savePixelsToPNG( int[] pixels, int width, int height, String imageFilename, boolean flipY) { if (pixels != null && imageFilename != null) { if (flipY) { // flip the pixels vertically (opengl has 0,0 at lower left, java is upper left) pixels = GLImage.flipPixels(pixels, width, height); } try { // Create a BufferedImage with the RGB pixels then save as PNG BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); image.setRGB(0, 0, width, height, pixels, 0, width); javax.imageio.ImageIO.write(image, "png", new File(imageFilename)); } catch (Exception e) { GLApp.err("GLImage.savePixelsToPNG(" + imageFilename + "): exception " + e); } } }
public static Image readImage(String filePath) throws IOException { return javax.imageio.ImageIO.read(new File(filePath)); }