在本篇博客中,我们将介绍如何使用Java开发一个简单的图像查看器和处理器,主要功能包括加载图像、应用灰度和二值化滤镜,以及保存处理后的图像。该项目采用Java的Swing库构建图形用户界面,并采用面向对象的方法,使代码模块化,易于扩展。
项目结构
我们的项目分为以下几个关键组件:
- ImageProUI:主用户界面,包含布局和事件监听器。
- ImageListener:处理用户操作的事件监听器。
- ImagePanel:用于绘制和刷新图像的面板。
- ImageUtils:执行图像加载和滤镜处理的工具类。
1. ImageProUI:主用户界面
ImageProUI
类负责创建主窗口和布局,初始化按钮面板,并将各个组件添加到窗口中。以下是ImageProUI
类的代码:
import javax.swing.*;
import java.awt.*;
public class ImageProUI {
// 创建一个监听器
ImageListener imgl = new ImageListener();
// 图像处理器
ImageUtils imgUtils = new ImageUtils();
// 图片面板 绘制刷新
ImagePanel imgPanel = new ImagePanel();
public void showUI() {
// 创建窗体 创建面板
JFrame jf = new JFrame();
jf.setTitle("美颜相机");
jf.setSize(1000, 800);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 布局: 流式布局 (JPanel 默认布局) 边框布局(窗体的默认布局)
// JPanel 面板 是一个容器
JPanel btnPanel = new JPanel();
// 设置面板的属性
btnPanel.setBackground(Color.gray);
imgPanel.setBackground(Color.BLACK);
// 设置宽高
Dimension dim = new Dimension(0, 100);
btnPanel.setPreferredSize(dim);
// 初始化按钮面板
initBtnPanel(btnPanel);
// 添加到窗体上
jf.add(btnPanel, BorderLayout.SOUTH);
jf.add(imgPanel, BorderLayout.CENTER);
jf.setVisible(true);
// 三个对象之间交替传递
imgPanel.addImageUtils(imgUtils);
imgl.addImagePanel(imgPanel);
imgl.addImageUtils(imgUtils);
}
public void initBtnPanel(JPanel btnPanel) {
String[] btnTexts = {"打开", "保存", "原图", "马赛克", "灰度","二值"};
for (int i = 0; i < btnTexts.length; i++) {
JButton btn = new JButton(btnTexts[i]);
btn.setBackground(Color.WHITE);
btnPanel.add(btn);
btn.addActionListener(imgl);
}
String[] btnTexts2 = {"画笔", "直线", "矩形", "填充", "截图", "马赛克画笔"};
for (int i = 0; i < btnTexts2.length; i++) {
JButton btn = new JButton(btnTexts2[i]);
btn.setBackground(Color.WHITE);
btnPanel.add(btn);
btn.addActionListener(imgl);
}
}
public static void main(String[] args) {
ImageProUI ipu = new ImageProUI();
ipu.showUI();
}
}
2. ImageListener:事件监听器
ImageListener
类实现了ActionListener
和MouseListener
接口,负责处理用户的操作事件,如打开图像、保存图像、应用滤镜等。以下是ImageListener
类的代码:
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
public class ImageListener implements ActionListener, MouseListener {
// 面板
ImagePanel imagePanel; // 实现刷新 绘制已保存的最后一张
ImageUtils imgUtils; // 执行加载图片 滤镜处理
public void addImageUtils(ImageUtils imgUtils) {
this.imgUtils = imgUtils;
}
public void addImagePanel(ImagePanel imagePanel) {
this.imagePanel = imagePanel;
}
@Override
public void actionPerformed(ActionEvent e) {
String ac = e.getActionCommand();
System.out.println("ac:" + ac);
if (ac.equals("打开")) {
// 打开图片 目的获取一张照片的绝对路径地址
// 文件选择器
JFileChooser jfc = new JFileChooser();
// 文件名过滤器
FileNameExtensionFilter filter = new FileNameExtensionFilter
("JPG & PNG", "jpg", "png");
jfc.setFileFilter(filter);
int state = jfc.showOpenDialog(null);
if (state == JFileChooser.APPROVE_OPTION) { // 选择时点了确定按钮
// 获取所选择的文件绝对路径
String path = jfc.getSelectedFile().getAbsolutePath();
// 图像处理对象 调用加载图片的方法 转为像素二维数组
imgUtils.loadImage(path);
}
} else if (ac.equals("保存")) {
// 保存图像的功能实现
} else if (ac.equals("原图")) {
imgUtils.drawImage();
} else if (ac.equals("灰度")) {
imgUtils.drawGray();
} else if (ac.equals("二值")) {
imgUtils.drawBinary();
}
// 刷新面板
imagePanel.repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
// 鼠标点击事件处理
}
@Override
public void mousePressed(MouseEvent e) {
// 鼠标按下事件处理
}
@Override
public void mouseReleased(MouseEvent e) {
// 鼠标释放事件处理
}
@Override
public void mouseEntered(MouseEvent e) {
// 鼠标进入事件处理
}
@Override
public void mouseExited(MouseEvent e) {
// 鼠标退出事件处理
}
}
3. ImagePanel:图像面板
ImagePanel
类继承自JPanel
,用于绘制和刷新图像。以下是ImagePanel
类的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class ImagePanel extends JPanel {
ImageUtils imgUtils;
public void addImageUtils(ImageUtils imgUtils) {
this.imgUtils = imgUtils;
}
// 绘制 图片
@Override
public void paint(Graphics g) {
super.paint(g);
paintImage(g);
}
public void paintImage(Graphics g) {
// 获取最后一张照片
BufferedImage lastImage = imgUtils.getLastImage();
if (lastImage == null) {
return;
}
int w = lastImage.getWidth();
int h = lastImage.getHeight();
// 计算居中位置
int x = this.getWidth() / 2 - w / 2;
int y = (this.getHeight() - h) / 2;
g.drawImage(lastImage, x, y, null);
}
}
4.ImageUtils:主函数
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class ImageUtils {
// 属性
private int[][] imgArr;
private int w;
private int h;
private ArrayList<BufferedImage> imgList = new ArrayList<>();
public void addImage(BufferedImage image) {
imgList.add(image);
}
public BufferedImage getLastImage() {
if (imgList.isEmpty()) {
return null;
}
return imgList.get(imgList.size() - 1);
}
// 加载图片
public void loadImage(String path) {
File file = new File(path);
try {
BufferedImage image = ImageIO.read(file);
addImage(image);
w = image.getWidth();
h = image.getHeight();
// 创建一个空的数组 存储图片的像素值
imgArr = new int[w][h];
// 将图片的像素值复制到数组中
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
imgArr[i][j] = image.getRGB(i, j);
}
}
System.out.println("加载图片完成");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// 绘制方法
public BufferedImage drawImage() {
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics imgGra = image.getGraphics();
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
int pixNum = imgArr[i][j];
Color color = new Color(pixNum);
imgGra.setColor(color);
imgGra.fillRect(i, j, 1, 1);
}
}
addImage(image);
return image;
}
public BufferedImage drawGray() {
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
// 获取这个图片对象的图像渲染器-画笔
Graphics imgGra = image.getGraphics();
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
int pixNum = imgArr[i][j];
Color color = new Color(pixNum);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int gray = (red + green + blue) / 3;
Color grayColor = new Color(gray, gray, gray);
imgGra.setColor(grayColor);
imgGra.fillRect(i, j, 1, 1);
}
}
addImage(image);
return image;
}
public BufferedImage drawBinary() {
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
// 获取这个图片对象的图像渲染器-画笔
Graphics imgGra = image.getGraphics();
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
int pixNum = imgArr[i][j];
Color color = new Color(pixNum);
// 取出红绿蓝 的平均值
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int gray = (red + green + blue) / 3;
// 根据灰度值判断
if (gray < 70) {
imgGra.setColor(Color.BLACK);
} else {
imgGra.setColor(Color.WHITE);
}
imgGra.fillRect(i, j, 1, 1);
}
}
addImage(image);
return image;
}
}