📄 imagery.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.ImageIO;
/**
* A panel for displaying an image (BufferedImage).
*/
class ImagePanel extends JPanel {
private BufferedImage image = null;
/**
* Sets the image to display, and adjusts the panel size accordingly.
* @param image the new image to display
*/
public void setImage(BufferedImage image) {
this.image = image;
Dimension d = new Dimension(image.getWidth(), image.getHeight());
setPreferredSize(d);
}
/**
* Returns the image being displayed.
* @return the image
*/
public BufferedImage getImage() {
return image;
}
/**
* Paints the image. This method is typically invoked by the Swing library
* and shall not be directly invoked by user code in general.
* @param g the graphics context to draw
*/
public void paint(Graphics g) {
super.paint(g);
if (image != null) {
g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), this);
}
}
}
/**
* Image processing utilities. All are static methods.
*/
class ImageProcessor {
/**
* Increases the brightness of an image, and return the brighter version.
* @param image the original image
* @return the brighter image
*/
public static BufferedImage brighter(BufferedImage image) {
return doBrightness(image, 20);
}
/**
* Decreases the brightness of an image, and return the darker version.
* @param image the original image
* @return the darker image
*/
public static BufferedImage darker(BufferedImage image) {
return doBrightness(image, -20);
}
/**
* Blurs an image, and return the blurred version.
* @param image the original image
* @return the blurer image
*/
public static BufferedImage blurer(BufferedImage image) {
float ninth = 1.0f / 9.0f;
float[] blurKernel = { ninth, ninth, ninth, ninth, ninth, ninth, ninth,
ninth, ninth, };
BufferedImageOp op = new ConvolveOp(new Kernel(3, 3, blurKernel));
BufferedImage mBufferedImage = op.filter(toRGB(image), null);
return mBufferedImage;
}
/**
* Sharpens an image, and return the sharpened version.
* @param image the original image
* @return the sharper image
*/
public static BufferedImage sharper(BufferedImage image) {
float[] sharpKernel = { 0.0f, -1.0f, 0.0f, -1.0f, 5.0f, -1.0f, 0.0f,
-1.0f, 0.0f };
BufferedImageOp op = new ConvolveOp(new Kernel(3, 3, sharpKernel));
BufferedImage mBufferedImage = op.filter(toRGB(image), null);
return mBufferedImage;
}
/**
* Enlarges an image, and return the larger version. When the original image
* is larger than 1024 by 768 (either sides), the original image is
* returned.
* @param image the original image
* @return the larger image
*/
public static BufferedImage larger(BufferedImage image) {
if (image.getWidth() > 1024 || image.getHeight() > 768)
return image;
else
return doScaling(image, 1.1, 1.1);
}
/**
* Reduces an image size, and return the smaller version. When the original
* image is smaller than 20 by 20 (either sides), the original image is
* returned.
* @param image the original image
* @return the smaller image
*/
public static BufferedImage smaller(BufferedImage image) {
if (image.getWidth() < 20 || image.getHeight() < 20)
return image;
else
return doScaling(image, 0.9, 0.9);
}
/**
* This is to make the image brighter
* @param image of the original image, image changing value
* @return the brighter/darker image
*/
private static BufferedImage doBrightness(BufferedImage inputImage,
int valueForAdd) {
short[] red = new short[256];
short[] green = new short[256];
short[] blue = new short[256];
for (int i = 0; i < 256; i++) {
red[i] = (short) (i + valueForAdd);
green[i] = (short) (i + valueForAdd);
blue[i] = (short) (i + valueForAdd);
if (red[i] > 255)
red[i] = 255;
else if (red[i] < 0)
red[i] = 0;
if (green[i] > 255)
green[i] = 255;
else if (green[i] < 0)
green[i] = 0;
if (blue[i] > 255)
blue[i] = 255;
else if (blue[i] < 0)
blue[i] = 0;
}
short[][] brightness = new short[][] { red, green, blue };
BufferedImageOp op = new LookupOp(new ShortLookupTable(0, brightness),
null);
BufferedImage mBufferedImage = op.filter(toRGB(inputImage), null);
return mBufferedImage;
}
/**
* To scale the size of the image
* @param image the original image, x-pixel value, y-pixel value
* @return the resized_image
*/
private static BufferedImage doScaling(BufferedImage image, double sx,
double sy) {
BufferedImageOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(sx, sy), AffineTransformOp.TYPE_BILINEAR);
BufferedImage mBufferedImage = op.filter(toRGB(image), null);
return mBufferedImage;
}
/**
* To change the color of the image such as brighter the photo
* @param image the original image
* @return the color_changed_image
*/
private static BufferedImage toRGB(BufferedImage image) {
if (image.getType() == BufferedImage.TYPE_INT_RGB) {
return image;
} else {
BufferedImage rgbImage = new BufferedImage(image.getWidth(), image
.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g2d = rgbImage.createGraphics();
g2d.drawImage(image, 0, 0, null);
return rgbImage;
}
}
}
/**
* This is a main class which is to build the GUI function and buttons response.
*/
public class Imagery extends JFrame implements ActionListener {
ImagePanel imagePanel = new ImagePanel();
void buildGUI()
{
JPanel mainpanel = new JPanel();
JPanel upper = new JPanel();
JPanel middle = new JPanel();
JPanel lower = new JPanel();
mainpanel.setLayout(new BorderLayout());
JButton Brighter = new JButton("Brighter");
Brighter.addActionListener(this);
JButton Darker = new JButton("Darker");
Darker.addActionListener(this);
JButton Blurer = new JButton("Blurer");
Blurer.addActionListener(this);
JButton Sharper = new JButton("Sharper");
Sharper.addActionListener(this);
JButton Larger = new JButton("Larger");
Larger.addActionListener(this);
JButton Smaller = new JButton("Smaller");
Smaller.addActionListener(this);
upper.add(Brighter);
upper.add(Darker);
upper.add(Blurer);
upper.add(Sharper);
upper.add(Larger);
upper.add(Smaller);
middle.add(imagePanel);
JButton Load = new JButton("Load");
Load.addActionListener(this);
JButton Save = new JButton("Save");
Save.addActionListener(this);
JButton Quit = new JButton("Quit");
Quit.addActionListener(this);
/**Add to the GUI*/
lower.add(Load);
lower.add(Save);
lower.add(Quit);
mainpanel.add(upper, BorderLayout.NORTH);
mainpanel.add(middle, BorderLayout.CENTER);
mainpanel.add(lower, BorderLayout.SOUTH);
getContentPane().add(mainpanel);
}
/**When the button is pressed, the relative action will be done*/
public void actionPerformed(ActionEvent e)
{
JButton b =(JButton) e.getSource();
String text = b.getText();
if (text.equals("Brighter"))
{
BufferedImage NewImage = ImageProcessor.brighter(imagePanel.getImage());
imagePanel.setImage(NewImage);
pack();
repaint();
}
else if (text.equals("Darker"))
{
BufferedImage NewImage = ImageProcessor.darker(imagePanel.getImage());
imagePanel.setImage(NewImage);
pack();
repaint();
}
else if (text.equals("Blurer"))
{
BufferedImage NewImage = ImageProcessor.blurer(imagePanel.getImage());
imagePanel.setImage(NewImage);
pack();
repaint();
}
else if (text.equals("Sharper"))
{
BufferedImage NewImage = ImageProcessor.sharper(imagePanel.getImage());
imagePanel.setImage(NewImage);
pack();
repaint();
}
else if (text.equals("Larger"))
{
BufferedImage NewImage = ImageProcessor.larger(imagePanel.getImage());
imagePanel.setImage(NewImage);
pack();
repaint();
}
else if (text.equals("Smaller"))
{
BufferedImage NewImage = ImageProcessor.smaller(imagePanel.getImage());
imagePanel.setImage(NewImage);
pack();
repaint();
}
else if (text.equals("Load"))
{
JFileChooser chooser = new JFileChooser();
int reply = chooser.showOpenDialog(null);
if (reply == JFileChooser.APPROVE_OPTION)
{
BufferedImage OriginalImage = imagePanel.getImage();
File file = chooser.getSelectedFile();
try
{
imagePanel.setImage(ImageIO.read(file));
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(this, "Cannot load file " + file.getName());
imagePanel.setImage(OriginalImage);
}
/*catch (IllegalArgumentException ex)
{
JOptionPane.showMessageDialog(this, "Cannot load file " + file.getName());
}*/
pack();
repaint();
}
}
else if (text.equals("Save"))
{
JFileChooser chooser = new JFileChooser();
int reply = chooser.showOpenDialog(null);
if (reply == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
String filename = file.getName();
int i = filename.lastIndexOf('.');
String ext = filename.substring(i+1);
if(ext.equalsIgnoreCase("jpg")||ext.equalsIgnoreCase("bmp")||ext.equalsIgnoreCase("gif")||ext.equalsIgnoreCase("png")||ext.equalsIgnoreCase("tiff")||ext.equalsIgnoreCase("jpeg")||ext.equalsIgnoreCase("jpe")||ext.equalsIgnoreCase("jfif")||ext.equalsIgnoreCase("tif")||ext.equalsIgnoreCase("ico"))
{
try
{
boolean ok;
ok=ImageIO.write(imagePanel.getImage(), ext, file);
if (ok==true)
JOptionPane.showMessageDialog(this, ""+filename+" save successful!");
else
JOptionPane.showMessageDialog(this, "Cannot save file in this type (" + ext +"), file name: " + filename);
}catch(Exception ex){
JOptionPane.showMessageDialog(this, "Cannot save file : " + filename);
}
}
else
JOptionPane.showMessageDialog(this, "Cannot save file in this type (" + ext +"), file name: " + filename);
}
}
else if (text.equals("Quit"))
{
setVisible(false);
System.exit(1);
}
}
/**main core*/
public static void main(String args[])
{
Imagery img = new Imagery();
img.buildGUI();
img.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
img.pack();
img.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -