Example usage for java.awt.image BufferedImage BufferedImage

List of usage examples for java.awt.image BufferedImage BufferedImage

Introduction

In this page you can find the example usage for java.awt.image BufferedImage BufferedImage.

Prototype

public BufferedImage(int width, int height, int imageType) 

Source Link

Document

Constructs a BufferedImage of one of the predefined image types.

Usage

From source file:org.web4thejob.web.util.MediaUtil.java

public static BufferedImage createThumbnail(byte[] bytes) {
    Image image = getImage(bytes);
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(), image.getHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bufferedImage.createGraphics();
    g.drawImage(image.toImageIcon().getImage(), 0, 0, null);
    g.dispose();/*from w w w  . j av  a 2s . com*/
    return createThumbnail(bufferedImage);
}

From source file:ml.hsv.java

public static void HSV2File(hsv hsvImage[][], int width, int height) throws IOException //store img output as hsv2file.png
{
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    WritableRaster raster = image.getRaster();
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int RGB = Color.HSBtoRGB(hsvImage[i][j].h, hsvImage[i][j].s, hsvImage[i][j].v);
            Color c = new Color(RGB);
            int temp[] = { c.getRed(), c.getGreen(), c.getBlue() };
            raster.setPixel(j, i, temp);
        }/* w  w  w .j  a  v a 2s .c o m*/
    }
    ImageIO.write(image, "PNG",
            new File("/home/shinchan/FinalProject/PaperImplementation/Eclipse/ML/output/hsv2file.png"));
}

From source file:com.yanbang.portal.controller.PortalController.java

/**
 * ???/*from  w  w w .j a  va 2s . c o m*/
 * 
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(params = "action=handleRnd")
public void handleRnd(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setHeader("Cache-Control", "no-store");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0L);
    response.setContentType("image/jpeg");
    BufferedImage image = new BufferedImage(65, 25, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics();
    g.setColor(Color.GRAY);
    g.fillRect(0, 0, 65, 25);
    g.setColor(Color.yellow);
    Font font = new Font("", Font.BOLD, 20);
    g.setFont(font);
    Random r = new Random();
    String rnd = "";
    int ir = r.nextInt(10);
    rnd = rnd + "" + ir;
    g.drawString("" + ir, 5, 18);
    g.setColor(Color.red);
    ir = r.nextInt(10);
    rnd = rnd + "" + ir;
    g.drawString("" + ir, 20, 18);
    g.setColor(Color.blue);
    ir = r.nextInt(10);
    rnd = rnd + "" + ir;
    g.drawString("" + ir, 35, 18);
    g.setColor(Color.green);
    ir = r.nextInt(10);
    rnd = rnd + "" + ir;
    g.drawString("" + ir, 50, 18);
    request.getSession().setAttribute("RND", rnd);
    ServletOutputStream out = response.getOutputStream();
    out.write(ImageUtil.imageToBytes(image, "gif"));
    out.flush();
    out.close();
}

From source file:net.pkhsolutions.pecsapp.control.PictureTransformer.java

/**
 * TODO Document me//  w  w  w.j a  v a2  s .co m
 *
 * @param image
 * @param maxHeightInPx
 * @param maxWidthInPx
 * @return
 */
@NotNull
public BufferedImage scaleIfNecessary(@NotNull BufferedImage image, int maxHeightInPx, int maxWidthInPx) {
    int w = image.getWidth();
    int h = image.getHeight();

    int newW;
    int newH;

    if (w <= maxWidthInPx && h <= maxHeightInPx) {
        return image;
    } else if (w > h) {
        newW = maxWidthInPx;
        newH = newW * h / w;
    } else {
        newH = maxHeightInPx;
        newW = newH * w / h;
    }
    LOGGER.info("Original size was w{} x h{}, new size is w{} x h{}", w, h, newW, newH);
    Image tmp = image.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
    BufferedImage img = new BufferedImage(newW, newH, image.getType());
    Graphics2D g2d = img.createGraphics();
    g2d.drawImage(tmp, 0, 0, null);
    g2d.dispose();
    return img;
}

