Java Code Examples for java.awt.image.BufferedImage.flush()

The following are Jave code examples for showing how to use flush() of the java.awt.image.BufferedImage class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: rekit-game   File: ImageManagement.java   View Source Code Vote up 7 votes
/**
 * Convert {@link AbstractImage} to {@link Image}.
 *
 * @param in
 *            the input image
 * @return the converted image
 */
public static Image toImage(AbstractImage in) {
	if (in == null) {
		return null;
	}
	BufferedImage res = new BufferedImage(in.width, in.height, BufferedImage.TYPE_INT_ARGB);
	int[] pixels = new int[in.pixels.length];
	for (int i = 0; i < pixels.length; i += 1) {
		pixels[i] = in.pixels[i] & 0xFF;
	}
	WritableRaster raster = Raster.createWritableRaster(res.getSampleModel(), null);
	raster.setPixels(0, 0, in.width, in.height, pixels);
	res.setData(raster);
	res.flush();
	return res;

}
 
Example 2
Project: NoMoreOversleeps   File: WebcamDefaultDevice.java   View Source Code Vote up 6 votes
@Override
public BufferedImage getImage()
{

	ByteBuffer buffer = this.getImageBytes();

	if (buffer == null)
	{
		LOG.error("Images bytes buffer is null!");
		return null;
	}

	byte[] bytes = new byte[this.size.width * this.size.height * 3];
	byte[][] data = new byte[][] { bytes };

	buffer.get(bytes);

	DataBufferByte dbuf = new DataBufferByte(data, bytes.length, OFFSET);
	WritableRaster raster = Raster.createWritableRaster(this.smodel, dbuf, null);

	BufferedImage bi = new BufferedImage(this.cmodel, raster, false, null);
	bi.flush();

	return bi;
}
 
Example 3
Project: myfaces-trinidad   File: OffscreenWrappingPainter.java   View Source Code Vote up 6 votes
@Override
public Dimension getPreferredSize(PaintContext context)
{
  Dimension size = null;

  // Get a shared buffer to use for measuring
  BufferedImage buffer = _createOffscreenBuffer(context, 1, 1);

  if (buffer != null)
  {
    Graphics g = _getInitializedGraphics(context, buffer);
    size = super.getPreferredSize(new ProxyContext(context, g));

    // Clean up
    g.dispose();
    buffer.flush();
  }
  else
  {
    // If we didn't get a buffer, just paint the contents directly
    size = super.getPreferredSize(context);
  }

  return size;
}
 
Example 4
Project: Nukkit-AntiCheat   File: AntiAutoAim.java   View Source Code Vote up 6 votes
private static byte[] parseBufferedImage(BufferedImage image) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    for(int y = 0; y < image.getHeight(); ++y) {
        for(int x = 0; x < image.getWidth(); ++x) {
            Color color = new Color(image.getRGB(x, y), true);
            outputStream.write(color.getRed());
            outputStream.write(color.getGreen());
            outputStream.write(color.getBlue());
            outputStream.write(color.getAlpha());
        }
    }

    image.flush();
    return outputStream.toByteArray();
}
 
Example 5
Project: Nukkit-Java9   File: Skin.java   View Source Code Vote up 5 votes
public void parseBufferedImage(BufferedImage image) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            Color color = new Color(image.getRGB(x, y), true);
            outputStream.write(color.getRed());
            outputStream.write(color.getGreen());
            outputStream.write(color.getBlue());
            outputStream.write(color.getAlpha());
        }
    }
    image.flush();
    this.setData(outputStream.toByteArray());
}
 
Example 6
Project: Nukkit-Java9   File: Skin.java   View Source Code Vote up 5 votes
public void parseBufferedImage(BufferedImage image) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            Color color = new Color(image.getRGB(x, y), true);
            outputStream.write(color.getRed());
            outputStream.write(color.getGreen());
            outputStream.write(color.getBlue());
            outputStream.write(color.getAlpha());
        }
    }
    image.flush();
    this.setData(outputStream.toByteArray());
}
 
Example 7
Project: Jupiter   File: Skin.java   View Source Code Vote up 5 votes
public void parseBufferedImage(BufferedImage image) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            Color color = new Color(image.getRGB(x, y), true);
            outputStream.write(color.getRed());
            outputStream.write(color.getGreen());
            outputStream.write(color.getBlue());
            outputStream.write(color.getAlpha());
        }
    }
    image.flush();
    this.setSkinData(outputStream.toByteArray());
}
 
