Tabnine Logo
BufferedImage
Code IndexAdd Tabnine to your IDE (free)

How to use
BufferedImage
in
java.awt.image

Best Java code snippets using java.awt.image.BufferedImage (Showing top 20 results out of 15,921)

Refine searchRefine arrow

  • Graphics2D
  • Graphics
  • ImageIO
  • Window
  • JFrame
  • Container
  • Color
origin: nutzam/nutz

/**
 * 在一个RGB画布上重新绘制Image,解决CMYK图像偏色的问题
 */
public static BufferedImage redraw(BufferedImage img, Color bg) {
  BufferedImage rgbImage = new BufferedImage(img.getWidth(),
                        img.getHeight(),
                        BufferedImage.TYPE_3BYTE_BGR);
  Graphics2D g2d = rgbImage.createGraphics();
  g2d.drawImage(img, 0, 0, bg, null);
  g2d.dispose();
  return rgbImage;
}
origin: linlinjava/litemall

private void drawTextInImgCenter(BufferedImage baseImage, String textToWrite, int y) {
  Graphics2D g2D = (Graphics2D) baseImage.getGraphics();
  g2D.setColor(new Color(167, 136, 69));
  String fontName = "Microsoft YaHei";
  Font f = new Font(fontName, Font.PLAIN, 28);
  g2D.setFont(f);
  g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  // 计算文字长度,计算居中的x点坐标
  FontMetrics fm = g2D.getFontMetrics(f);
  int textWidth = fm.stringWidth(textToWrite);
  int widthX = (baseImage.getWidth() - textWidth) / 2;
  // 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
  g2D.drawString(textToWrite, widthX, y);
  // 释放对象
  g2D.dispose();
}
origin: stackoverflow.com

 public static void main(String... args) throws Exception {

  BufferedImage image = ImageIO.read(
     new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));

  int w = image.getWidth();
  int h = image.getHeight();

  int[] dataBuffInt = image.getRGB(0, 0, w, h, null, 0, w); 

  Color c = new Color(dataBuffInt[100]);

  System.out.println(c.getRed());   // = (dataBuffInt[100] >> 16) & 0xFF
  System.out.println(c.getGreen()); // = (dataBuffInt[100] >> 8)  & 0xFF
  System.out.println(c.getBlue());  // = (dataBuffInt[100] >> 0)  & 0xFF
  System.out.println(c.getAlpha()); // = (dataBuffInt[100] >> 24) & 0xFF
}
origin: libgdx/libgdx

  @Override
  public int compare (BufferedImage o1, BufferedImage o2) {
    return o2.getWidth() * o2.getHeight() - o1.getWidth() * o1.getHeight();
  }
});
origin: libgdx/libgdx

static private void plot (BufferedImage dst, int x, int y, int argb) {
  if (0 <= x && x < dst.getWidth() && 0 <= y && y < dst.getHeight()) dst.setRGB(x, y, argb);
}
origin: libgdx/libgdx

public Rect (BufferedImage source, int left, int top, int newWidth, int newHeight, boolean isPatch) {
  image = new BufferedImage(source.getColorModel(),
    source.getRaster().createWritableChild(left, top, newWidth, newHeight, 0, 0, null),
    source.getColorModel().isAlphaPremultiplied(), null);
  offsetX = left;
  offsetY = top;
  regionWidth = newWidth;
  regionHeight = newHeight;
  originalWidth = source.getWidth();
  originalHeight = source.getHeight();
  width = newWidth;
  height = newHeight;
  this.isPatch = isPatch;
}
origin: stackoverflow.com

Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(new Color(0xFFAA00));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(basicStroke);
g2d.drawRoundRect(3, 3, getWidth()-4, getHeight()-4, archH, archH);
setLayout(new GridBagLayout()); 
Graphics2D g2d = (Graphics2D)g.create(); // cloning to work, it is safer aproach
Rectangle2D txRect = new Rectangle2D.Double(0, 0, textureImg.getWidth(), textureImg.getHeight());
TexturePaint txPaint = new TexturePaint(textureImg, txRect);
g2d.setPaint(txPaint);
Graphics2D g2d = (Graphics2D) g.create();
if(gradientImage==null || gradientImage.getHeight() != getHeight())
BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
  Graphics2D g2d = (Graphics2D) image.getGraphics();
  g2d.setPaint(lgrPaint);
