Filtration and changing of digital photos Print E-mail
Article Index
Filtration and changing of digital photos
2
3
All Pages
In the article below is described, how to change and filter digital photos. You should take one photo in the JPG format and place it to the same dir as the program source code

For to begin, let's greate basis for those examples. The following program, ExampleFramework, uses JFrame, which contains image and uses JSlider. But for the easiest comprehension of filters and different codes, which are related to the effects, I've put fast access functions. For sinhronization purposes the selected image will be called as filteredImage, if it was filtered or changed.

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.BorderLayout;
import java.awt.image.BufferedImage;

public class ExampleFramework extends JPanel {
private BufferedImage originalImage;
private BufferedImage filteredImage;
private JSlider slide = new JSlider(1, 50, 25);
private int height, width;

ExampleFramework(String titlebar) {
createBufferedImages();
setUpJFrame(titlebar);
}

private void createBufferedImages() {
Image image =
new ImageIcon("test.jpg").getImage();
height = image.getHeight(null);
width = image.getWidth(null);
originalImage =
new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
filteredImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = originalImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
}

private void setUpJFrame(String titlebar) {
JFrame frame = new JFrame(titlebar);
frame.setSize(filteredImage.getWidth(),
filteredImage.getHeight());
frame.getContentPane().setLayout
(new BorderLayout());
frame.getContentPane().add
(this, BorderLayout.CENTER);
frame.getContentPane().add
(slide, BorderLayout.SOUTH);
frame.setResizable(false);
frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

// fast filtering methods.

public void setChangeListener(ChangeListener cl) {
slide.addChangeListener(cl);
}

public void setFilteredImage
(BufferedImage image) {
filteredImage = image;
repaint();
}

public int getSlideValue() {
return slide.getValue();
}

public BufferedImage getOriginal() {
return originalImage;
}

public void paintComponent(Graphics g) {
g.clearRect(0, 0, width, height);
g.drawImage(filteredImage, 0, 0, this);
}
}



Let's use afine transformation for zooming of the image. It's nessesary to create afine transformation and pass 2 arguments to it. Then new image will be created with help of AffineTransformOp, which was created previously. At last the refresh image on the creen command will be sent to the ExampleFramework Here is code, which demonstrates those steps:

AffineTransformOp op = new AffineTransformOp(
AffineTransform.getScaleInstance(multiple,
multiple),
AffineTransformOp.TYPE_BILINEAR);
BufferedImage tempImage =
op.filter(original, null);
framework.setFilteredImage(tempImage);

The first argument defines, which type of the affine transformation (AffineTransform) is created. In this case you create zooming by calling getScaleInstance() method, and sends horizontal and vertical parameters. The 2nd argument defines the type of interpolation. In this case the type TYPE_BILINEAR is used, which counts the value of each pixel as average of neighbor pixels. Your other choice can be the TYPE_NEAREST_NEIGHBOR, which pastes values of neighbor pixels. Use each of type in this example. Pay attention, that type TYPE_BILINEAR is slower, but gives better quality of the image. Thus TYPE_NEAREST_NEIGHBOR is more sensitive, the image becomes granular while zooming.