java学习--美颜相机

通过这篇博客我们来学习一个美颜相机的代码,即完成图片处理功能。

一.实现功能

1.在文本框中输入图片地址传入要处理的图片

2.点击窗体上的加载图片按钮将图片像素点保存到二维数组中

3.点击其他功能按钮将相应的效果绘制在窗体上

二.创建窗体

1.设置窗体大小、标题等

2.添加按钮

除了一个个添加按钮外,我们还可以通过for循环语句添加所需按钮

 String[] btnStrs={"加载图片", "原图", "马赛克", "灰度", "二值化", "圆点马赛克", "反片", "轮廓化", "油画", "暖色调","冷色调","清空"};
        for(int i=0;i< btnStrs.length;i++){
            JButton btn=new JButton();
            btn.setText(btnStrs[i]);
            btn.setBounds(50+i*100,30,100,30);
            jf.add(btn);
            btn.addActionListener(lis);
        }

3.添加文本框

        JLabel adressJla=new JLabel("图片地址");
        adressJla.setBounds(50,80,60,30);
        jf.add(adressJla);

        JTextField adressJtf=new JTextField(500);
        adressJtf.setBounds(150,80,500,30);
        jf.add(adressJtf);

最终窗体效果图

 三.添加监听器

窗体已经构建好,接下来只要实现监听器功能即可

创建类ImageListener

传入及绘制图片我们需要动作监听器ActionListener

实现鼠标笔功能我们需要鼠标监听器MouseMotionListener

import java.awt.event.*;

public class ImageListener implements ActionListener, MouseMotionListener {
    @Override
    public void actionPerformed(ActionEvent e) {

    }


    @Override
    public void mouseDragged(MouseEvent e) {
        
    }

    @Override
    public void mouseMoved(MouseEvent e) {

    }
}

1.ActionListener

1.1.首先我们要将窗体文本框中输入的文件地址传入监听器中

在ImageListener中创建JTextField对象adressJtf

JTextField adressJtf;

Image中传入

lis.adressJtf=adressJtf;

1.2.在ImageTools中创建方法getImagePixs()

getImagePixs()功能:存储图片地址imagePaths中图片的像素值到二维数组中

   public int[][] getImagePixs(String imagePaths){
        File file=new File(imagePaths);
        BufferedImage buffImg=null;
        try {
            buffImg = ImageIO.read(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        int width=buffImg.getWidth();
        int height=buffImg.getHeight();
        int [][]imgArr=new int[width][height];
        for(int i=0;i<width;i++){
            for(int j=0;j<height;j++){
                imgArr[i][j]=buffImg.getRGB(i,j);
            }
        }
        return imgArr;
    }

1.3.完善ImageTools中方法

*原图

public void drawImage01(Graphics g,int[][]imgArr){
        for(int i=0;i< imgArr.length;i++) {
            for (int j = 0; j < imgArr[0].length; j++) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                g.setColor(color);
                g.fillRect(100 + i, 200 + j, 1, 1);
            }
        }
    }

*马赛克

画像素值的间距增大

public void drawImage02(Graphics g,int[][]imgArr){
        for(int i=0;i< imgArr.length;i+=3) {
            for (int j = 0; j < imgArr[0].length; j+=3) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                g.setColor(color);
                g.fillRect(100 + i, 200 + j, 3, 3);
            }
        }
    }

*灰度

计算每一个像素灰度值,并生成灰色

引用自 百度百科-验证https://baike.baidu.com/item/%E7%81%B0%E5%BA%A6%E5%80%BC/10259111?fr=ge_ala

这里取第一种方法计算灰度值 

public void drawImage03(Graphics g,int[][]imgArr){
        for(int i=0;i< imgArr.length;i++) {
            for (int j = 0; j < imgArr[0].length; j++) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                int R=color.getRed();
                int G=color.getGreen();
                int B=color.getBlue();
                int gray=(int) (R * 0.3 + G * 0.59 + B * 0.11);
                Color color1=new Color(gray,gray,gray);
                g.setColor(color1);
                g.fillRect(100 + i, 200 + j, 1, 1);
            }
        }
    }

*二值化

灰度值小于100为黑色

灰度值大于等于100为白色

public void drawImage04(Graphics g,int[][]imgArr){
        for(int i=0;i< imgArr.length;i++) {
            for (int j = 0; j < imgArr[0].length; j++) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                int R=color.getRed();
                int G=color.getGreen();
                int B=color.getBlue();
                int gray=(int) (R * 0.3 + G * 0.59 + B * 0.11);
                Color color1;
                if(gray<100){
                    color1=new Color(0,0,0);
                } else {
                    color1=new Color(255,255,255);
                }
                g.setColor(color1);
                g.fillRect(100 + i, 200 + j, 1, 1);
            }
        }
    }

 *圆点马赛克

