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:Main.java

public static void main(String[] argv) throws Exception {
    BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_BYTE_INDEXED);

    Kernel kernel = new Kernel(3, 3, new float[] { -1, -1, -1, -1, 9, -1, -1, -1, -1 });
    BufferedImageOp op = new ConvolveOp(kernel);
    bufferedImage = op.filter(bufferedImage, null);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_BYTE_INDEXED);

    Kernel kernel = new Kernel(3, 3,
            new float[] { 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f });
    BufferedImageOp op = new ConvolveOp(kernel);
    bufferedImage = op.filter(bufferedImage, null);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_BYTE_INDEXED);

    Kernel kernel = new Kernel(3, 3, new float[] { -2, 0, 0, 0, 1, 0, 0, 0, 2 });
    BufferedImageOp op = new ConvolveOp(kernel);
    bufferedImage = op.filter(bufferedImage, null);

}

From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java

/**
 * Flip the image horizontal/*  w  w w . ja  va 2 s. c  o  m*/
 *
 * @param bufferedImage original image
 * @return horizontally flipped image
 */
private static BufferedImage flipHorizontal(BufferedImage bufferedImage) {
    AffineTransform at = AffineTransform.getTranslateInstance(bufferedImage.getWidth(), 0);
    at.scale(-1.0, 1.0);
    BufferedImageOp biOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    BufferedImage imgRes = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),
            bufferedImage.getType());
    return biOp.filter(bufferedImage, imgRes);
}

From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java

/**
 * Flip the image horizontal/*from   ww w .j a  v a  2  s. c  om*/
 *
 * @param bufferedImage original image
 * @return horizontally flipped image
 */
private static BufferedImage flipVertical(BufferedImage bufferedImage) {
    AffineTransform at = AffineTransform.getTranslateInstance(0, bufferedImage.getHeight());
    at.scale(1.0, -1.0);
    BufferedImageOp biOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    BufferedImage imgRes = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),
            bufferedImage.getType());
    return biOp.filter(bufferedImage, imgRes);
}

From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java

/**
 * Rotate an image using the requested angle
 *
 * @param bufferedImage imeg to rotate//  w w w.j av  a2s  .  c  om
 * @param angle         angle to rotate
 * @return BufferedImage containing the rotation
 */
@SuppressWarnings({ "UnusedAssignment" })
private static BufferedImage rotate(BufferedImage bufferedImage, int angle) {
    angle = angle % 360;
    if (angle == 0)
        return bufferedImage;
    if (angle < 0)
        angle += 360;
    switch (angle) {
    case 90:
        BufferedImageOp rot90 = new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI / 2.0,
                bufferedImage.getHeight() / 2.0, bufferedImage.getHeight() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img90 = new BufferedImage(bufferedImage.getHeight(), bufferedImage.getWidth(),
                bufferedImage.getType());
        return rot90.filter(bufferedImage, img90);
    case 180:
        BufferedImageOp rot180 = new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI,
                bufferedImage.getWidth() / 2.0, bufferedImage.getHeight() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img180 = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),
                bufferedImage.getType());
        return rot180.filter(bufferedImage, img180);
    case 270:
        BufferedImageOp rot270 = new AffineTransformOp(AffineTransform.getRotateInstance(-Math.PI / 2.0,
                bufferedImage.getWidth() / 2.0, bufferedImage.getWidth() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img270 = new BufferedImage(bufferedImage.getHeight(), bufferedImage.getWidth(),
                bufferedImage.getType());
        return rot270.filter(bufferedImage, img270);
    default:
        //rotate using a non-standard angle (have to draw a box around the image that can fit it)
        int box = (int) Math.sqrt(bufferedImage.getHeight() * bufferedImage.getHeight()
                + bufferedImage.getWidth() * bufferedImage.getWidth());
        BufferedImage imgFree = new BufferedImage(box, box, bufferedImage.getType());
        BufferedImage imgRet = new BufferedImage(box, box, bufferedImage.getType());
        Graphics2D g = imgFree.createGraphics();
        if (bufferedImage.getTransparency() == Transparency.OPAQUE) {
            //draw a white background on opaque images since they dont support transparency
            g.setBackground(Color.WHITE);
            g.clearRect(0, 0, box, box);
            Graphics2D gr = imgRet.createGraphics();
            gr.setBackground(Color.WHITE);
            gr.clearRect(0, 0, box, box);
            gr = null;
        }
        g.drawImage(bufferedImage, box / 2 - bufferedImage.getWidth() / 2,
                box / 2 - bufferedImage.getHeight() / 2, bufferedImage.getWidth(), bufferedImage.getHeight(),
                null);
        g = null;
        AffineTransform at = new AffineTransform();
        at.rotate(angle * Math.PI / 180.0, box / 2.0, box / 2.0);
        BufferedImageOp bio;
        bio = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        return bio.filter(imgFree, imgRet);
    }
}