Example 8
Project: OpenJSharp   File: WComponentPeer.java   View Source Code Vote up 5 votes
@Override
public void print(Graphics g) {

    Component comp = (Component)target;

    // To conserve memory usage, we will band the image.

    int totalW = comp.getWidth();
    int totalH = comp.getHeight();

    int hInc = (int)(totalH / BANDING_DIVISOR);
    if (hInc == 0) {
        hInc = totalH;
    }

    for (int startY = 0; startY < totalH; startY += hInc) {
        int endY = startY + hInc - 1;
        if (endY >= totalH) {
            endY = totalH - 1;
        }
        int h = endY - startY + 1;

        Color bgColor = comp.getBackground();
        int[] pix = createPrintedPixels(0, startY, totalW, h,
                                        bgColor == null ? 255 : bgColor.getAlpha());
        if (pix != null) {
            BufferedImage bim = new BufferedImage(totalW, h,
                                          BufferedImage.TYPE_INT_ARGB);
            bim.setRGB(0, 0, totalW, h, pix, 0, totalW);
            g.drawImage(bim, 0, startY, null);
            bim.flush();
        }
    }

    comp.print(g);
}
 
Example 9
Project: Jenisys3   File: Skin.java   View Source Code Vote up 5 votes
public void parseBufferedImage(BufferedImage image) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            Color color = new Color(image.getRGB(x, y), true);
            outputStream.write(color.getRed());
            outputStream.write(color.getGreen());
            outputStream.write(color.getBlue());
            outputStream.write(color.getAlpha());
        }
    }
    image.flush();
    this.setData(outputStream.toByteArray());
}
 
Example 10
Project: jdk8u-jdk   File: WComponentPeer.java   View Source Code Vote up 5 votes
@Override
public void print(Graphics g) {

    Component comp = (Component)target;

    // To conserve memory usage, we will band the image.

    int totalW = comp.getWidth();
    int totalH = comp.getHeight();

    int hInc = (int)(totalH / BANDING_DIVISOR);
    if (hInc == 0) {
        hInc = totalH;
    }

    for (int startY = 0; startY < totalH; startY += hInc) {
        int endY = startY + hInc - 1;
        if (endY >= totalH) {
            endY = totalH - 1;
        }
        int h = endY - startY + 1;

        Color bgColor = comp.getBackground();
        int[] pix = createPrintedPixels(0, startY, totalW, h,
                                        bgColor == null ? 255 : bgColor.getAlpha());
        if (pix != null) {
            BufferedImage bim = new BufferedImage(totalW, h,
                                          BufferedImage.TYPE_INT_ARGB);
            bim.setRGB(0, 0, totalW, h, pix, 0, totalW);
            g.drawImage(bim, 0, startY, null);
            bim.flush();
        }
    }

    comp.print(g);
}
 
Example 11
Project: jdk8u-jdk   File: PrintAllXcheckJNI.java   View Source Code Vote up 5 votes
public static void main(String []s)
{
    Frame frame = new Frame();
    frame.setVisible(true);

    BufferedImage img = new BufferedImage(frame.getWidth(),
                                          frame.getHeight(),
                                          BufferedImage.TYPE_INT_RGB);
    Graphics2D g = img.createGraphics();

    frame.printAll(g);

    g.dispose();
    img.flush();
}
 
Example 12
Project: openjdk-jdk10   File: WComponentPeer.java   View Source Code Vote up 5 votes
@Override
public void print(Graphics g) {

    Component comp = (Component)target;

    // To conserve memory usage, we will band the image.

    int totalW = comp.getWidth();
    int totalH = comp.getHeight();

    int hInc = (int)(totalH / BANDING_DIVISOR);
    if (hInc == 0) {
        hInc = totalH;
    }

    for (int startY = 0; startY < totalH; startY += hInc) {
        int endY = startY + hInc - 1;
        if (endY >= totalH) {
            endY = totalH - 1;
        }
        int h = endY - startY + 1;

        Color bgColor = comp.getBackground();
        int[] pix = createPrintedPixels(0, startY, totalW, h,
                                        bgColor == null ? 255 : bgColor.getAlpha());
        if (pix != null) {
            BufferedImage bim = new BufferedImage(totalW, h,
                                          BufferedImage.TYPE_INT_ARGB);
            bim.setRGB(0, 0, totalW, h, pix, 0, totalW);
            g.drawImage(bim, 0, startY, null);
            bim.flush();
        }
    }

    comp.print(g);
}
 