MainContainer.textureImg = MainContainer.textureImg.getSubimage(0, 0, 200, 200);
    JFrame frame = new JFrame("Demo: LogIn Dialogue");
origin: stackoverflow.com

+ "The effect we want is a multi-line label.";
BufferedImage image = new BufferedImage(
  400,
  300,
  BufferedImage.TYPE_INT_RGB);
Graphics2D imageGraphics = image.createGraphics();
GradientPaint gp = new GradientPaint(
  20f,
BufferedImage bi = new BufferedImage(
  d.width,
  d.height,
  BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.setColor(new Color(255, 255, 255, 128));
g.fillRoundRect(
  0,
  0,
  bi.getWidth(f),
  bi.getHeight(f),
  15,
Graphics g2 = image.getGraphics();
JLabel imageLabel = new JLabel(ii);
f.setVisible(true);
origin: stackoverflow.com

  image = ImageIO.read(new URL(
      "http://sstatic.net/stackoverflow/img/logo.png"));
} catch (IOException e) {
return new Dimension(image.getWidth(), image.getHeight());
int w = old.getWidth();
int h = old.getHeight();
BufferedImage img = new BufferedImage(
    w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(old, 0, 0, null);
g2d.setPaint(Color.red);
g2d.setFont(new Font("Serif", Font.BOLD, 20));
String s = "Hello, world!";
FontMetrics fm = g2d.getFontMetrics();
int x = img.getWidth() - fm.stringWidth(s) - 5;
int y = fm.getHeight();
g2d.drawString(s, x, y);
g2d.dispose();
return img;
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new TextOverlay());
f.pack();
f.setVisible(true);
origin: libgdx/libgdx

BufferedImage canvas = new BufferedImage(width, height, getBufferedImageType(settings.format));
Graphics2D g = (Graphics2D)canvas.getGraphics();
if (!settings.silent) System.out.println("Writing " + canvas.getWidth() + "x" + canvas.getHeight() + ": " + outputFile);
  Rect rect = page.outputRects.get(r);
  BufferedImage image = rect.getImage(imageProcessor);
  int iw = image.getWidth();
  int ih = image.getHeight();
  int rectX = page.x + rect.x, rectY = page.y + page.height - rect.y - (rect.height - settings.paddingY);
  if (settings.duplicatePadding) {
          plot(canvas, rectX - j, rectY + iw - 1 + i, image.getRGB(0, 0));
          plot(canvas, rectX + ih - 1 + j, rectY + iw - 1 + i, image.getRGB(0, ih - 1));
          plot(canvas, rectX - j, rectY - i, image.getRGB(iw - 1, 0));
          plot(canvas, rectX + ih - 1 + j, rectY - i, image.getRGB(iw - 1, ih - 1));
          plot(canvas, rectX - i, rectY + iw - 1 - j, image.getRGB(j, 0));
          plot(canvas, rectX + ih - 1 + i, rectY + iw - 1 - j, image.getRGB(j, ih - 1));
          plot(canvas, rectX + j, rectY - i, image.getRGB(iw - 1, j));
          plot(canvas, rectX + j, rectY + iw - 1 + i, image.getRGB(0, j));
          plot(canvas, rectX - i, rectY - j, image.getRGB(0, 0));
          plot(canvas, rectX - i, rectY + ih - 1 + j, image.getRGB(0, ih - 1));
          plot(canvas, rectX + iw - 1 + i, rectY - j, image.getRGB(iw - 1, 0));
          plot(canvas, rectX + iw - 1 + i, rectY + ih - 1 + j, image.getRGB(iw - 1, ih - 1));
  && !(settings.outputFormat.equalsIgnoreCase("jpg") || settings.outputFormat.equalsIgnoreCase("jpeg"))) {
origin: stackoverflow.com

BufferedImage originalImage = ImageIO.read(url);
final BufferedImage textImage = new BufferedImage(
  originalImage.getWidth(),
  originalImage.getHeight(),
  BufferedImage.TYPE_INT_ARGB);
Graphics2D g = textImage.createGraphics();
FontRenderContext frc = g.getFontRenderContext();
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 250);
GlyphVector gv = font.createGlyphVector(frc, "Cat");
int yOff = 80+(int)-box.getY();
Shape shape = gv.getOutline(xOff,yOff);
g.setClip(shape);
g.drawImage(originalImage,0,0,null);
g.setClip(null);
g.setStroke(new BasicStroke(2f));
g.setColor(Color.BLACK);
g.setRenderingHint(
  RenderingHints.KEY_ANTIALIASING,
  RenderingHints.VALUE_ANTIALIAS_ON);
ImageIO.write(textImage,"png",file);
Desktop.getDesktop().open(file);
origin: kevin-wayne/algs4

private void init() {
  if (frame != null) frame.setVisible(false);
  frame = new JFrame();
  offscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  onscreenImage  = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  offscreen = offscreenImage.createGraphics();
  onscreen  = onscreenImage.createGraphics();
  setXscale();
  setYscale();
  offscreen.setColor(DEFAULT_CLEAR_COLOR);
  offscreen.fillRect(0, 0, width, height);
  setPenColor();
  setPenRadius();
                       RenderingHints.VALUE_ANTIALIAS_ON);
  hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  offscreen.addRenderingHints(hints);
  draw.addMouseMotionListener(this);
  frame.setContentPane(draw);
  frame.addKeyListener(this);    // JLabel cannot get keyboard focus
  frame.setResizable(false);
origin: stackoverflow.com

BufferedImage img = new BufferedImage(PREF_W, PREF_H,
   BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
   RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(BASIC_STROKE);
g2.setColor(Color.blue);
int circleCount = 10;
for (int i = 0; i < circleCount ; i++) {
  int w = PREF_W - 2 * x;
  int h = w;
  g2.drawOval(x, y, w, h);
g2.dispose();
return img;
BufferedImage img = new BufferedImage(PREF_W, PREF_H,
   BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
   RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(BASIC_STROKE);
origin: stackoverflow.com

    JFrame frame = new JFrame();
    frame.setLayout(new GridLayout(N, N, N, N));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    for (int i = 0; i < N * N; i++) {
      frame.add(new RotatePanel());
    frame.pack();
    frame.setVisible(true);
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
g2d.rotate(theta);
g2d.translate(-image.getWidth(this) / 2, -image.getHeight(this) / 2);
g2d.drawImage(image, 0, 0, null);
BufferedImage bi = new BufferedImage(
  size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(
  RenderingHints.KEY_ANTIALIASING,
  RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.getHSBColor(r.nextFloat(), 1, 1));
g2d.setStroke(new BasicStroke(size / 8));
g2d.drawLine(0, size / 2, size, size / 2);
g2d.drawLine(size / 2, 0, size / 2, size);
g2d.dispose();
return bi;
origin: stackoverflow.com

Component component) {
BufferedImage image = new BufferedImage(
 component.getWidth(),
 component.getHeight(),
component.paint( image.getGraphics() ); // alternately use .printAll(..)
return image;
Runnable r = new Runnable() {
 public void run() {
  final JFrame f = new JFrame("Test Screenshot");
       new ImageIcon(
        img.getScaledInstance(
         img.getWidth(null)/2,
         img.getHeight(null)/2,
         Image.SCALE_SMOOTH )
        )));
     try {
      ImageIO.write(
       img,
       "png",
  JMenuBar mb = new JMenuBar();
  mb.add(menu);
  f.setJMenuBar(mb);
origin: libgdx/libgdx

if (scale <= 0) throw new IllegalArgumentException("scale cannot be <= 0: " + scale);
int width = image.getWidth(), height = image.getHeight();
if (image.getType() != BufferedImage.TYPE_4BYTE_ABGR) {
  BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
  newImage.getGraphics().drawImage(image, 0, 0, null);
  image = newImage;
  BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
  newImage.getGraphics().drawImage(image, 0, 0, width, height, 1, 1, width + 1, height + 1, null);
  image = newImage;
  width = Math.max(1, Math.round(width * scale));
  height = Math.max(1, Math.round(height * scale));
  BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
  if (scale < 1) {
    newImage.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING), 0, 0, null);
  } else {
    Graphics2D g = (Graphics2D)newImage.getGraphics();
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, resampling.value);
    g.drawImage(image, 0, 0, width, height, null);
origin: stackoverflow.com

this.add(new JPanel() {
  @Override
  protected void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, null);
img = new BufferedImage( 450, 350, IMAGE_TYPE );   // here you should create a compatible BufferedImage
this.setSize(img.getWidth(), img.getHeight());
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
tiles[0] = createOneTile( new Color( 255, 255, 255 ) );
tiles[1] = createOneTile( new Color( 255,   0, 255 ) );
tiles[2] = createOneTile( new Color(   0,   0, 255 ) );
tiles[3] = createOneTile( new Color(   0, 255, 255 ) );  
  for (int j = 0; j < map.length; j++) {
    final BufferedImage tile = tiles[map[j][i]];
    for (int x = 0; x < tile.getWidth(); x++) {
      for (int y = 0; y < tile.getHeight(); y++) {
        img.setRGB( x + i * 50, y + j * 50, tile.getRGB(x,y) );
this.setVisible( true );
final BufferedImage res = new BufferedImage( 50, 50, IMAGE_TYPE );
for (int x = 0; x < res.getWidth(); x++) {
  for (int y = 0; y < res.getHeight(); y++) {
    res.setRGB( x, y, c.getRGB() - r.nextInt(150) );
origin: stackoverflow.com

canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
fillCanvas(Color.BLUE);
drawRect(Color.RED, 0, 0, width/2, height/2);
return new Dimension(canvas.getWidth(), canvas.getHeight());
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(canvas, null, null);
int color = c.getRGB();
for (int x = 0; x < canvas.getWidth(); x++) {
  for (int y = 0; y < canvas.getHeight(); y++) {
    canvas.setRGB(x, y, color);
int color = c.getRGB();
    canvas.setRGB(x, y, color);
int width = 640;
int height = 480;
JFrame frame = new JFrame("Direct draw demo");
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
origin: skylot/jadx

public void decode(InputStream in, OutputStream out) throws JadxException {
  try {
    byte[] data = IOUtils.toByteArray(in);
    BufferedImage im = ImageIO.read(new ByteArrayInputStream(data));
    int w = im.getWidth();
    int h = im.getHeight();
    BufferedImage im2 = new BufferedImage(w + 2, h + 2, BufferedImage.TYPE_INT_ARGB);
    im2.createGraphics().drawImage(im, 1, 1, w, h, null);
    NinePatch np = getNinePatch(data);
    drawHLine(im2, h + 1, np.padLeft + 1, w - np.padRight);
    drawVLine(im2, w + 1, np.padTop + 1, h - np.padBottom);
    int[] xDivs = np.xDivs;
    for (int i = 0; i < xDivs.length; i += 2) {
      drawHLine(im2, 0, xDivs[i] + 1, xDivs[i + 1]);
    }
    int[] yDivs = np.yDivs;
    for (int i = 0; i < yDivs.length; i += 2) {
      drawVLine(im2, 0, yDivs[i] + 1, yDivs[i + 1]);
    }
    ImageIO.write(im2, "png", out);
  } catch (IOException | NullPointerException ex) {
    throw new JadxException(ex.toString());
  }
}
origin: marytts/marytts

private void createShadowPicture(BufferedImage image) {
  int width = image.getWidth();
  int height = image.getHeight();
  int extra = 0;
  if (System.getProperty("os.name").equalsIgnoreCase("Windows XP")) {
    extra = 14; // Only create shadow if Windows XP (avoids double shadow in Mac OS; not tested for other OSes)
  }
  setSize(new Dimension(width + extra, height + extra));
  setLocationRelativeTo(null);
  Rectangle windowRect = getBounds();
  splash = new BufferedImage(width + extra, height + extra, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g2 = (Graphics2D) splash.getGraphics();
  try {
    Robot robot = new Robot(getGraphicsConfiguration().getDevice());
    BufferedImage capture = robot.createScreenCapture(new Rectangle(windowRect.x, windowRect.y, windowRect.width + extra,
        windowRect.height + extra));
    g2.drawImage(capture, null, 0, 0);
  } catch (AWTException e) {
  }
  BufferedImage shadow = new BufferedImage(width + extra, height + extra, BufferedImage.TYPE_INT_ARGB);
  Graphics g = shadow.getGraphics();
  g.setColor(new Color(0.0f, 0.0f, 0.0f, 0.3f));
  g.fillRoundRect(6, 6, width, height, 12, 12);
  g2.drawImage(shadow, getBlurOp(7), 0, 0);
  g2.drawImage(image, 0, 0, this);
}
java.awt.imageBufferedImage

Javadoc

The BufferedImage subclass describes an java.awt.Image with an accessible buffer of image data. All BufferedImage objects have an upper left corner coordinate of (0, 0).

Most used methods

  • <init>
  • getWidth
    Returns the width of the BufferedImage.
  • getHeight
    Returns the height of the BufferedImage.
  • createGraphics
    Creates a Graphics2D, which can be used to draw into this BufferedImage.
  • getGraphics
    This method returns a Graphics2D, but is here for backwards compatibility. #createGraphics() is more
  • getRGB
    Returns an array of integer pixels in the default RGB color model (TYPE_INT_ARGB) and default sRGB c
  • getRaster
  • getType
    Returns the image type. If it is not one of the known types, TYPE_CUSTOM is returned.
  • setRGB
    Sets an array of integer pixels in the default RGB color model (TYPE_INT_ARGB) and default sRGB colo
  • getColorModel
    Returns the ColorModel.
  • getSubimage
    Returns a subimage defined by a specified rectangular region. The returned BufferedImage shares the
  • getScaledInstance
  • getSubimage,
  • getScaledInstance,
  • flush,
  • getData,
  • getSampleModel,
  • getTransparency,
  • setData,
  • copyData,
  • isAlphaPremultiplied,
  • getAlphaRaster

Popular in Java

  • Reactive rest calls using spring rest template
  • setRequestProperty (URLConnection)
  • getResourceAsStream (ClassLoader)
  • getApplicationContext (Context)
  • Font (java.awt)
    The Font class represents fonts, which are used to render text in a visible way. A font provides the
  • Window (java.awt)
    A Window object is a top-level window with no borders and no menubar. The default layout for a windo
  • BitSet (java.util)
    The BitSet class implements abit array [http://en.wikipedia.org/wiki/Bit_array]. Each element is eit
  • ConcurrentHashMap (java.util.concurrent)
    A plug-in replacement for JDK1.5 java.util.concurrent.ConcurrentHashMap. This version is based on or
  • Filter (javax.servlet)
    A filter is an object that performs filtering tasks on either the request to a resource (a servlet o
  • Get (org.apache.hadoop.hbase.client)
    Used to perform Get operations on a single row. To get everything for a row, instantiate a Get objec
  • Top Vim plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now