Java美颜相机2.0-按钮切换图像滤镜-封装

为实现按钮换滤镜我们要修改界面,在显示界面添加各种按钮

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

public class lvjing2 extends JFrame {
    imageListener il = new imageListener ();
    int width=800,height=800;
    public void showUI(){
        setTitle("图像处理v3.0");
        FlowLayout flowLayout=new FlowLayout();
        setLayout(flowLayout);
        setSize(width,height);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(true);
        String[] buttonStr = { "打开","保存","加载图片", "原图", "马赛克", "灰度", "二值化", "圆点马赛克","反片",
                "轮廓化", "油画", "怀旧",  "画笔", "贴图", "截图"};
        for(int i = 0; i < buttonStr.length; i++){
            JButton btn = new JButton ();
            btn.setText (buttonStr[i]);
            btn.setBackground (Color.WHITE);
            this.add (btn);
            btn.addActionListener (il);
        }
        setVisible (true);
        Graphics g = this.getGraphics ();
        il.g = g;
    }

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

再将各个按钮对应的功能写进actionlistener中

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class imageListener implements ActionListener {
    Graphics g;
    int[][] imgArr;
    int w;
    int h;
    // 创建一个图片工具对象
    ImageTools imageTools = new ImageTools ();
    @Override
    public void actionPerformed(ActionEvent e){
        String ac = e.getActionCommand ();
        System.out.println ("点击了:" + ac);
        if(ac.equals ("加载图片")){
            imgArr = imageTools.getImagePixs
                    ("D:\\Java其他文件\\llz230930\\23.9.30-2.jpg");
            w = imgArr.length;
            h = imgArr[0].length;
        } else if(ac.equals ("原图")){
            imageTools.drawImage01 (g, imgArr);
        } else if(ac.equals ("马赛克")){
            imageTools.drawImage02 (g, imgArr);
        } else if(ac.equals ("圆点马赛克")){
            imageTools.drawImage03 (g, imgArr);
        } else if(ac.equals ("灰度")){
            imageTools.drawImage04 (g, imgArr);
        } else if(ac.equals ("二值化")){
            imageTools.drawImage05 (g, imgArr);
        } else if(ac.equals ("反片")){
            imageTools.drawImage06 (g, imgArr);
        } else if(ac.equals ("怀旧")){
            imageTools.drawImage07 (g, imgArr);
        } else if(ac.equals ("油画")){
            imageTools.drawImage08 (g, imgArr);
        } else if(ac.equals ("轮廓化")){
            imageTools.drawImage09 (g, imgArr);
        }
    }
}

为了让ActionListener中的代码简洁,我们采用分装,将各个滤镜要处理输出rgb值的代码写入另外创建的ImageTools中,当我们要用到这个tool时再用imageTools.***的格式调用对应的代码

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
public class ImageTools {
    // 读取图片的像素
// 封装好的图片路径转二维像素数组的方法
// 输入: 图片路径
// 输出: 一张图片的二维数组
    public int[][] getImagePixs(String imagePath){
// File BufferedImage ImageIO
// 根据传入的参数路径 创建一个File对象
        File file = new File(imagePath);
// 创建一个BufferedImage 对象变量名 ,切记不要创建对象
        BufferedImage buffImg = null;
//使用ImageIO 直接调用read方法 读取File对象
// 将读取的返回值数据对象存入buffimg中
        try {
            buffImg = ImageIO.read(file);
// 异常处理机制: 程序运行时可能会遇到一些特殊情况
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
// 获取buffImg的宽高 创建一个空的二维数组
        int width = buffImg.getWidth();
        int height = buffImg.getHeight();
        int[][] imgArr = new int[width][height];
// 双重循环遍历 将buffImg中的像素值取出来存入数组中
        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){
        BufferedImage bufferedImage = new BufferedImage (imgArr.length,
                imgArr[0].length, BufferedImage.TYPE_INT_ARGB);
        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);
                bufferedImage.setRGB (i, j, color.getRGB ());
            }
        }
        g.drawImage (bufferedImage, 100, 100, null);
    }
    // 马赛克
    public void drawImage02(Graphics g, int[][] imgArr){
        int a=10,b=10,c=-1,d;
        for(int i = 0; i < imgArr.length; i += 10){
            c+=1;
            d=-1;
            b=10;
            if(i+10>imgArr.length){a=imgArr.length-c*10;}
            for(int j = 0; j < imgArr[0].length; j += 10){
                d+=1;
                if(j+10>imgArr[0].length){b=imgArr[0].length-d*10;}
                int rgb = imgArr[i][j];
                Color color = new Color (rgb);
                g.setColor (color);
                g.fillRect (100 + i, 100 + j, a, b);
            }
        }
    }
    // 圆点马赛克
    public void drawImage03(Graphics g, int[][] imgArr){
        int a=10,b=10,c=-1,d;
        for(int i = 0; i < imgArr.length; i += 10){
            c+=1;
            d=-1;
            b=10;
            if(i+10>imgArr.length){a=imgArr.length-c*10;}
            for(int j = 0; j < imgArr[0].length; j += 10){
                d+=1;
                if(j+10>imgArr[0].length){b=imgArr[0].length-d*10;}
                int rgb = imgArr[i][j];
                Color color = new Color (rgb);
                g.setColor (color);
                g.fillOval (100 + i, 100 + j, a, b);
            }
        }
    }
    // 灰度
    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 red = color.getRed ();
                int green = color.getGreen ();
                int blue = color.getBlue ();
                int gray = (red + blue + green) / 3;
                Color color1 = new Color (gray, gray, gray);
                g.setColor (color1);
                g.fillRect (100 + i, 100 + j, 1, 1);
            }
        }
    }
    // 二值化
    public void drawImage05(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 red = color.getRed ();
                int green = color.getGreen ();
                int blue = color.getBlue ();
                int gray = (red + blue + green) / 3;
                if(gray < 150){
                    g.setColor (Color.BLACK);
                } else{
                    g.setColor (Color.WHITE);
                }
                g.fillRect (100 + i, 100 + j, 1, 1);
            }
        }
    }
    // 反片
    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 red = color.getRed ();
                int green = color.getGreen ();
                int blue = color.getBlue ();
                Color color1 = new Color (255 - red, 255 - green, 255 - blue);
                g.setColor (color1);
                g.fillRect (100 + i, 100 + j, 1, 1);
            }
        }
    }
    // 老照片 暖色
    public void drawImage07(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 red = color.getRed ();
                int green = color.getGreen ();
                int blue = color.getBlue ();
                int nred = red + 25;
                int ngreen = green + 25;
                if(nred > 255) nred = 255;
                if(ngreen > 255) ngreen = 255;
                Color color1 = new Color (nred, ngreen, blue);
                g.setColor (color1);
                g.fillRect (100 + i, 100 + 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, 100 + j, w, h);
            }
        }
    }
    // 轮廓化 相邻的像素点之间进行比较 差值较大 就替换颜色
    public void drawImage09(Graphics g, int[][] imgArr){
        for(int i = 0; i < imgArr.length - 2; i++){
            for(int j = 0; j < imgArr[0].length - 2; j++){
                int rgb = imgArr[i][j];
                Color color = new Color (rgb);
                int red = color.getRed ();
                int green = color.getGreen ();
                int blue = color.getBlue ();
                int gray = (red + blue + green) / 3;
                int rgb1 = imgArr[i + 2][j + 2];
                Color color1 = new Color (rgb1);
                int red1 = color1.getRed ();
                int green1 = color1.getGreen ();
                int blue1 = color1.getBlue ();
                int gray1 = (red1 + blue1 + green1) / 3;
// 判断两个颜色是否有差值
                if(Math.abs (gray1 - gray) > 10){
                    g.setColor (Color.BLACK);
                } else{
                    g.setColor (Color.WHITE);
                }
                g.fillRect (100 + i, 100 + j, 1, 1);
            }
        }
    }
}

以下为各种滤镜效果图

原图: 

马赛克:

灰度:

二值化:

圆点马赛克:

反片:

轮廓化:

油画:

怀旧:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值