From source file:BlurredImage.java

public void paint(Graphics g) {
    try {//from   ww w . j a  v  a 2  s  .c  om
        BufferedImage myImage = ImageIO.read(this.getClass().getResource("redrock.png"));

        BufferedImage filteredImage = new BufferedImage(myImage.getWidth(null), myImage.getHeight(null),
                BufferedImage.TYPE_BYTE_GRAY);

        Graphics g1 = filteredImage.getGraphics();
        g1.drawImage(myImage, 400, 200, null);

        float[] blurKernel = { 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f };

        BufferedImageOp blur = new ConvolveOp(new Kernel(3, 3, blurKernel));
        myImage = blur.filter(myImage, null);
        g1.dispose();

        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(myImage, null, 3, 3);
    } catch (Exception e) {

    }
}

From source file:MainClass.java

public void paint(Graphics g) {
    BufferedImage img = createImage();

    float ninth = 1.0f / 9.0f;

    float[] blurKernel = { ninth, ninth, ninth, ninth, ninth, ninth, ninth, ninth, ninth };

    BufferedImageOp blurOp = new ConvolveOp(new Kernel(3, 3, blurKernel));

    BufferedImage clone = blurOp.filter(img, null);

    g.drawImage(clone, 20, 20, this);
}

From source file:MainClass.java

public void paint(Graphics g) {
    short[] invert = new short[256];

    for (int i = 0; i < invert.length; i++)
        invert[i] = (short) (255 - i);

    BufferedImageOp invertOp = new LookupOp(new ShortLookupTable(0, invert), null);

    BufferedImage clone = invertOp.filter(createImage(), null);

    g.drawImage(clone, 20, 20, this);
}

From source file:RotateImage45Degrees.java

public RotateImage45Degrees(String imageFile) {
    addNotify();//from w ww.  j a v  a  2s  .  com
    frameInsets = getInsets();
    inputImage = Toolkit.getDefaultToolkit().getImage(imageFile);

    MediaTracker mt = new MediaTracker(this);
    mt.addImage(inputImage, 0);
    try {
        mt.waitForID(0);
    } catch (InterruptedException ie) {
    }

    sourceBI = new BufferedImage(inputImage.getWidth(null), inputImage.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = (Graphics2D) sourceBI.getGraphics();
    g.drawImage(inputImage, 0, 0, null);

    AffineTransform at = new AffineTransform();

    // scale image
    at.scale(2.0, 2.0);

    // rotate 45 degrees around image center
    at.rotate(45.0 * Math.PI / 180.0, sourceBI.getWidth() / 2.0, sourceBI.getHeight() / 2.0);

    /*
     * translate to make sure the rotation doesn't cut off any image data
     */
    AffineTransform translationTransform;
    translationTransform = findTranslation(at, sourceBI);
    at.preConcatenate(translationTransform);

    // instantiate and apply affine transformation filter
    BufferedImageOp bio;
    bio = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);

    destinationBI = bio.filter(sourceBI, null);

    int frameInsetsHorizontal = frameInsets.right + frameInsets.left;
    int frameInsetsVertical = frameInsets.top + frameInsets.bottom;
    setSize(destinationBI.getWidth() + frameInsetsHorizontal, destinationBI.getHeight() + frameInsetsVertical);
    show();
}