Example 13
Project: openjdk-jdk10   File: PrintAllXcheckJNI.java   View Source Code Vote up 5 votes
public static void main(String []s)
{
    Frame frame = new Frame();
    frame.setVisible(true);

    BufferedImage img = new BufferedImage(frame.getWidth(),
                                          frame.getHeight(),
                                          BufferedImage.TYPE_INT_RGB);
    Graphics2D g = img.createGraphics();

    frame.printAll(g);

    g.dispose();
    img.flush();
}
 
Example 14
Project: BasicsProject   File: GifCaptcha.java   View Source Code Vote up 5 votes
@Override
 public void out(OutputStream os)
 {
     try
     {
         GifEncoder gifEncoder = new GifEncoder();   // gif编码类,这个利用了洋人写的编码类,所有类都在附件中
         //生成字符
         gifEncoder.start(os);
         gifEncoder.setQuality(180);
         gifEncoder.setDelay(100);
         gifEncoder.setRepeat(0);
         BufferedImage frame;
         char[] rands =alphas();
         Color fontcolor[]=new Color[len];
         for(int i=0;i<len;i++)
         {
             fontcolor[i]=new Color(20 + num(110), 20 + num(110), 20 + num(110));
         }
         for(int i=0;i<len;i++)
         {
             frame=graphicsImage(fontcolor, rands, i);
             gifEncoder.addFrame(frame);
             frame.flush();
         }
         gifEncoder.finish();
     }finally
     {
     	try {
	os.close();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
     }

 }
 
Example 15
Project: CoreX   File: Skin.java   View Source Code Vote up 5 votes
public void parseBufferedImage(BufferedImage image) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            Color color = new Color(image.getRGB(x, y), true);
            outputStream.write(color.getRed());
            outputStream.write(color.getGreen());
            outputStream.write(color.getBlue());
            outputStream.write(color.getAlpha());
        }
    }
    image.flush();
    this.setData(outputStream.toByteArray());
}
 
Example 16
Project: NoMoreOversleeps   File: WebcamCapture.java   View Source Code Vote up 4 votes
@Override
public BufferedImage transform(BufferedImage image)
{
	image = GRAY.filter(image, null);
	Graphics2D graphics = image.createGraphics();
	Font font = new Font("ARIAL", Font.PLAIN, 11);
	if (privacyMode)
	{
		graphics.setColor(Color.DARK_GRAY);
		graphics.fillRect(0, 0, 320, 240);
	}
	graphics.setFont(font);
	graphics.setColor(Color.BLACK);
	graphics.fillRect(0, 0, 320, 20);
	graphics.setColor(Color.WHITE);
	long now = System.currentTimeMillis();
	String str = CommonUtils.convertTimestamp(now);
	if (NMOStatistics.INSTANCE.scheduleStartedOn != 0)
	{
		str = str + "   " + FormattingHelper.formatTimeElapsedWithDays(NMOStatistics.INSTANCE.scheduleStartedOn == 0 ? 0 : now, NMOStatistics.INSTANCE.scheduleStartedOn) + "   " + FormattingHelper.formatTimeElapsedWithDays(NMOStatistics.INSTANCE.scheduleStartedOn == 0 ? 0 : now, NMOStatistics.INSTANCE.scheduleLastOversleep);
	}
	graphics.drawString(str, 4, 14);
	if (MainDialog.isCurrentlyPaused.get() && !((MainDialog.scheduleStatusShort.startsWith("CORE ") || MainDialog.scheduleStatusShort.startsWith("NAP ") || MainDialog.scheduleStatusShort.startsWith("SIESTA ")) && MainDialog.pauseReason.startsWith("Sleep block: ")))
	{
		graphics.setColor(Color.BLACK);
		graphics.fillRect(0, 204, 320, 36);
		graphics.setColor(Color.WHITE);
		graphics.drawString("PAUSED for \"" + MainDialog.pauseReason + "\"\n", 4, 218);
		graphics.drawString("until " + CommonUtils.dateFormatter.format(MainDialog.pausedUntil), 4, 234);
	}
	else
	{
		graphics.setColor(Color.BLACK);
		graphics.fillRect(0, 220, 320, 20);
		graphics.setColor(Color.WHITE);
		graphics.drawString(MainDialog.scheduleStatus, 4, 234);
	}
	image.flush();
	graphics.dispose();
	return image;
}
 
