功能不全的美颜相机

如何做出粗糙的图片处理器

先写窗体

public void showUI(){
            JFrame jf = new JFrame();
            jf.setTitle("图像处理V1.0");
            jf.setSize(1280,800);
            jf.setLocationRelativeTo(null);//居中显示
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //设置流式布局
            FlowLayout layout=new FlowLayout();
            jf.setLayout(layout);

然后添加按钮以及按钮文本内容

可以有这些
String[]btnTexts={
                    "加载",
                    "原图",
                    "灰度化",
                    "二值化",
                    "马赛克",
                    "圆点马赛克",
                    "轮廓化",
                    "怀旧",
                    "图片融合",
                    "...."};
            for(int i=0;i<btnTexts.length;i++){
                JButton btn= new JButton(btnTexts[i]);
                btn.setBackground(Color.PINK);
                jf.add(btn);
                btn.addActionListener(imgl);
            }

在这之后设置窗体可见

jf.setVisible(true);

完成试着运行一下看窗体大小是否合适吧

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

接下来需要让这些按钮按了发挥上面文本提示的作用

先在新的类imageUtils里面试图获取一张大小合适的美丽图片
写一个用来储存图片的
int [][] imgArray;
将图片储存进去
public void getImageArray(String path){
        File file=new File(path);
        try{
        BufferedImage img=ImageIO.read(file);//读取图片
            //将图片的像素存入数组中
            int w=img.getWidth();
            int h=img.getHeight();
            //根据宽高创建数组
            imgArray=new int[w][h];
            //遍历图片获取像素 存入数组中
            for (int i=0;i<w; i++){
                for (int j=0;j<h;j++){
                    imgArray[i][j]=img.getRGB(i,j);
                }
            }
            System.out.println("加载完成!");
        }catch (IOException e){
            throw new RuntimeException(e);
        }
    }
再在新的类里面放监听器
选择图片文件的所在地址
public class imageListener implements ActionListener {
    imageUtils iu=new imageUtils();

    public void actionPerformed(ActionEvent e){
        System.out.println("点击了按钮");
        //获取按钮的文本用于判断调用了那种滤镜功能
        String opStr=e.getActionCommand();
        System.out.println("图像操作:"+opStr);
        if(opStr.equals("加载")){
            iu.getImageArray("图片地址!!!!!!!!");

现在添加画笔图片接下来的效果变化就能显示出来了

			//获取画笔
            Graphics g=jf.getGraphics();
            //传给监听器imgl中iu的g imgl.iu.addGraphics(g);
            imgl.iu.addGraphics(g);
还要在Utils里面定义一下
 Graphics g;

然后就能用了

先画个原图

public void drawImage(int x ,int y){
        int w=imgArray.length;
        int h= imgArray[0].length;
        for (int i=0;i<w;i++){
            for(int j=0;j<h;j++){
                int pixNum=imgArray[i][j];
                Color color=new Color(pixNum);
                g.setColor(color);
                g.fillRect(i+x,j+y,1,1);
            }
        }
    }

接下来在原图上改改

来个灰度化

public void gray(int x,int y){
        int w=imgArray.length;
        int h= imgArray[0].length;
        for (int i=0;i<w;i++){
            for(int j=0;j<h;j++){
                int pixNum=imgArray[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);
                g.setColor(grayColor);
                g.fillRect(i+x,j+y,1,1);
            }
        }
    }

二值化

可以直接在灰度的基础上进行一些修改
 public void binary(int x,int y){
        int w=imgArray.length;
        int h= imgArray[0].length;
        for (int i=0;i<w;i++){
            for(int j=0;j<h;j++){
                int pixNum=imgArray[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){
                    g.setColor(Color.BLACK);
                }else {
                    g.setColor(Color.WHITE);
                }
                g.fillRect(i+x,j+y,1,1);
            }
        }
    }

马赛克

可以直接在原图的基础上修改
public void mosaic(int x,int y){
        int w=imgArray.length;
        int h= imgArray[0].length;
        for (int i=0;i<w;i+=10){
            for(int j=0;j<h;j+=10){
                int pixNum=imgArray[i][j];
                Color color=new Color(pixNum);
                g.setColor(color);
                g.fillRect(i+x,j+y,10,10);
            }
        }
    }

圆点马赛克

可以直接在马赛克的基础上修改
 public void roundmosaic(int x,int y){
        int w=imgArray.length;
        int h= imgArray[0].length;
        for (int i=0;i<w;i+=10){
            for(int j=0;j<h;j+=10){
                int pixNum=imgArray[i][j];
                Color color=new Color(pixNum);
                g.setColor(color);
                g.fillOval(i+x,j+y,10,10);
            }
        }
    }

轮廓化

还是可以在灰度上面修改
public void outline(int x,int y){
        int w=imgArray.length;
        int h= imgArray[0].length;
        for (int i=0;i<w-1;i++){
            for(int j=0;j<h-1;j++){
                int pixNum=imgArray[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;
                int otPixNum=imgArray[i+1][j+1];
                Color otcolor=new Color(otPixNum);
                int otred=otcolor.getRed();
                int otgreen=otcolor.getGreen();
                int otblue=otcolor.getBlue();
                int otgray=(otred+otgreen+otblue)/3;
                if(Math.abs(gray-otgray)>=20){
                    g.setColor(Color.BLACK);
                }else {
                    g.setColor(Color.WHITE);
                }

                g.fillRect(i+x,j+y,1,1);
            }
        }
    }

老照片

仍然可以在灰度化的基础上修改
public void old(int x,int y){
        int w=imgArray.length;
        int h= imgArray[0].length;
        for (int i=0;i<w;i++){
            for(int j=0;j<h;j++){
                int pixNum=imgArray[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;
//                //颜色更改但在范围内
//                red+=20;
//                if(red>255){
//                    red=255;
//                }
                //设置为灰度色
                Color oldColor=new Color(red,green,blue/3);
                g.setColor(oldColor);
                g.fillRect(i+x,j+y,1,1);
            }
        }
    }

图片融合

还得再创建个地方储存图片
int [][] imgArray2;
然后读取图片存入数组
public void fusion(int x,int y,String path){
        File file=new File(path);
        try{
            BufferedImage img=ImageIO.read(file);//读取图片
            //将图片的像素存入数组中
            int w=img.getWidth();
            int h=img.getHeight();
            //根据宽高创建数组
            imgArray2=new int[w][h];
            //遍历图片获取像素 存入数组中
            for (int i=0;i<w; i++){
                for (int j=0;j<h;j++){
                    imgArray2[i][j]=img.getRGB(i,j);
                }
            }
            System.out.println("加载完成!");
        }catch (IOException e){
            throw new RuntimeException(e);
        }

就可以绘制了

图片的每个数值都只取一半
//绘制
        int w1=imgArray.length;
        int h1=imgArray[0].length;
        int w2=imgArray2.length;
        int h2=imgArray2[0].length;
        int mw=Math.min(w1,w2);
        int mh=Math.min(h1,h2);
        for(int i=0;i<mw;i++){
            for(int j=0;j<mh;j++){
                int pix1=imgArray[i][j];
                int pix2=imgArray2[i][j];
                Color color1=new Color(pix1);
                Color color2=new Color(pix2);
                int red1=color1.getRed();
                int green1=color1.getGreen();
                int blue1=color1.getBlue();

                int red2=color2.getRed();
                int green2=color2.getGreen();
                int blue2=color2.getBlue();

                int red=(red1+red2)/2;
                int green=(green1+green2)/2;
                int blue=(blue1+blue2)/2;

                Color color3=new Color(red,green,blue);
                g.setColor(color3);
                g.fillRect(i+x,j+y,1,1);
            }
        }
    }

完成后在监听器里面让点击的按钮能获得对应内容的产生

else if(opStr.equals("原图")){
            iu.drawImage(100,100);
        }else if (opStr.equals("灰度化")){
            iu.gray(450,100);
        }else if (opStr.equals("二值化")){
            iu.binary(800,100);
        }else if (opStr.equals("马赛克")){
            iu.mosaic(100,350);
        }else if (opStr.equals("圆点马赛克")){
            iu.roundmosaic(450,350);
        }else if (opStr.equals("轮廓化")){
            iu.outline(800,350);
        }else if (opStr.equals("怀旧")){
            iu.old(100,600);
        }else if (opStr.equals("图片融合")){
            iu.fusion(450,600,"你选的另一张图片!!!!!!!!!");
        }

这样粗糙的图片处理器就做好了:D

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值