用java写一个对传入的图片进行手动任意裁剪的程序
时间: 2023-06-16 12:05:55 浏览: 91
java实现图片的裁剪
4星 · 用户满意度95%
以下是一个简单的Java程序,可以通过图形用户界面手动选择裁剪区域,然后将其保存为新的图像文件。
```java
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageCropper extends JFrame implements ActionListener {
private BufferedImage originalImage;
private BufferedImage croppedImage;
private JLabel imageLabel;
private JFileChooser fileChooser;
private JButton loadButton;
private JButton cropButton;
private JButton saveButton;
private int x, y, w, h;
public ImageCropper() {
setTitle("Image Cropper");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Create file chooser
fileChooser = new JFileChooser();
fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".jpg") || f.isDirectory();
}
public String getDescription() {
return "JPEG Images (*.jpg)";
}
});
// Create load button
loadButton = new JButton("Load Image");
loadButton.addActionListener(this);
// Create crop button
cropButton = new JButton("Crop Image");
cropButton.addActionListener(this);
cropButton.setEnabled(false);
// Create save button
saveButton = new JButton("Save Image");
saveButton.addActionListener(this);
saveButton.setEnabled(false);
// Add buttons to panel
JPanel buttonPanel = new JPanel();
buttonPanel.add(loadButton);
buttonPanel.add(cropButton);
buttonPanel.add(saveButton);
// Add panel to frame
add(buttonPanel, BorderLayout.NORTH);
// Create image label
imageLabel = new JLabel();
imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
// Add image label to frame
JScrollPane scrollPane = new JScrollPane(imageLabel);
add(scrollPane, BorderLayout.CENTER);
// Set frame size and visibility
setSize(500, 500);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loadButton) {
// Show file chooser dialog
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
// Load original image
File file = fileChooser.getSelectedFile();
originalImage = ImageIO.read(file);
// Display original image
imageLabel.setIcon(new ImageIcon(originalImage));
pack();
// Enable crop button
cropButton.setEnabled(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
} else if (e.getSource() == cropButton) {
// Get crop region from user
Rectangle rect = new Rectangle(
JOptionPane.showInputDialog(this, "Enter x-coordinate:"),
JOptionPane.showInputDialog(this, "Enter y-coordinate:"),
JOptionPane.showInputDialog(this, "Enter width:"),
JOptionPane.showInputDialog(this, "Enter height:"));
// Crop image
croppedImage = originalImage.getSubimage(rect.x, rect.y, rect.width, rect.height);
// Display cropped image
imageLabel.setIcon(new ImageIcon(croppedImage));
pack();
// Enable save button
saveButton.setEnabled(true);
} else if (e.getSource() == saveButton) {
// Show file chooser dialog
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
// Save cropped image
File file = fileChooser.getSelectedFile();
ImageIO.write(croppedImage, "jpg", file);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new ImageCropper();
}
}
```
这个程序使用了Java的Swing图形用户界面库,可以通过按钮加载图像、选择裁剪区域并保存裁剪后的图像。你可以根据需要修改代码以适应你的特定需求。
阅读全文