用Java制作一个简单的图片处理器

package com.image_processor;


import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.Buffer;

public class image extends JFrame{
    public void showUI(){
        setTitle ("图像处理v2.0");
        setSize (2000, 1100);
        setDefaultCloseOperation (EXIT_ON_CLOSE);
        setVisible (true);
    }//建立主界面

    public void paint(Graphics g){
        super.paint (g);
        int[][] imgArr = getImagePixs ("C:\\Users\\HP\\Pictures\\Camera Roll\\background.png");
        //一个读取照片的类,用于将照片中像素点转化为数字存入二维数组中
        int w = imgArr.length;
        int h = imgArr[0].length;
        //将原图打印出来,检验代码可操作性
        for(int i = 0; i < w; i++){
            for(int j = 0; j < h; j++){
                int rgb = imgArr[i][j];
                Color color = new Color (rgb);
                g.setColor (color);
                g.fillRect (100 + i, 30 + j, 1, 1);
            }
        }
// 马赛克效果(主要原理是像素点面积变大)
        for(int i = 0; i < w; i += 10){
            for(int j = 0; j < h; j += 10){
                int rgb = imgArr[i][j];
                Color color = new Color (rgb);
                g.setColor (color);
                g.fillRect (900 + i, 30 + j, 10, 10);
            }
        }
// 灰度(调整红绿蓝的占比实现不同灰度)
        for(int i = 0; i < w; i += 1){
            for(int j = 0; j < h; j += 1){
                int rgb = imgArr[i][j];
                Color color = new Color (rgb);
                int red = color.getRed ();
                int green = color.getGreen ();
                int blue = color.getBlue ();

                int gray = (int) (red * 0.30 + green * 0.59 + blue * 0.11);
                Color color1 = new Color (gray, gray, gray);

                g.setColor (color1);
                g.fillRect (900 + i, 30 + j, 1, 1);
            }
        }
// 二值化(将灰度小于100的全部变为黑,大于100的全部变为白)
        for(int i = 0; i < w; i += 1){
            for(int j = 0; j < h; j += 1){
                int rgb = imgArr[i][j];
                Color color = new Color (rgb);
                int red = color.getRed ();
                int green = color.getGreen ();
                int blue = color.getBlue ();

                int gray = (int) (red * 0.30 + green * 0.59 + blue * 0.11);

                if(gray < 100){
                    g.setColor (Color.BLACK);
                } else{
                    g.setColor (Color.WHITE);
                }
                g.fillRect (900 + i, 30 + j, 1, 1);
            }
        }
// 反片(将色素对应的值去补数操作得到反片)
        for(int i = 0; i < w; i += 1){
            for(int j = 0; j < h; j += 1){
                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 (900 + i, 30 + j, 1, 1);
            }
        }


    }
    // 读取图片的像素
    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 static void main(String[] args){
        com.exercise.exercise2 imageProUI = new com.exercise.exercise2();
        imageProUI.showUI ();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值