非常适合新人入手的项目,java美颜相机,简单易懂,所见即所得,你值得拥有
第一步:创建一个类,写一个窗口
继承JFrame类,写一个方法,这个方法将成为我们的窗口。
写出窗口的一些基本参数
通过主函数调用窗口,这个时候就可以运行了
第二步:添加按钮,以及写一个监听器类
通过for循环给窗口添加按钮,在前面加入流式布局
运行一下
写一个监听器类,实现ActionListener接口,重写接口中的方法。
在主类中通过对象调用监听器类
给按钮加上监听器
我们在监听器上加一个输出语句测试一下是不是连上了。
没有问题
第三步:给每个按钮写上方法
首先,我们要知道你点击了什么按钮,通过getActionCommand方法获取点击按钮的文字信息。
非常成功
接下来声明一些我们需要用到的变量
注意这个画笔,我们是要用我们主函数在窗口上的画笔的
然后我们写第一个加载图片的方法
在此之前,我们先需要写一个函数用于从本地文件路径提取图片的像素,要提取的东西有三个,图片的高,宽,以及每个像素的像素值,在这里我们选择使用一个二维数组来存储这些信息。
写完这个函数后我们可以开始写按钮的功能了。
像这样写出第一个按钮的功能
尝试一下
图片成功地加载出来了。
再加入一些其他的方法,可以看看我的源码自行决定
package Camera1023;
import javax.swing.*;
import java.awt.*;
public class main extends JFrame {
//引用监听器
imageListener listener = new imageListener();
public void intiUI(){
//设置标题
this.setTitle("Camera1023");
//设置大小
this.setSize(600, 1000);
//关闭及停止程序
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
//流式布局
this.setLayout(new FlowLayout());
//添加按钮
String[] btuStrs = {"加载图片","原片","马赛克","灰度","二值化","圆点马赛克","轮廓化","反值","画笔"};
for (String btuStr : btuStrs) {
JButton button = new JButton(btuStr);
this.add(button);
//给所有按钮加上监听器
button.addActionListener(listener);
}
//显示
this.setVisible(true);
//加入画笔
listener.g = this.getGraphics();
}
public static void main(String[] args) {
main main = new main();
main.intiUI();
}
}
package Camera1023;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
public class imageListener implements ActionListener {
//声明变量
Graphics g;
//存储像素值
int[][] imgArr;
//高宽
int w,h;
//重写接口方法
public void actionPerformed(ActionEvent e){
String command = e.getActionCommand();
System.out.println(command+"被点击了");
if (command.equals("加载图片")){
//通过调用下边写好的函数,
imgArr = getImgPixs("C:\\Users\\eugene\\Desktop\\微信图片编辑_20241023125849.jpg");
//获取高度宽度
w = imgArr.length;
h = imgArr[0].length;
for (int i=0;i<w;i++){
for (int j=0;j<h;j++){
//提取每个像素的rgb
int rgb = imgArr[i][j];
Color c = new Color(rgb);
//设置颜色
g.setColor(c);
g.fillRect(i,j+90,1,1);
}
}
}
if (command.equals("原片")){
for (int i=0;i<w;i++){
for (int j=0;j<h;j++){
//提取每个像素的rgb
int rgb = imgArr[i][j];
Color c = new Color(rgb);
//设置颜色
g.setColor(c);
g.fillRect(i,j+90,1,1);
}
}
}
if (command.equals("马赛克")){
for (int i=0;i<w;i++){
for (int j=0;j<h;j++){
//提取每个像素的rgb
int rgb = imgArr[i][j];
Color c = new Color(rgb);
//设置颜色
g.setColor(c);
g.fillRect(i,j+90,10,10);
}
}
}
if (command.equals("圆点马赛克")){
for (int i=0;i<w;i++){
for (int j=0;j<h;j++){
//提取每个像素的rgb
int rgb = imgArr[i][j];
Color c = new Color(rgb);
//设置颜色
g.setColor(c);
g.fillOval(i,j+90,10,10);
}
}
}
if (command.equals("灰度")){
for(int i=0;i<w;i++){
for (int j = 0; j < h; j++) {
int rgb = imgArr[i][j];
Color color = new Color(rgb);
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
int grey = (int)(red*0.30+green*0.59+blue*0.11);
Color color1 = new Color(grey,grey,grey);
g.setColor(color1);
g.fillRect(i,90+j,1,1);
}
}
}
if (command.equals("反值")){
for(int i=0;i<w;i++){
for (int j = 0; j < h; j++) {
int rgb = imgArr[i][j];
Color color = new Color(rgb);
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
Color color1=new Color(255-red,255-green,255-blue);
g.setColor(color1);
g.fillRect(i,90+j,1,1);
}
}
}
if (command.equals("二值化")){
for(int i=0;i<w;i++){
for (int j = 0; j < h; j++) {
int rgb = imgArr[i][j];
Color color = new Color(rgb);
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
int grey = (int)(red*0.30+green*0.59+blue*0.11);
if(grey>125){
grey=255;
}else{
grey=0;
}
Color color1 = new Color(grey,grey,grey);
g.setColor(color1);
g.fillRect(i,90+j,1,1);
}
}
}
}
//写一个函数用来提取像素,输入为本地文件路径
public int[][] getImgPixs(String imagepath) {
//导入文件路径
File file = new File(imagepath);
//初始化缓存图像
BufferedImage image = null;
//调用read方法,读取File对象
//将返回值存入image
try{
image = ImageIO.read(file);
}
//丢掉报错
catch (Exception e){
throw new RuntimeException(e);
}
//先获取图片的宽高
int w = image.getWidth();
int h = image.getHeight();
int[][] imgArr = new int[w][h];
//此时缓存中已有我们要的像素值,通过for循环一个一个提取
for (int x = 0; x < w; x++){
for (int y = 0; y < h; y++){
imgArr[x][y] = image.getRGB(x, y);
}
}
//最后返回提取出来的数组
return imgArr;
}
}