Example 17
Project: myfaces-trinidad   File: PainterImageRenderer.java   View Source Code Vote up 4 votes
/**
  * Render the image given the Map of
  * properties that describe what to render.  A PaintContext
  * object is created using the given Map of properties.
  */
@Override
public Image renderImage(
  ImageContext imageContext,
  Map<Object, Object> requestedProperties,
  Map<Object, Object> responseProperties
  )
{
    if (!isRenderable(imageContext, requestedProperties))
    {
      return null;
    }

    Painter painter = getPainter(imageContext, requestedProperties);

    // First we measure the preferred size using a dummy image of
    // size 1x1 - we need this image to get the Graphics obejct
    // for measuring.
    BufferedImage measureImage = createImage(1, 1);

    // Create a PaintContext to use for measuring
    PaintContext measureContext = createPaintContext(imageContext,
                                                     measureImage,
                                                     requestedProperties,
                                                     responseProperties);

    // Get a measurement for the requested image
    Dimension d = painter.getPreferredSize(measureContext);

    int width = d.width;
    int height = d.height;

    // We're done with the measure image and context - free them up
    measureImage.flush();
    disposePaintContext(measureContext);

    // Now that we know how big the image should be, create the image
    // that we'll use for painting
    BufferedImage paintImage = createImage(width, height);

    // Create a PaintContext to use for drawing
    PaintContext paintContext = createPaintContext(imageContext,
                                                   paintImage,
                                                   requestedProperties,
                                                   responseProperties);

    // Fill in the image with the surrounding color
    Graphics g = paintContext.getPaintGraphics();
    Color oldColor = g.getColor();
    g.setColor(paintContext.getSurroundingColor());
    g.fillRect(0, 0, width, height);
    g.setColor(oldColor);

    // Paint the image
    painter.paint(paintContext, g, 0, 0, width, height);

    // Now that we are done painting, dispose the PaintContext
    disposePaintContext(paintContext);

    // Store width/height for client
    responseProperties.put(WIDTH_RESPONSE_KEY,
                           width);
    responseProperties.put(HEIGHT_RESPONSE_KEY,
                           height);

    return paintImage;
}
 
Example 18
Project: myfaces-trinidad   File: OffscreenWrappingPainter.java   View Source Code Vote up 4 votes
@Override
public void paint(
  PaintContext context,
  Graphics     g,
  int          x,
  int          y,
  int          width,
  int          height
  )
{
  // On X servers with limited color capacities, we have problems with
  // getting the background color of the offscreen image to match the
  // actual background color of the destination BufferedImage.  The
  // problem is that when the X server runs out of colors, it uses a
  // near match - which might not be the background color that we want.
  // The result is that the area behind the text may have a different
  // background color than the rest of the image.  (eg. in buttons, the
  // bounding box of the text has a white background, while the rest of
  // the button content is the light off-white specified by BLAF.)
  //
  // To work around this problem, we convert background pixels to
  // transparent before drawing the offscreen buffer.  However, we
  // can't simply filter on pixels where the rgb value is equal to
  // our desired background rgb value - as the actual rgb value is
  // picked by the X server.  So, we've got a real hack here...
  //
  // We make the offscreen buffer one pixel taller than needed.
  // We fill this entire area, including the extra pixel scan line
  // at the top, with the background color.  Then, we draw the content
  // starting at y=1.  So, when we get around to filtering the background,
  // we know that the pixels at y=0 are the background color - all pixel
  // values which match the value at 0, 0 are filtered to transparent.
  // Yeah, I know this is insane.  Feel free to rip this out if you've
  // got a better solution.

  // Create the offscreen buffer.  We make it one pixel taller than
  // necessary.  We want the top scan line to be filled with the background
  // color, but without any actual content, so that we can use it later
  // (during transparency filtering) to get the real background color.
  BufferedImage buffer = _createOffscreenBuffer(context, width, height + 1);

  if (buffer == null)
  {
    super.paint(context, g, x, y, width, height);
    return;
  }

  // If we've got a buffer, use it's graphics object for rendering
  // our wrapped painter
  Graphics offscreenG = _getInitializedGraphics(context, buffer);

  // Fill in the background - including the extra 1 pixel at the top
  offscreenG.setColor(context.getPaintBackground());
  offscreenG.fillRect(0, 0, width, height + 1);

  // Reset for text rendering
  offscreenG.setColor(g.getColor());
  offscreenG.translate(-x, -y);

  // Render the wrapped painter into the offscreen buffer.  We offset
  // the y coordinate by one so that no content will be rendered into
  // the top pixel.
  super.paint(context, offscreenG, x, y + 1, width, height);

  // Filter out the background
  Image transparentImage = ImageUtils.createFilteredImage(buffer,
                             new TransparencyFilter());
  ImageUtils.loadImage(transparentImage);

  // Now, render the transparent image into the original in Graphics object
  g.drawImage(transparentImage,
              x, y, x+width, y+height, 0, 1, width, height + 1, this);

  // Clean up
  offscreenG.dispose();
  transparentImage.flush();
  buffer.flush();
}
 