与马赛克原理相同,画方改为画圆

public void drawImage05(Graphics g,int[][]imgArr){
        for(int i=0;i< imgArr.length;i+=3) {
            for (int j = 0; j < imgArr[0].length; j+=3) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                g.setColor(color);
                g.fillOval(100 + i, 200 + j, 3, 3);
            }
        }
    }

*反片

public void drawImage06(Graphics g,int[][]imgArr){
        for(int i=0;i< imgArr.length;i++) {
            for (int j = 0; j < imgArr[0].length; j++) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                int R=color.getRed();
                int G=color.getGreen();
                int B=color.getBlue();
                Color color2=new Color(255-R,255-G,255-B);
                g.setColor(color2);
                g.fillRect(100 + i, 200 + j, 1, 1);
            }
        }
    }

*轮廓化

相邻的像素点之间灰度值进行比较,差值较大就替换颜色
public void drawImage07(Graphics g,int[][]imgArr){
        for(int i=0;i< imgArr.length-1;i++){
            for(int j=0;j<imgArr[0].length-1;j++){
                int rgb1=imgArr[i][j];
                Color color1 = new Color(rgb1);
                int R=color1.getRed();
                int G=color1.getGreen();
                int B=color1.getBlue();
                int gray=(int) (R * 0.3 + G * 0.59 + B * 0.11);

                int rgb2=imgArr[i+1][j+1];
                Color color2= new Color(rgb2);
                int R2=color2.getRed();
                int G2=color2.getGreen();
                int B2=color2.getBlue();
                int gray2=(int) (R2 * 0.3 + G2 * 0.59 + B2 * 0.11);

                if(abs(gray-gray2)>10){
                    g.setColor(Color.black);
                }else{
                    g.setColor(Color.white);
                }
                g.fillRect(100 + i, 200 + j, 1, 1);
            }
        }
    }

*油画

在一定范围内随机画大小不同的椭圆,形成油画效果

 public void drawImage08(Graphics g,int[][]imgArr){
        Random random = new Random();
        for(int i = 0; i < imgArr.length; i += 3){
            for(int j = 0; j < imgArr[0].length; j += 3){
                int rgb = imgArr[i][j];
                Color color = new Color (rgb);
                int w = random.nextInt (10) + 4;
                int h = random.nextInt (10) + 4;
                g.setColor (color);
                g.fillOval (100 + i, 200 + j, w, h);
            }
        }
    }

*暖色调

R值增加图片色调会变暖,要注意R值增加后不可大于255

 public void drawImage09(Graphics g,int[][]imgArr){
        for(int i=0;i< imgArr.length;i++) {
            for (int j = 0; j < imgArr[0].length; j++) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                int R=color.getRed();
                int G=color.getGreen();
                int B=color.getBlue();
                if(R<=225){
                    R+=30;
                }
                Color color2=new Color(R,G,B);
                g.setColor(color2);
                g.fillRect(100 + i, 200 + j, 1, 1);
            }
        }
    }

*冷色调

B值增加图片色调会变冷

 public void drawImage10(Graphics g,int[][]imgArr){
        for(int i=0;i<imgArr.length;i++) {
            for (int j = 0; j < imgArr[0].length; j++) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                int R=color.getRed();
                int G=color.getGreen();
                int B=color.getBlue();
                if(B<=225){
                    B+=30;
                }
                Color color2=new Color(R,G,B);
                g.setColor(color2);
                g.fillRect(100 + i, 200 + j, 1, 1);
            }
        }
    }

1.4.完善按钮

监测按钮上的 文本并调用对应的方法(见总代码监听器部分)

2.MouseMotionListener

创建鼠标笔对象b

传入并添加MouseMotionListener   lis

        lis.b= jf.getGraphics();
        jf.addMouseMotionListener(lis);

方法mouseDragged()在鼠标拖动时调用,,并画一个圆,相当于画笔 

 public void mouseDragged(MouseEvent e) {
        int x=e.getX();
        int y=e.getY();
        for(int i=0;i<255;i++){
            Color c=new Color(i,i,i);
            b.setColor(c);
            b.fillOval(x+i/10/3-255/10/2,y+i/10/3-255/10/2,(255-i)/10,(255-i)/10);
        }
    }

 

