Example usage for java.awt.image BufferedImage getSource

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

Introduction

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

Prototype

public ImageProducer getSource() 

Source Link

Document

Returns the object that produces the pixels for the image.

Usage

From source file:edu.stanford.epad.epadws.handlers.dicom.DSOUtil.java

private static Image makeColorTransparent(BufferedImage im, final Color color) {
    ImageFilter filter = new RGBImageFilter() {
        public int markerRGB = color.getRGB() | 0xFF000000;

        @Override//from ww  w.j  a  v  a2  s  .  c o m
        public final int filterRGB(int x, int y, int rgb) {
            if ((rgb | 0xFF000000) == markerRGB) {
                return 0x00FFFFFF & rgb;
            } else {
                return rgb;
            }
        }
    };

    ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}

From source file:net.sf.webphotos.tools.Thumbnail.java

private static Image estampar(Image im) {
    try {/*from   w w  w  . jav  a2 s .  c om*/
        Image temp = new ImageIcon(im).getImage();

        BufferedImage buf = new BufferedImage(temp.getWidth(null), temp.getHeight(null),
                BufferedImage.TYPE_INT_RGB);

        Graphics2D g2 = (Graphics2D) buf.getGraphics();

        g2.drawImage(temp, 0, 0, null);
        g2.setBackground(Color.BLUE);

        Dimension dimensaoFoto = new Dimension(im.getWidth(null), im.getHeight(null));

        // aplicar texto
        if (texto != null) {
            Util.out.println("aplicando texto " + texto);

            Font fonte = new Font(txFamilia, txEstilo, txTamanho);
            g2.setFont(fonte);
            FontMetrics fm = g2.getFontMetrics(fonte);
            Dimension dimensaoTexto = new Dimension(fm.stringWidth(texto), fm.getHeight());
            Point posTexto = calculaPosicao(dimensaoFoto, dimensaoTexto, txMargem, txPosicao);

            g2.setPaint(txCorFundo);
            g2.drawString(texto, (int) posTexto.getX() + 1, (int) posTexto.getY() + 1 + fm.getHeight());
            g2.setPaint(txCorFrente);
            g2.drawString(texto, (int) posTexto.getX(), (int) posTexto.getY() + fm.getHeight());
        }

        // aplicar marca dagua
        if (marcadagua != null) {
            Image marca = new ImageIcon(marcadagua).getImage();
            int rule = AlphaComposite.SRC_OVER;
            float alpha = (float) mdTransparencia / 100;
            Point pos = calculaPosicao(dimensaoFoto, new Dimension(marca.getWidth(null), marca.getHeight(null)),
                    mdMargem, mdPosicao);

            g2.setComposite(AlphaComposite.getInstance(rule, alpha));
            g2.drawImage(marca, (int) pos.getX(), (int) pos.getY(), null);
        }

        g2.dispose();
        //return (Image) buf;
        return Toolkit.getDefaultToolkit().createImage(buf.getSource());
    } catch (Exception e) {
        Util.err.println("[Thumbnail.estampar]/ERRO: Inesperado - " + e.getMessage());
        e.printStackTrace(Util.err);
        return im;
    }
}

From source file:edu.stanford.epad.epadws.handlers.dicom.DSOUtil.java

private static Image makeColorOpaque(BufferedImage im, final Color color) {
    nonBlank.set(false);//from w w  w.  ja  v a 2  s .co  m
    ImageFilter filter = new RGBImageFilter() {
        public int markerRGB = color.getRGB() | 0xFF000000;

        @Override
        public final int filterRGB(int x, int y, int rgb) {
            if ((rgb & 0x00FFFFFF) != 0) {
                nonBlank.set(true);
            }
            if ((rgb | 0xFF000000) == markerRGB) {
                //               nonBlank.set(true);
                return 0xFF000000 | rgb;
            } else {
                return rgb;
            }
        }
    };

    ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}

From source file:nz.co.fortytwo.freeboard.server.util.ChartProcessor.java

/**
 * Use Imagemajick convert to make white transparent, so we can overlay charts
 * @param dir//  w ww . j  av a  2 s . co  m
 * @throws IOException
 * @throws InterruptedException
 */
private void processPng(File dir) throws IOException, InterruptedException {
    //   File tmpPng = new File(dir.getAbsoluteFile()+".new");
    if (manager) {
        System.out.print("      Convert " + dir.getName() + "\n");
    }
    if (logger.isDebugEnabled())
        logger.debug("      Convert " + dir.getName());

    BufferedImage img = ImageIO.read(dir);
    ImageProducer ip = new FilteredImageSource(img.getSource(), filter);
    Image transparentImage = Toolkit.getDefaultToolkit().createImage(ip);
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = dest.createGraphics();
    g2.drawImage(transparentImage, 0, 0, null);
    g2.dispose();
    ImageIO.write(dest, "PNG", dir);

}

From source file:org.polymap.rhei.batik.engine.svg.Svg2Png.java

