Example usage for java.awt.image BufferedImageOp filter

List of usage examples for java.awt.image BufferedImageOp filter

Introduction

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

Prototype

public BufferedImage filter(BufferedImage src, BufferedImage dest);

Source Link

Document

Performs a single-input/single-output operation on a BufferedImage .

Usage

From source file:Java2DExample.java

public BufferedImage processImage(BufferedImage image) {
    byte[] invertArray = new byte[256];

    for (int counter = 0; counter < 256; counter++)
        invertArray[counter] = (byte) (255 - counter);

    BufferedImageOp invertFilter = new LookupOp(new ByteLookupTable(0, invertArray), null);
    return invertFilter.filter(image, null);

}

From source file:ImageProcessingTest.java

/**
 * Apply a filter and repaint./* w ww  .jav a 2s.  c  o m*/
 * @param op the image operation to apply
 */
private void filter(BufferedImageOp op) {
    if (image == null)
        return;
    image = op.filter(image, null);
    repaint();
}

From source file:Java2DExample.java

public BufferedImage processImage(BufferedImage image) {
    float[] blurMatrix = { 1.0f / 9.0f, 1.0f / 9.0f, 1.0f / 9.0f, 1.0f / 9.0f, 1.0f / 9.0f, 1.0f / 9.0f,
            1.0f / 9.0f, 1.0f / 9.0f, 1.0f / 9.0f };
    BufferedImageOp blurFilter = new ConvolveOp(new Kernel(3, 3, blurMatrix), ConvolveOp.EDGE_NO_OP, null);
    return blurFilter.filter(image, null);
}

From source file:Java2DExample.java

public BufferedImage processImage(BufferedImage image) {
    float[] sharpenMatrix = { 0.0f, -1.0f, 0.0f, -1.0f, 5.0f, -1.0f, 0.0f, -1.0f, 0.0f };
    BufferedImageOp sharpenFilter = new ConvolveOp(new Kernel(3, 3, sharpenMatrix), ConvolveOp.EDGE_NO_OP,
            null);// w ww .  j a v a  2  s  . co m
    return sharpenFilter.filter(image, null);
}