点击清空按钮清空 

四.总代码

窗体

import javax.swing.*;
import java.awt.*;

public class Image {
    ImageListener lis=new ImageListener();
    public void showUI(){
        JFrame jf=new JFrame();
        jf.setSize(1300,800);
        jf.setTitle("美颜相机");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setLocationRelativeTo(null);
        jf.setResizable(true);
        jf.setLayout(null);

        String[] btnStrs={"加载图片", "原图", "马赛克", "灰度", "二值化", "圆点马赛克", "反片", "轮廓化", "油画", "暖色调","冷色调","清空"};
        for(int i=0;i< btnStrs.length;i++){
            JButton btn=new JButton();
            btn.setText(btnStrs[i]);
            btn.setBounds(50+i*100,30,100,30);
            jf.add(btn);
            btn.addActionListener(lis);
        }

        JLabel adressJla=new JLabel("图片地址");
        adressJla.setBounds(50,80,60,30);
        jf.add(adressJla);

        JTextField adressJtf=new JTextField(500);
        adressJtf.setBounds(150,80,500,30);
        jf.add(adressJtf);

        jf.setVisible(true);

        lis.adressJtf=adressJtf;
        lis.g= jf.getGraphics();
        lis.b= jf.getGraphics();
        jf.addMouseMotionListener(lis);
    }

    public static void main(String[] args) {
        Image ui=new Image();
        ui.showUI();
    }
}