Example 19
Project: myfaces-trinidad   File: BlafImageUtils.java   View Source Code Vote up 4 votes
static ImmInsets __getDialogPadding()
{
  if (_sDialogPadding != null)
    return _sDialogPadding;

  // Create a BufferedImage that we can use to rasterize some glyphs.
  int width = 40;
  int height = 40;
  BufferedImage image = new BufferedImage(40,
                                          40,
                                          BufferedImage.TYPE_INT_ARGB);

  // Get the Graphics object to use to draw into the image
  Graphics g = image.getGraphics();

  // Clear out the image
  g.setColor(Color.white);
  g.fillRect(0, 0, width, height);

  // Render our glyphs
  g.setColor(Color.black);
  g.setFont(new Font("Dialog", Font.PLAIN, 12));

  FontMetrics metrics = g.getFontMetrics();
  int baseline = metrics.getAscent();

  g.drawString("X", 0, baseline);

  // Now that we have rendered the glyphs, we examine the
  // image to see how many lines of padding we've got.
  int top = 0;
  for (int y = 0; y < height; y++)
  {
    if (!_isWhiteScanline(image, y, width))
    {
      top = y;
      break;
    }
  }

  // Just use the descent as the bottom padding
  int bottom = metrics.getDescent();

  _sDialogPadding = new ImmInsets(top, 0, bottom, 0);


  // Clean up
  g.dispose();
  image.flush();

  return _sDialogPadding;
}
 
Example 20
Project: Yass   File: YassGroups.java   View Source Code Vote up 4 votes
/**
 * Gets the icon attribute of the YassGroups object
 *
 * @param s Description of the Parameter
 * @return The icon value
 */
public ImageIcon getCover(String s) {
    //System.out.println("getcover " + s);
    ImageIcon ii = covers.get(s);
    if (ii != null) {
        return ii;
    }

    try {
        s = s.replace(':', '_');
        s = s.replace('?', '_');
        s = s.replace('/', '_');
        s = s.replace('\\', '_');
        s = s.replace('*', '_');
        s = s.replace('\"', '_');
        s = s.replace('<', '_');
        s = s.replace('>', '_');
        s = s.replace('|', '_');

        String coverDir = prop.getProperty("cover-directory");

        File file = new File(coverDir + File.separator + s + ".jpg");
        BufferedImage img;
        if (file.exists()) {
            img = javax.imageio.ImageIO.read(file);
        } else {
            java.net.URL is = I18.getResource(s + ".jpg");
            // throws exception when not found
            img = YassUtils.readImage(is);
        }

        //attention: scale down only
        BufferedImage bufferedImage = YassUtils.getScaledInstance(img, WIDTH, WIDTH);

        //BufferedImage bufferedImage = new BufferedImage(WIDTH, WIDTH, BufferedImage.TYPE_INT_RGB);
        //Graphics2D g2d = bufferedImage.createGraphics();
        //g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        //g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        //g2d.drawImage(img, 0, 0, WIDTH, WIDTH, null);
        //g2d.dispose();
        img.flush();

        ii = new ImageIcon(bufferedImage);
        covers.put(s, ii);
        return ii;
    } catch (Exception e) {
        //e.printStackTrace();
    }
    return null;
}