简易图片处理(java)

图片读取和存储

原理:将图片看作一个二维数组p[][],则p[i][j]对应着相应像素的颜色,将颜色值读取并存储即可。

public int[][] getimg(String fname){//fname为文件名
        File file = new File(fname);//打开图片文件
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(file);//读取图片
        }catch (Exception e){
            e.printStackTrace();
        }

        int w = bi.getWidth();
        int h = bi.getHeight();
        int[][] imgIndex = new int[w][h];
        for(int i = 0; i < w;i ++){
            for(int j = 9; j < h;j ++){
                int pixel ;
                pixel = bi.getRGB(i,j);//取得(i,j)位置Color的rgb值
                imgIndex[i][j] = pixel;//将rgb值存储
            }
        }
        return imgIndex;
    }

图片绘制

根据上述读取和存储图片所得的数据,对像素(i,j)进行涂色即可

public void loadimg(Graphics g){
        int[][] data = getimg("p1.png");
        Color c ;
        for(int i = 0 ; i < data.length ; i++){
            for(int j = 0 ; j < data[i].length ; j++){
                c = new Color(data[i][j]);
                int rr = c.getRed();
                int rg = c.getGreen();
                int rb = c.getBlue();
                g.setColor(new Color(rr,rg,rb));
                g.fillOval(i,j,1,1);//相当于对对应的像素点涂色
            }
        }
    }

图片处理

在图片绘制的基础上,改变绘制图片的法则,就可以对图片进行简单处理

public void loadimg(Graphics g){
        int[][] data = getimg("p1.png");
        Color c ;
        for(int i = 0 ; i < data.length ; i++){
            for(int j = 0 ; j < data[i].length ; j++){
                c = new Color(data[i][j]);
                int rr = c.getRed();
                int rg = c.getGreen();
                int rb = c.getBlue();
                g.setColor(new Color(rr/2,rg/2,rb/2));//将颜色的r,g,b值同除以一个数,绘制出的图片亮度会变低
                g.fillOval(i/2,j/2,1,1);//若图片太大,框体无法展示完全,将i,j同除以一个数,可以对图片进行缩小和放大
            }
        }
	]

完整代码

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;

public class Pui extends JFrame {
    Graphics g;
    public void initUI(){
        this.setSize(1000,800);
        this.setTitle("Picture");
        this.setDefaultCloseOperation(3);
        FlowLayout fl =new FlowLayout();
        this.setLayout(fl);
        this.setVisible(true);
        JButton btn = new JButton();
        btn.setText("Start");
        this.add(btn);
        g = this.getGraphics();
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e != null){
                    loadimg(g);
                }
            }
        });

    }
    public void loadimg(Graphics g){
        int[][] data = getimg("p1.png");
        Color c ;
        for(int i = 0 ; i < data.length ; i++){
            for(int j = 0 ; j < data[i].length ; j++){
                c = new Color(data[i][j]);
                int rr = c.getRed();
                int rg = c.getGreen();
                int rb = c.getBlue();
                //if(rr > 180)g.setColor(Color.WHITE);
                //else {
                //    g.setColor(Color.BLACK);
                //}
                g.setColor(new Color(rr/2,rg/2,rb/2));
                g.fillOval(i/2,j/2,2,2);
            }
        }
    }
    public int[][] getimg(String fname){
        File file = new File(fname);
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(file);
        }catch (Exception e){
            e.printStackTrace();
        }

        int w = bi.getWidth();
        int h = bi.getHeight();
        int[][] imgIndex = new int[w][h];
        for(int i = 0; i < w;i ++){
            for(int j = 9; j < h;j ++){
                int pixel ;
                pixel = bi.getRGB(i,j);
                imgIndex[i][j] = pixel;
            }
        }
        return imgIndex;
    }public static void main(String[] args) {
        new Pui().initUI();
        System.out.println("Hello world!");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值