【无标题】美颜相机的初步开发

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 ImageUI extends JFrame {
    public void showUI() {
        setTitle("美颜相机");
        setSize(1200, 800);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        FlowLayout fl = new FlowLayout();
        setLayout(fl);
        JButton button1 = new JButton("功能1");
        button1.setBounds(20, 20, 100, 35);
        add(button1);


    }
//创建画笔和二维数组,将图片的各像素值存入二维数组中,再用循环让画笔在窗口中画出
    public void paint(Graphics g) {
        super.paint(g);
        int[][] imgArr = getImagePix("C:\\Users\\husheng\\Desktop\\dd1361becb3621b78892e1effac07b80.jpg");
        int width = imgArr.length;
        int high = imgArr[0].length;
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < high; j++) {
                int rgb = imgArr[i][j];
                Color color = new Color(rgb);
                g.setColor(color);
                g.fillRect(100 + i, 100 + j, 1, 1);

            }
        }
//循环读取二维数组,rgb变量存储数组中rgb值,用方法读取数中red,green,blue的值
再对颜色进行调节,达到渲染的目的。以下是反色代码
        for (int i = 0; i < width; i += 1) {
            for (int j = 0; j < high; 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(100 + i, 100 + j, 1, 1);
            }
        }
    }
//创建二维数组,用file读取文件信息,并写方法读取图片中的像素值,最后存入二维数组中
    public int[][] getImagePix(String imagePath) {
        File file = new File(imagePath);
        BufferedImage img = null;
        try {
            img = ImageIO.read(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        int width = img.getWidth();
        int high = img.getHeight();
        int[][] imgSArr = new int[width][high];
        for (int i = 0; i < width; i += 1) {
            for (int j = 0; j < high; j += 1) {
                imgSArr[i][j] = img.getRGB(i, j);

            }
        }
        return imgSArr;
    }


    public static void main(String[] args) {
        ImageUI imageUI = new ImageUI();
        imageUI.showUI();
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值