image

Sharpening a Buffered Image

In this example we are going to see how can you load an image from a source (a URL for instance) and make it look sharper in your own screen and according to the graphics environment that it is displayed into.

Basically, to sharpen a Buffered Image one should take the following steps:

  • Load an image from a URL using Toolkit.getDefaultToolkit().getImage method
  • Use an ImageObserver to monitor the loading of the image. When the image is fully load the user will be notified
  • Create a buffed image from the source image with a format more close to the custom display environment using GraphicsEnvironment, GraphicsDevice and GraphicsConfiguration to perform several image configurations
  • Draw the image into the buffered image
  • Use a BufferedImageOp to put a new sharper Kernel to the image
  • And simply paint the buffered image in a new Frame
as you can see in the code snippet that follows:
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.javacodegeeks.snippets.desktop;
 
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.ImageObserver;
import java.awt.image.Kernel;
 
public class BufferedImageSharpen {
 
    static BufferedImage image;
    static boolean imageLoaded = false;
 
    public static void main(String[] args) {
 
        // The ImageObserver implementation to observe loading of the image
 
        ImageObserver myImageObserver = new ImageObserver() {
 
            @Override
            public boolean imageUpdate(Image image, int flags, int x, int y, int width, int height) {
 
                if ((flags & ALLBITS) != 0) {
 
                    imageLoaded = true;
 
                    System.out.println("Image loading finished!");
 
                    return false;
 
                }
 
                return true;
 
            }
        };
 
        // The image URL - change to where your image file is located!
 
        String imageURL = "image.png";
 
        /**
         *
         * This call returns immediately and pixels are loaded in the background
         *
         * We use an ImageObserver to be notified when the loading of the image
         *
         * is complete
         *
         */
        Image sourceImage = Toolkit.getDefaultToolkit().getImage(imageURL);
 
        sourceImage.getWidth(myImageObserver);
 
        // We wait until the image is fully loaded
 
        while (!imageLoaded) {
 
            try {
                Thread.sleep(100);
 
            } catch (InterruptedException e) {
            }
 
        }
 
        // Create a buffered image from the source image with a format that's compatible with the screen
 
        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
 
        GraphicsDevice graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
 
        GraphicsConfiguration graphicsConfiguration = graphicsDevice.getDefaultConfiguration();
 
        // If the source image has no alpha info use Transparency.OPAQUE instead
 
        image = graphicsConfiguration.createCompatibleImage(sourceImage.getWidth(null), sourceImage.getHeight(null), Transparency.BITMASK);
 
        // Copy image to buffered image
 
        Graphics graphics = image.createGraphics();
 
        // Paint the image onto the buffered image
 
        graphics.drawImage(sourceImage, 0, 0, null);
 
        graphics.dispose();
 
        // A 3x3 kernel that sharpens an image
 
        Kernel kernel = new Kernel(3, 3,
                new float[]{
                    -1, -1, -1,
                    -1, 9, -1,
                    -1, -1, -1});
 
        BufferedImageOp op = new ConvolveOp(kernel);
 
        image = op.filter(image, null);
 
        // Create frame with specific title
 
        Frame frame = new Frame("Example Frame");
 
        // Add a component with a custom paint method
 
        frame.add(new CustomPaintComponent());
 
        // Display the frame
 
        int frameWidth = 300;
 
        int frameHeight = 300;
 
        frame.setSize(frameWidth, frameHeight);
 
        frame.setVisible(true);
 
    }
 
    /**
     * To draw on the screen, it is first necessary to subclass a Component and
     * override its paint() method. The paint() method is automatically called
     * by the windowing system whenever component's area needs to be repainted.
     */
    static class CustomPaintComponent extends Component {
 
        @Override
        public void paint(Graphics g) {
 
// Retrieve the graphics context; this object is used to paint
// shapes
 
            Graphics2D g2d = (Graphics2D) g;
 
            /**
             * Draw an Image object The coordinate system of a graphics context
             * is such that the origin is at the northwest corner and x-axis
             * increases toward the right while the y-axis increases toward the
             * bottom.
             */
            int x = 0;
            int y = 0;
            g2d.drawImage(image, x, y, this);
        }
    }
}

 
This was an example on how to sharpen a Buffered Image.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button