From source file:be.fedict.eid.idp.sp.PhotoServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    LOG.debug("doGet");
    response.setContentType("image/jpg");
    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=-1"); // http 1.1
    response.setHeader("Pragma", "no-cache, no-store"); // http 1.0
    response.setDateHeader("Expires", -1);
    ServletOutputStream out = response.getOutputStream();
    HttpSession session = request.getSession();

    byte[] photoData = (byte[]) session.getAttribute(PHOTO_SESSION_ATTRIBUTE);

    if (null != photoData) {
        BufferedImage photo = ImageIO.read(new ByteArrayInputStream(photoData));
        if (null == photo) {
            /*/*from   www  .  j ava 2s . com*/
             * In this case we render a photo containing some error message.
             */
            photo = new BufferedImage(140, 200, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = (Graphics2D) photo.getGraphics();
            RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            graphics.setRenderingHints(renderingHints);
            graphics.setColor(Color.WHITE);
            graphics.fillRect(1, 1, 140 - 1 - 1, 200 - 1 - 1);
            graphics.setColor(Color.RED);
            graphics.setFont(new Font("Dialog", Font.BOLD, 20));
            graphics.drawString("Photo Error", 0, 200 / 2);
            graphics.dispose();
            ImageIO.write(photo, "jpg", out);
        } else {
            out.write(photoData);
        }
    }
    out.close();
}

From source file:gui.images.CodebookVectorProfilePanel.java

/**
 * Generate a BufferedImage that would correspond to a JPanel. Images are
 * faster to show than interactive components if many components need to be
 * presented./*from  w ww  .ja v a2s .  co  m*/
 *
 * @param panel JPanel object.
 * @return BufferedImage of how the content in the provided panel would be
 * rendered.
 */
public BufferedImage createImage(JPanel panel) {
    int width = panel.getSize().width;
    int height = panel.getSize().height;
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    panel.paint(g);
    return bi;
}

From source file:c.depthchart.ViewerPanel.java

public ViewerPanel() {
    setBackground(Color.WHITE);/*from  w  w  w . java  2 s  .  c  om*/

    df = new DecimalFormat("0.#"); // 1 dp
    msgFont = new Font("SansSerif", Font.BOLD, 18);

    initChart();
    configOpenNI();

    histogram = new float[MAX_DEPTH_SIZE];

    imWidth = depthMD.getFullXRes();
    imHeight = depthMD.getFullYRes();
    System.out.println("Image dimensions (" + imWidth + ", " + imHeight + ")");

    // create empty image object of correct size and type
    imgbytes = new byte[imWidth * imHeight];
    image = new BufferedImage(imWidth, imHeight, BufferedImage.TYPE_BYTE_GRAY);

    new Thread(this).start(); // start updating the panel's image
}

From source file:com.fun.util.TesseractUtil.java

/**
 * /*from   w ww . ja v  a2s  .  c om*/
 *
 * @param imageFile
 * @param times
 * @param targetFile
 * @throws IOException
 */
private static void scaled(File imageFile, int times, File targetFile) throws IOException {
    BufferedImage image = ImageIO.read(imageFile);
    int targetWidth = image.getWidth() * times;
    int targetHeight = image.getHeight() * times;
    int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage tmp = new BufferedImage(targetWidth, targetHeight, type);
    Graphics2D g2 = tmp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2.drawImage(image, 0, 0, targetWidth, targetHeight, null);
    g2.dispose();
    ImageIO.write(tmp, "png", targetFile);
}

From source file:com.tdclighthouse.prototype.servlets.JLatexServlet.java

private synchronized BufferedImage generateImage(String latex) {
    TeXFormula formula = new TeXFormula(latex);
    TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);
    icon.setInsets(new Insets(5, 5, 5, 5));

    BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    g2.setColor(Color.white);//from  ww w.  java2  s.c o  m
    g2.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
    JLabel jl = new JLabel();
    jl.setForeground(new Color(0, 0, 0));
    icon.paintIcon(jl, g2, 0, 0);
    return image;
}

From source file:com.github.andreax79.meca.Main.java