监听器 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ImageListener implements ActionListener, MouseMotionListener {
    Graphics g,b;
    JTextField adressJtf;
    int[][]imgArr;
    int w,h;
    ImageTools imageTools=new ImageTools();

    @Override
    public void actionPerformed(ActionEvent e) {
        String btnText=e.getActionCommand();
        if(btnText.equals("加载图片")){
            String imagePath=adressJtf.getText();
            imgArr=imageTools.getImagePixs(imagePath);
            w=imgArr.length;
            h=imgArr[0].length;
        }else if(btnText.equals("原图")){
            imageTools.drawImage01(g,imgArr);
        } else if (btnText.equals("马赛克")) {
            imageTools.drawImage02(g,imgArr);
        } else if (btnText.equals("灰度")) {
            imageTools.drawImage03(g,imgArr);
        } else if (btnText.equals("二值化")) {
            imageTools.drawImage04(g,imgArr);
        } else if (btnText.equals("圆点马赛克")) {
            imageTools.drawImage05(g,imgArr);
        } else if (btnText.equals("反片")) {
            imageTools.drawImage06(g,imgArr);
        } else if (btnText.equals("轮廓化")) {
            imageTools.drawImage07(g,imgArr);
        } else if (btnText.equals("油画")) {
            imageTools.drawImage08(g,imgArr);
        } else if (btnText.equals("暖色调")) {
            imageTools.drawImage09(g,imgArr);
        } else if (btnText.equals("冷色调")) {
            imageTools.drawImage10(g,imgArr);
        } else if (btnText.equals("清空")) {
            g.setColor(Color.white);
            g.fillRect(0,150,1300,800);
        }
    }


    @Override
    public void mouseDragged(MouseEvent e) {
        int x=e.getX();
        int y=e.getY();
        for(int i=0;i<255;i++){
            Color c=new Color(i,i,i);
            b.setColor(c);
            b.fillOval(x+i/10/3-255/10/2,y+i/10/3-255/10/2,(255-i)/10,(255-i)/10);
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {

    }
}
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import static java.lang.Math.abs;

public class ImageTools {
    public int[][] getImagePixs(String imagePaths){
        File file=new File(imagePaths);
        BufferedImage buffImg=null;
        try {
            buffImg = ImageIO.read(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        int width=buffImg.getWidth();
        int height=buffImg.getHeight();
        int [][]imgArr=new int[width][height];
        for(int i=0;i<width;i++){
            for(int j=0;j<height;j++){
                imgArr[i][j]=buffImg.getRGB(i,j);
            }
        }
        return imgArr;
    }
    //原图
    public void drawImage01(Graphics g,int[][]imgArr){
        for(int i=0;i< imgArr.length;i++) {
            for (int j = 0; j < imgArr[0].length; j++) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                g.setColor(color);
                g.fillRect(100 + i, 200 + j, 1, 1);
            }
        }
    }
    //马赛克
    public void drawImage02(Graphics g,int[][]imgArr){
        for(int i=0;i< imgArr.length;i+=3) {
            for (int j = 0; j < imgArr[0].length; j+=3) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                g.setColor(color);
                g.fillRect(100 + i, 200 + j, 3, 3);
            }
        }
    }
    //灰度
    public void drawImage03(Graphics g,int[][]imgArr){
        for(int i=0;i< imgArr.length;i++) {
            for (int j = 0; j < imgArr[0].length; j++) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                int R=color.getRed();
                int G=color.getGreen();
                int B=color.getBlue();
                int gray=(int) (R * 0.3 + G * 0.59 + B * 0.11);
                Color color1=new Color(gray,gray,gray);
                g.setColor(color1);
                g.fillRect(100 + i, 200 + j, 1, 1);
            }
        }
    }
    //二值化
     public void drawImage04(Graphics g,int[][]imgArr){
        for(int i=0;i< imgArr.length;i++) {
            for (int j = 0; j < imgArr[0].length; j++) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                int R=color.getRed();
                int G=color.getGreen();
                int B=color.getBlue();
                int gray=(int) (R * 0.3 + G * 0.59 + B * 0.11);
                Color color1;
                if(gray<100){
                    color1=new Color(0,0,0);
                } else {
                    color1=new Color(255,255,255);
                }
                g.setColor(color1);
                g.fillRect(100 + i, 200 + j, 1, 1);
            }
        }
    }
    //圆点马赛克
    public void drawImage05(Graphics g,int[][]imgArr){
        for(int i=0;i< imgArr.length;i+=3) {
            for (int j = 0; j < imgArr[0].length; j+=3) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                g.setColor(color);
                g.fillOval(100 + i, 200 + j, 3, 3);
            }
        }
    }
    //反片
    public void drawImage06(Graphics g,int[][]imgArr){
        for(int i=0;i< imgArr.length;i++) {
            for (int j = 0; j < imgArr[0].length; j++) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                int R=color.getRed();
                int G=color.getGreen();
                int B=color.getBlue();
                Color color2=new Color(255-R,255-G,255-B);
                g.setColor(color2);
                g.fillRect(100 + i, 200 + j, 1, 1);
            }
        }
    }
    //轮廓化
    public void drawImage07(Graphics g,int[][]imgArr){
        for(int i=0;i< imgArr.length-1;i++){
            for(int j=0;j<imgArr[0].length-1;j++){
                int rgb1=imgArr[i][j];
                Color color1 = new Color(rgb1);
                int R=color1.getRed();
                int G=color1.getGreen();
                int B=color1.getBlue();
                int gray=(int) (R * 0.3 + G * 0.59 + B * 0.11);

                int rgb2=imgArr[i+1][j+1];
                Color color2= new Color(rgb2);
                int R2=color2.getRed();
                int G2=color2.getGreen();
                int B2=color2.getBlue();
                int gray2=(int) (R2 * 0.3 + G2 * 0.59 + B2 * 0.11);

                if(abs(gray-gray2)>10){
                    g.setColor(Color.black);
                }else{
                    g.setColor(Color.white);
                }
                g.fillRect(100 + i, 200 + j, 1, 1);
            }
        }
    }
    //油画
    public void drawImage08(Graphics g,int[][]imgArr){
        Random random = new Random();
        for(int i = 0; i < imgArr.length; i += 3){
            for(int j = 0; j < imgArr[0].length; j += 3){
                int rgb = imgArr[i][j];
                Color color = new Color (rgb);
                int w = random.nextInt (10) + 4;
                int h = random.nextInt (10) + 4;
                g.setColor (color);
                g.fillOval (100 + i, 200 + j, w, h);
            }
        }
    }
    //暖色调
    public void drawImage09(Graphics g,int[][]imgArr){
        for(int i=0;i< imgArr.length;i++) {
            for (int j = 0; j < imgArr[0].length; j++) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                int R=color.getRed();
                int G=color.getGreen();
                int B=color.getBlue();
                if(R<=225){
                    R+=30;
                }
                Color color2=new Color(R,G,B);
                g.setColor(color2);
                g.fillRect(100 + i, 200 + j, 1, 1);
            }
        }
    }
    //冷色调
    public void drawImage10(Graphics g,int[][]imgArr){
        for(int i=0;i<imgArr.length;i++) {
            for (int j = 0; j < imgArr[0].length; j++) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                int R=color.getRed();
                int G=color.getGreen();
                int B=color.getBlue();
                if(B<=225){
                    B+=30;
                }
                Color color2=new Color(R,G,B);
                g.setColor(color2);
                g.fillRect(100 + i, 200 + j, 1, 1);
            }
        }
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

.山高人为峰.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值