From source file:edu.emory.library.tast.images.ThumbnailServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    // location of images
    String baseUrl = AppConfig.getConfiguration().getString(AppConfig.IMAGES_URL);
    baseUrl = StringUtils.trimEnd(baseUrl, '/');

    // image name and size
    String imageFileName = request.getParameter("i");
    int thumbnailWidth = Integer.parseInt(request.getParameter("w"));
    int thumbnailHeight = Integer.parseInt(request.getParameter("h"));

    // image dir/*from  ww w  .  j  ava 2 s.  c om*/
    String imagesDir = AppConfig.getConfiguration().getString(AppConfig.IMAGES_DIRECTORY);

    // create the thumbnail name
    String thumbnailFileName = FilenameUtils.getBaseName(imageFileName) + "-" + thumbnailWidth + "x"
            + thumbnailHeight + ".png";

    // does it exist?
    File thumbnailFile = new File(imagesDir, thumbnailFileName);
    if (thumbnailFile.exists()) {
        response.sendRedirect(baseUrl + "/" + thumbnailFileName);
        return;
    }

    // read the image
    File imageFile = new File(imagesDir, imageFileName);
    BufferedImage image = ImageIO.read(imageFile);
    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();
    BufferedImage imageCopy = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
    imageCopy.getGraphics().drawImage(image, 0, 0, imageWidth, imageHeight, 0, 0, imageWidth, imageHeight,
            null);

    // height is calculated automatically
    if (thumbnailHeight == 0)
        thumbnailHeight = (int) ((double) thumbnailWidth / (double) imageWidth * (double) imageHeight);

    // width is calculated automatically
    if (thumbnailWidth == 0)
        thumbnailWidth = (int) ((double) thumbnailHeight / (double) imageHeight * (double) imageWidth);

    // create an empty thumbnail
    BufferedImage thumbnail = new BufferedImage(thumbnailWidth, thumbnailHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D gr = thumbnail.createGraphics();
    gr.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    gr.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    // determine the piece of the image which we want to clip
    int clipX1, clipX2, clipY1, clipY2;
    if (imageWidth * thumbnailHeight > thumbnailWidth * imageHeight) {

        int clipWidth = (int) Math
                .round(((double) thumbnailWidth / (double) thumbnailHeight) * (double) imageHeight);
        int imgCenterX = imageWidth / 2;
        clipX1 = imgCenterX - clipWidth / 2;
        clipX2 = imgCenterX + clipWidth / 2;
        clipY1 = 0;
        clipY2 = imageHeight;
    } else {
        int clipHeight = (int) Math
                .round(((double) thumbnailHeight / (double) thumbnailWidth) * (double) imageWidth);
        int imgCenterY = imageHeight / 2;
        clipX1 = 0;
        clipX2 = imageWidth;
        clipY1 = imgCenterY - clipHeight / 2;
        clipY2 = imgCenterY + clipHeight / 2;
    }

    // we filter the image first to get better results when shrinking
    if (2 * thumbnailWidth < clipX2 - clipX1 || 2 * thumbnailHeight < clipY2 - clipY1) {

        int kernelDimX = (clipX2 - clipX1) / (thumbnailWidth);
        int kernelDimY = (clipY2 - clipY1) / (thumbnailHeight);

        if (kernelDimX % 2 == 0)
            kernelDimX++;
        if (kernelDimY % 2 == 0)
            kernelDimY++;

        if (kernelDimX < kernelDimY)
            kernelDimX = kernelDimY;
        if (kernelDimY < kernelDimX)
            kernelDimY = kernelDimX;

        float[] blurKernel = new float[kernelDimX * kernelDimY];
        for (int i = 0; i < kernelDimX; i++)
            for (int j = 0; j < kernelDimY; j++)
                blurKernel[i * kernelDimX + j] = 1.0f / (float) (kernelDimX * kernelDimY);

        BufferedImageOp op = new ConvolveOp(new Kernel(kernelDimX, kernelDimY, blurKernel));
        imageCopy = op.filter(imageCopy, null);

    }

    // draw the thumbnail
    gr.drawImage(imageCopy, 0, 0, thumbnailWidth, thumbnailHeight, clipX1, clipY1, clipX2, clipY2, null);

    // and we are done
    gr.dispose();
    ImageIO.write(thumbnail, "png", thumbnailFile);

    // redirect to it
    response.sendRedirect(baseUrl + "/" + thumbnailFileName);

}

From source file:com.salesmanager.core.util.ProductImageUtil.java

public BufferedImage blurImage(BufferedImage image) {
    float ninth = 1.0f / 9.0f;
    float[] blurKernel = { ninth, ninth, ninth, ninth, ninth, ninth, ninth, ninth, ninth };
    Map<Key, Object> map = new HashMap<Key, Object>();
    map.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    map.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    map.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    RenderingHints hints = new RenderingHints(map);
    BufferedImageOp op = new ConvolveOp(new Kernel(3, 3, blurKernel), ConvolveOp.EDGE_NO_OP, hints);
    return op.filter(image, null);
}

From source file:de.brazzy.nikki.model.ImageReader.java

/**
 * Scales image to given size, preserving aspec ratio
 * /*from   w w w  .j  ava 2  s .  c om*/
 * @param toWidth
 *            width to scale to
 * @param paintBorder
 *            whether to paint an etched border around the image
 * @param isThumbnail
 *            if true, faster low-quality scaling will be used
 */