public static BufferedImage makeColorTransparent(BufferedImage im, final Color color) {
    ImageFilter filter = new RGBImageFilter() {

        private int shift = 0xFF000000;

        public int rgbToMakeTransparent = color.getRGB() | shift;

        public final int filterRGB(int x, int y, int rgb) {
            if ((rgb | shift) == rgbToMakeTransparent) {
                return 0x00FFFFFF & rgb;
            }/* w  w w  .j  av  a  2s. c  o  m*/
            return rgb;
        }
    };
    Image newImage = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(im.getSource(), filter));
    BufferedImage bufferedImage = new BufferedImage(newImage.getWidth(null), newImage.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bufferedImage.createGraphics();
    g2.drawImage(newImage, 0, 0, null);
    g2.dispose();
    return bufferedImage;
}

From source file:paquete.HollywoodUI.java

private void btn_buscar_fotoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_buscar_fotoMouseClicked
    JFileChooser jfc = new JFileChooser();
    jfc.setCurrentDirectory(new File("./"));
    FileFilter filtro = new FileNameExtensionFilter("Jpg, Png", "jpg", "png");
    jfc.setFileFilter(filtro);/*from   ww  w .  j a  v a2 s.  co  m*/
    File archivo = null;
    try {
        int op = jfc.showOpenDialog(this.add_actor);
        if (op == JFileChooser.APPROVE_OPTION) {
            archivo = jfc.getSelectedFile();
        }
        BufferedImage imagen = ImageIO.read(archivo);
        Image img;
        img = Toolkit.getDefaultToolkit().createImage(imagen.getSource())
                .getScaledInstance(label_profile.getWidth(), label_profile.getHeight(), 0);
        label_profile.setIcon(new ImageIcon(img));
        this.temp.setFoto_actor(resize(imagen, 54, 54));
    } catch (Exception e) {
        JOptionPane.showMessageDialog(add_actor, "Error al intentar abrir archivo", "ERROR",
                JOptionPane.ERROR_MESSAGE);
    }
}

From source file:tv.dyndns.kishibe.qmaclone.server.IconUploadServletStub.java

private void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    logger.log(Level.INFO, request.toString());

    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setSizeMax(64L * 1024L);//from  w ww  .  j  a  v a2  s  .  c o  m
    List<FileItem> items = null;

    try {
        List<FileItem> temp = upload.parseRequest(request);
        items = temp;
    } catch (FileUploadException e) {
        writeResponse(response, Constant.ICON_UPLOAD_RESPONSE_FAILED_TO_PARSE_REQUEST);
        return;
    }

    String userCode = null;
    BufferedImage inputImage = null;
    long originalFileSize = 0;

    for (FileItem item : items) {
        if (item.isFormField()) {
            String key = item.getFieldName();
            String value = item.getString();

            if (key.equals(Constant.FORM_NAME_USER_CODE)) {
                userCode = value;
            }

            continue;
        }

        // ??
        originalFileSize = item.getSize();
        try (InputStream inputStream = item.getInputStream()) {
            inputImage = ImageIO.read(inputStream);
        } catch (IOException e) {
            writeResponse(response, Constant.ICON_UPLOAD_RESPONSE_FAILED_TO_DETECT_IMAGE_FILE_TYPE);
            return;
        }

        if (inputImage == null) {
            writeResponse(response, Constant.ICON_UPLOAD_RESPONSE_FAILED_TO_DETECT_IMAGE_FILE_TYPE);
            return;
        }
    }

    if (userCode == null || inputImage == null) {
        writeResponse(response, Constant.ICON_UPLOAD_RESPONSE_REQUEST_FORMAT_ERROR);
        return;
    }

    String extension = "jpg";
    String fileTitle = Utility.createFileName();
    String fileName = fileTitle + "." + extension;

    // 
    // TODO(nodchip): ?
    int size = Constant.ICON_SIZE * 2;
    ImageFilter imageFilter = new AreaAveragingScaleFilter(size, size);
    Image middleImage = new Canvas().createImage(new FilteredImageSource(inputImage.getSource(), imageFilter));
    BufferedImage outputImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    outputImage.createGraphics().drawImage(middleImage, 0, 0, size, size, null);

    // ???
    new File(Constant.ICON_FOLDER_PATH).mkdirs();
    File outputFile = new File(Constant.ICON_FOLDER_PATH, fileName);
    ImageIO.write(outputImage, "jpg", outputFile);

    logger.log(Level.INFO, String.format("%d bytes -> %d bytes (%s)", originalFileSize, outputFile.length(),
            outputFile.getPath()));

    // ???
    int userId = Integer.parseInt(userCode);
    PacketUserData userData;
    try {
        userData = database.getUserData(userId);
    } catch (DatabaseException e) {
        logger.log(Level.WARNING, "????????", e);
        throw new ServletException(e);
    }
    File oldFile = new File(Constant.ICON_FOLDER_PATH, userData.imageFileName);
    if (oldFile.isFile() && !oldFile.getName().equals(Constant.ICON_NO_IMAGE)) {
        oldFile.delete();
    }
    userData.imageFileName = fileName;
    try {
        database.setUserData(userData);
    } catch (DatabaseException e) {
        logger.log(Level.WARNING, "???????", e);
        throw new ServletException(e);
    }

    // ????
    writeResponse(response, Constant.ICON_UPLOAD_RESPONSE_OK);
}