public static Stats drawRule(int ruleNumber, int size, Boundaries boundaries, UpdatePattern updatePattern,
        int steps, double alpha, String pattern, Output output, ColorScheme colorScheme) throws IOException {
    Rule rule = new Rule(ruleNumber);
    Row row = new Row(size, rule, boundaries, updatePattern, pattern, alpha); // e.g. 00010011011111
    Stats stats = new Stats();

    FileOutputStream finalImage = null;
    Graphics2D g = null;/* w  w  w .  j  av  a 2  s  .  co m*/
    BufferedImage img = null;

    if (output != Output.noOutput) {
        String fileName = "rule" + ruleNumber;
        // pattern
        if (pattern != null)
            fileName += pattern;
        // alpha
        if (alpha > 0)
            fileName += String.format("_a%02d", (int) (alpha * 100));
        // updatePattern
        if (updatePattern != UpdatePattern.synchronous)
            fileName += "-" + updatePattern;
        fileName += ".jpeg";

        File file = new File(fileName);
        finalImage = new FileOutputStream(file);

        int width = (int) (cellSize * (size + 1) * (output == Output.all ? 1.25 : 1));
        int height = cellSize * (steps + 1);
        img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        g = img.createGraphics();
        g.setBackground(Color.white);
        g.clearRect(0, 0, width, height);
        g.setColor(Color.black);
    }

    int startMeansFromStep = 50;
    List<Double> densities = new LinkedList<Double>();
    double totalDensities = 0;

    double prevValue = 0;
    double prevDelta = 0;
    double prevOnes = 0;
    double prevOnesDelta = 0;

    for (int t = 0; t < steps; t++) {
        if (t >= startMeansFromStep) {
            double density = row.getDensity();
            densities.add(density);
            totalDensities += density;
        }
        // System.out.println(String.format("%4d", t) + " " + row.toString() + " ones=" + row.getOnes());
        if (output != Output.noOutput) {
            for (int j = 0; j < row.getSize(); j++) {

                switch (colorScheme) {
                case noColor:
                    if (row.getCell(j).getState()) {
                        g.setColor(Color.black);
                        g.fillRect(j * cellSize, t * cellSize, cellSize, cellSize);
                    }
                    break;
                case omegaColor:
                    g.setColor(row.getCell(j).getOmegaColor());
                    g.fillRect(j * cellSize, t * cellSize, cellSize, cellSize);
                    break;
                case activationColor:
                    if (row.getCell(j).getState()) {
                        g.setColor(row.getCell(j).getColor());
                        g.fillRect(j * cellSize, t * cellSize, cellSize, cellSize);
                    }
                    break;
                }
            }

            if (output == Output.all) {
                double value = row.getValue();
                double delta = Math.abs(value - prevValue);
                double ones = row.getOnes();
                double onesDelta = Math.abs(ones - prevOnes);
                if (t > 0) {
                    g.setColor(Color.red);
                    g.drawLine((int) (prevValue * cellSize / 4.0) + cellSize * (size + 1),
                            (int) ((t - 1) * cellSize), (int) (value * cellSize / 4.0) + cellSize * (size + 1),
                            (int) (t * cellSize));
                    g.setColor(Color.blue);
                    g.drawLine((int) (prevOnes * cellSize / 4.0) + cellSize * (size + 1),
                            (int) ((t - 1) * cellSize), (int) (ones * cellSize / 4.0) + cellSize * (size + 1),
                            (int) (t * cellSize));
                    if (t > 1) {
                        g.setColor(Color.orange);
                        g.drawLine((int) (prevDelta * cellSize / 4.0) + cellSize * (size + 1),
                                (int) ((t - 1) * cellSize),
                                (int) (delta * cellSize / 4.0) + cellSize * (size + 1), (int) (t * cellSize));
                        g.setColor(Color.cyan);
                        g.drawLine((int) (prevOnesDelta * cellSize / 4.0) + cellSize * (size + 1),
                                (int) ((t - 1) * cellSize),
                                (int) (onesDelta * cellSize / 4.0) + cellSize * (size + 1),
                                (int) (t * cellSize));
                    }
                }
                prevValue = value;
                prevDelta = delta;
                prevOnes = ones;
                prevOnesDelta = onesDelta;
            }
        }

        row = new Row(row);
    }

    double means = totalDensities / densities.size();
    double var = 0;
    for (double density : densities)
        var += Math.pow(density - means, 2);
    var = var / densities.size();
    System.out.println("Rule: " + ruleNumber + " Boundaties: " + boundaries + " UpdatePattern: " + updatePattern
            + " Alpha: " + String.format("%.3f", alpha) + " Means: " + String.format("%.6f", means)
            + " Variance: " + String.format("%.6f", var));
    stats.setMeans(means);
    stats.setVariance(var);

    if (output != Output.noOutput) {
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(finalImage);
        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(img);
        param.setQuality(1.0f, true);
        encoder.encode(img, param);
        finalImage.flush();
        finalImage.close();
    }

    return stats;
}