public byte[] scale(int toWidth, boolean paintBorder, boolean isThumbnail)
        throws IOException, LLJTranException {
    readMainImage();

    int toHeight = heightForWidth(mainImage, toWidth);

    BufferedImageOp op = isThumbnail ? new ThumpnailRescaleOp(toWidth, toHeight)
            : new ResampleOp(toWidth, toHeight);

    BufferedImage scaledImage = op.filter(mainImage, null);
    if (paintBorder) {
        Graphics2D g = scaledImage.createGraphics();
        g.setPaintMode();
        new EtchedBorder(EtchedBorder.RAISED, Color.LIGHT_GRAY, Color.DARK_GRAY).paintBorder(null, g, 0, 0,
                toWidth, toHeight);
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ImageIO.write(scaledImage, "jpg", out);
    return adjustForRotation(out.toByteArray());
}

From source file:com.devnexus.ting.web.controller.SiteController.java

@RequestMapping("/s/organizers")
public String getOrganizers(final Model model) {

    final List<Organizer> organizers = businessService.getAllOrganizersWithPicture();

    final OrganizerList organizerList = new OrganizerList(organizers);
    model.addAttribute("organizerList", organizerList);

    int columnLength = (int) (organizers.size() / 4);

    model.addAttribute("columnLength", columnLength < 1 ? 1 : columnLength);
    model.addAttribute("organizers", organizers);

    final Map<Long, String> organizerPictures = new HashMap<>();

    for (Organizer organizer : organizers) {
        final FileData imageData = organizer.getPicture();
        if (imageData != null) {
            final ByteArrayInputStream bais = new ByteArrayInputStream(imageData.getFileData());
            try {
                final BufferedImage image;
                final BufferedImage imageToReturn;
                image = ImageIO.read(bais);

                if (image.getWidth() != 310 && image.getHeight() != 360) {
                    BufferedImageOp resampler = new ResampleOp(310, 360, ResampleOp.FILTER_LANCZOS);
                    imageToReturn = resampler.filter(image, null);
                } else {
                    imageToReturn = image;
                }/*from w  w  w  .  j a  va 2  s  .c  om*/

                final ByteArrayOutputStream out = new ByteArrayOutputStream();
                ImageIO.write(imageToReturn, "JPG", out);

                byte[] bytes = out.toByteArray();

                final String base64bytes = Base64.encodeBase64String(bytes);
                final String src = "data:image/jpg;base64," + base64bytes;
                organizerPictures.put(organizer.getId(), src);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    model.addAttribute("organizerPictures", organizerPictures);
    return "organizers";

}

From source file:view.FramePrincipal.java

private void applyKernelJava(Kernel kernel, String DescricaoKernel) {
    long before = 0;
    long after = 0;
    double durationMS = 0;
    String message = null;//from w w  w .  j  a v a  2 s .c  o m

    BufferedImageOp bop = new ConvolveOp(kernel);
    before = System.nanoTime();
    outputImage = bop.filter(inputImage, outputImage);
    after = System.nanoTime();
    durationMS = (after - before) / 1e6;
    message = "Java: " + String.format("%.2f", durationMS) + " ms" + " Descrio : "
            + truncate(DescricaoKernel, 13);
    DadosEstatisticos dadosEstatisticos = new DadosEstatisticos(truncate(DescricaoKernel, 13),
            kernel.getWidth(), kernel.getHeight(), (float) durationMS);
    listaDadosEstatisticosJava.add(dadosEstatisticos);
    setInfoProcesso(dadosEstatisticos);
    System.out.println(message);

}

From source file:view.FramePrincipal.java

private void applyKernelJavaThread(final Kernel kernel, final String DescricaoKernel) {
    new Thread(new Runnable() {
        @Override/*from  w ww.  j  a  v  a2s . com*/
        public void run() {
            long before = 0;
            long after = 0;
            double durationMS = 0;
            String message = null;

            BufferedImageOp bop = new ConvolveOp(kernel);
            before = System.nanoTime();
            outputImage = bop.filter(inputImage, outputImage);
            after = System.nanoTime();
            durationMS = (after - before) / 1e6;
            message = "Java ThRead: " + String.format("%.2f", durationMS) + " ms" + " Descrio : "
                    + truncate(DescricaoKernel, 13);
            DadosEstatisticos dadosEstatisticos = new DadosEstatisticos(truncate(DescricaoKernel, 13),
                    kernel.getWidth(), kernel.getHeight(), (float) durationMS);
            listaDadosEstatisticosJavaTHREAD.add(dadosEstatisticos);
            setInfoProcesso(dadosEstatisticos);
            System.out.println(message);

        }
    }).start();

}