List of usage examples for java.awt.image BufferedImage getGraphics
public java.awt.Graphics getGraphics()
From source file:irille.pub.verify.RandomImageServlet.java
/** * ???,,?16,/*from w w w.ja v a 2 s .co m*/ * @param num ?? * @param out ? * @throws IOException */ protected static void render(String num, boolean gif, OutputStream out) throws IOException { if (num.getBytes().length > 4) throw new IllegalArgumentException("The length of param num cannot exceed 4."); int width = 50; int height = 18; BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = (Graphics2D) bi.getGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); Font mFont = new Font("Tahoma", Font.BOLD | Font.ITALIC, 16); g.setFont(mFont); g.setColor(Color.BLACK); g.drawString(num, 2, 15); if (gif) { AnimatedGifEncoder e = new AnimatedGifEncoder(); e.setTransparent(Color.WHITE); e.start(out); e.setDelay(0); e.addFrame(bi); e.finish(); } else { ImageIO.write(bi, "png", out); } }
From source file:Main.java
/** * Pads the given {@link BufferedImage} on all sides by the given padding amount. * * @param source The source image.// w w w .j a v a 2s . c om * @param padding The amount to pad on all sides, in pixels. * @return A new, padded image, or the source image if no padding is performed. */ public static BufferedImage paddedImage(BufferedImage source, int padding) { if (padding == 0) { return source; } BufferedImage newImage = newArgbBufferedImage(source.getWidth() + padding * 2, source.getHeight() + padding * 2); Graphics2D g = (Graphics2D) newImage.getGraphics(); g.drawImage(source, padding, padding, null); return newImage; }
From source file:net.noday.core.utils.Captcha.java
public static BufferedImage gen(String text, int width, int height) throws IOException { BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); Graphics2D g = (Graphics2D) bi.getGraphics(); g.setColor(Color.GRAY);/*from ww w .ja v a 2 s . c o m*/ g.fillRect(0, 0, width, height); for (int i = 0; i < 10; i++) { g.setColor(randColor(150, 250)); g.drawOval(random.nextInt(110), random.nextInt(24), 5 + random.nextInt(10), 5 + random.nextInt(10)); Font f = new Font("Arial", Font.ITALIC, 20); g.setFont(f); g.setColor(randColor(10, 240)); g.drawString(text, 4, 24); } return bi; }
From source file:ImageUtil.java
public static BufferedImage crop(BufferedImage src, int width, int height) throws IOException { int x = src.getWidth() / 2 - width / 2; int y = src.getHeight() / 2 - height / 2; // System.out.println("---" + src.getWidth() + " - " + src.getHeight() + " - " + x + " - " + y); BufferedImage clipping = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//src.getType()); Graphics2D area = (Graphics2D) clipping.getGraphics().create(); area.drawImage(src, 0, 0, clipping.getWidth(), clipping.getHeight(), x, y, x + clipping.getWidth(), y + clipping.getHeight(), null); area.dispose();//from ww w. j a v a2 s . com return clipping; }
From source file:net.duckling.ddl.util.ImageUtils.java
/** * ???/*from ww w . j a va 2 s.c om*/ * @param tmpFilePath ? * @return */ public static boolean scare(String tmpFilePath) { try { BufferedImage src = ImageIO.read(new File(tmpFilePath)); // int width = src.getWidth(); int height = src.getHeight(); if (width > DEFAULT_WIDTH) { height = (DEFAULT_WIDTH * height) / width; width = DEFAULT_WIDTH; } Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT); BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.drawImage(image, 0, 0, null); // ?? g.dispose(); File resultFile = new File(tmpFilePath); ImageIO.write(tag, "JPEG", resultFile);// ? return true; } catch (IOException e) { LOG.error(e); } return false; }
From source file:com.chinarewards.gwt.license.util.ImageDisposeUtil.java
/** * @param imgsrc //w ww. j av a 2 s . co m * @param imgdist ? * @param widthdist * @param heightdist * @param int benchmark :0,12 * */ public static void reduceImg(String imgsrc, String imgdist, int widthdist, int heightdist, int benchmark) { try { // System.out.println("*******widthdist********:"+widthdist); // System.out.println("*******heightdist********:"+heightdist); // System.out.println("*******benchmark********:"+benchmark); File srcfile = new File(imgsrc); if (!srcfile.exists()) { return; } Image src = javax.imageio.ImageIO.read(srcfile); int width = src.getWidth(null); int height = src.getHeight(null); if (width <= widthdist && height <= heightdist) { // SysUtil.cpoyFile(imgsrc, imgdist); FileUtils.copyFile(new File(imgsrc), new File(imgdist)); return; } // float wh = (float) width / (float) height; if (benchmark == 0) { if (wh > 1) { float tmp_heigth = (float) widthdist / wh; BufferedImage tag = new BufferedImage(widthdist, (int) tmp_heigth, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, widthdist, (int) tmp_heigth, null); FileOutputStream out = new FileOutputStream(imgdist); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } else { float tmp_width = (float) heightdist * wh; BufferedImage tag = new BufferedImage((int) tmp_width, heightdist, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, (int) tmp_width, heightdist, null); FileOutputStream out = new FileOutputStream(imgdist); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } } if (benchmark == 1) { float tmp_heigth = (float) widthdist / wh; BufferedImage tag = new BufferedImage(widthdist, (int) tmp_heigth, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, widthdist, (int) tmp_heigth, null); FileOutputStream out = new FileOutputStream(imgdist); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } if (benchmark == 2) { float tmp_width = (float) heightdist * wh; BufferedImage tag = new BufferedImage((int) tmp_width, heightdist, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, (int) tmp_width, heightdist, null); FileOutputStream out = new FileOutputStream(imgdist); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } } catch (IOException ex) { logger.error(ex); } }
From source file:Utils.java
public static BufferedImage createGradientImage(int width, int height, Color gradient1, Color gradient2) { BufferedImage gradientImage = createCompatibleImage(width, height); GradientPaint gradient = new GradientPaint(0, 0, gradient1, 0, height, gradient2, false); Graphics2D g2 = (Graphics2D) gradientImage.getGraphics(); g2.setPaint(gradient);//from ww w. j a v a 2 s . c om g2.fillRect(0, 0, width, height); g2.dispose(); return gradientImage; }
From source file:bayesGame.ui.painter.OrNodePainter.java
public static Image paintPercentage(Color gridColor, Color falseColor, int size, int squaresize, BayesNode node, Fraction parentNode1Probability, Fraction parentNode2Probability) { BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); NodePainter.graphicBackgroundPainter(g, 0, 0, size, size); // get non-zero truth table entries from the node List<Map<Object, Boolean>> nonZeroEntries = node.getNonZeroProbabilities(); // get the identities of its parents by taking the first map and querying it // for KeySet, subtracting the object representing the node itself Set<Object> nodeParents = nonZeroEntries.get(0).keySet(); nodeParents.remove(node.type);//ww w . java 2 s .c o m if (nodeParents.size() > 2) { throw new IllegalArgumentException("OR node with more than 2 parents not yet implemented"); } Object[] nodeParentsArray = nodeParents.toArray(); Object parent1 = nodeParentsArray[0]; Object parent2 = nodeParentsArray[1]; // for each map, check the truth table entry it corresponds to and color // those appropriately boolean p1true_p2true = false; boolean p1true_p2false = false; boolean p1false_p2true = false; boolean p1false_p2false = false; for (Map<Object, Boolean> map : nonZeroEntries) { Boolean parent1truth = map.get(parent1); Boolean parent2truth = map.get(parent2); if (parent1truth && parent2truth) { p1true_p2true = true; } else if (parent1truth && !parent2truth) { p1true_p2false = true; } else if (!parent1truth && parent2truth) { p1false_p2true = true; } else if (!parent1truth && !parent2truth) { p1false_p2false = true; } } int XSize = parentNode1Probability.multiply(size).intValue(); int X_Size = size - XSize; int YSize = parentNode2Probability.multiply(size).intValue(); int Y_Size = size - YSize; if (p1true_p2true) { NodePainter.squarePainter(g, 0, 0, XSize, YSize, gridColor, Color.BLACK); } else { NodePainter.squarePainter(g, 0, 0, XSize, YSize, NodePainter.RECTANGLE_BOX_BACKGROUND_COLOR, Color.BLACK); } NodePainter.squarePainter(g, XSize, 0, X_Size, YSize, gridColor, Color.BLACK); NodePainter.squarePainter(g, 0, YSize, XSize, Y_Size, gridColor, Color.BLACK); if (p1false_p2false) { NodePainter.squarePainter(g, XSize, YSize, X_Size, Y_Size, falseColor, Color.BLACK); } else { NodePainter.squarePainter(g, XSize, YSize, X_Size, Y_Size, NodePainter.RECTANGLE_BOX_BACKGROUND_COLOR, Color.BLACK); } return img; }
From source file:FontAlgo.java
private static TextualChar getTextualChar(char a_char) throws Throwable { BufferedImage bImg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics g = bImg.getGraphics(); g.setColor(Color.green);/*w w w . j a va 2s . co m*/ g.fillRect(0, 0, WIDTH, HEIGHT); g.setFont(appliedFont); g.setColor(Color.black); g.drawString(new String(new char[] { a_char }), 10, g.getFontMetrics().getHeight()); PixelGrabber p = new PixelGrabber(bImg, 0, 0, WIDTH, HEIGHT, true); if (p.grabPixels()) { char[][] pattern = new char[WIDTH][HEIGHT]; int baseColourPixel = 0, contrastColourPixel = 0, x1 = 0, x2 = 0, y1 = 0, y2 = 0; int[] pixels = (int[]) p.getPixels(); baseColourPixel = pixels[0]; // System.out.println("base: " + base); int xCounter = 0, yCounter = 0; for (int iPixel : pixels) { // System.out.println(iX + " - " + iY); if (isReverse) { pattern[xCounter][yCounter] = iPixel == baseColourPixel ? CHAR_TO_PATTERN : ' '; } else { pattern[xCounter][yCounter] = iPixel != baseColourPixel ? CHAR_TO_PATTERN : ' '; } yCounter++; if (yCounter > 49) { xCounter++; yCounter = 0; } if (contrastColourPixel == 0 && iPixel != baseColourPixel) { contrastColourPixel = iPixel; x1 = xCounter - 2; y1 = yCounter - 3; y2 = yCounter + 3; } if (contrastColourPixel == iPixel) { x2 = xCounter + 3; if (y1 > (yCounter - 3)) { y1 = yCounter - 3; } if (y2 < (yCounter + 3)) { y2 = yCounter + 3; } } } return new TextualChar(x1, x2, y1, y2, pattern); } return null; }
From source file:bayesGame.ui.painter.AndNodePainter.java
public static Image paintPercentage(Color gridColor, Color falseColor, int size, int squaresize, BayesNode node, Fraction parentNode1Probability, Fraction parentNode2Probability) { BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); NodePainter.graphicBackgroundPainter(g, 0, 0, size, size); // get non-zero truth table entries from the node List<Map<Object, Boolean>> nonZeroEntries = node.getNonZeroProbabilities(); // get the identities of its parents by taking the first map and querying it // for KeySet, subtracting the object representing the node itself Set<Object> nodeParents = nonZeroEntries.get(0).keySet(); nodeParents.remove(node.type);//ww w .jav a2s . c om if (nodeParents.size() > 2) { throw new IllegalArgumentException("AND node with more than 2 parents not yet implemented"); } Object[] nodeParentsArray = nodeParents.toArray(); Object parent1 = nodeParentsArray[0]; Object parent2 = nodeParentsArray[1]; // for each map, check the truth table entry it corresponds to and color // those appropriately boolean p1true_p2true = false; boolean p1true_p2false = false; boolean p1false_p2true = false; boolean p1false_p2false = false; for (Map<Object, Boolean> map : nonZeroEntries) { Boolean parent1truth = map.get(parent1); Boolean parent2truth = map.get(parent2); if (parent1truth && parent2truth) { p1true_p2true = true; } else if (parent1truth && !parent2truth) { p1true_p2false = true; } else if (!parent1truth && parent2truth) { p1false_p2true = true; } else if (!parent1truth && !parent2truth) { p1false_p2false = true; } } Color whiteColor = Color.WHITE; int XSize = parentNode1Probability.multiply(size).intValue(); int X_Size = size - XSize; int YSize = parentNode2Probability.multiply(size).intValue(); int Y_Size = size - YSize; if (p1true_p2true) { NodePainter.squarePainter(g, 0, 0, XSize, YSize, gridColor, Color.BLACK); } else { NodePainter.squarePainter(g, 0, 0, XSize, YSize, NodePainter.RECTANGLE_BOX_BACKGROUND_COLOR, Color.BLACK); } NodePainter.squarePainter(g, XSize, 0, X_Size, YSize, falseColor, Color.BLACK); NodePainter.squarePainter(g, 0, YSize, XSize, Y_Size, falseColor, Color.BLACK); if (p1false_p2false) { NodePainter.squarePainter(g, XSize, YSize, X_Size, Y_Size, falseColor, Color.BLACK); } else { NodePainter.squarePainter(g, XSize, YSize, X_Size, Y_Size, NodePainter.RECTANGLE_BOX_BACKGROUND_COLOR, Color.BLACK); } return img; }