Java图片原理与处理实现滤镜效果

喜欢拍照的友友们,肯定对各种修图、美颜、滤镜很感兴趣。今天我就简单地给大家讲一下图片原理和用Java编程实现的一些滤镜效果。

图片是个二维结构,有像素、分辨率、大小、颜色、色调、亮度等基本属性。其中像素个数就是图片的宽和高的乘积。我们要绘制图片,需要学会如何绘制像素点及如何设置颜色RGB。

// 绘制随机的一个颜色矩阵并存储起来 
        Random random = new Random ();
        int[][] imgarr = new int[200][200];
        for(int i = 0; i < 200; i++){
            for(int j = 0; j < 200; j++){
                // 随机一个(0-16777214)范围的整数
                int randomRGB= random.nextInt (16777215);
                imgarr[i][j]=randomRGB;
                // 如何绘制一个像素点
                Color color = new Color (randomRGB);
                g.setColor (color);
                g.fillRect (100+i,100+j,1,1);
            }
        }

        // 遍历数组 取出所有的数设置为颜色再绘制出来
        for(int i = 0; i < 200; i++){
            for(int j=0;j<200;j++){
                int  rgb =  imgarr[i][j];
                Color color = new Color (rgb);
//              int blue = (rgb>>0)&0xFF;
                // 获取RGB三原色
                int red = color.getRed ();
                int green = color.getGreen ();
                int blue = color.getBlue ();
                Color nColor = new Color (red/2,green/2,blue/2);
                g.setColor(nColor);
                g.fillRect (350+i,100+j,1,1);
            }
        }

我们要对图片进行一些属性的修改,就需要获取有关图片数据,而图片数据就是int类型二维数组。所以当我们获取一张照片时,我们需要将其转为一个int类型的二维数组。那如何将一张照片转为一个int类型的二维数组呢?

​
/*给定一张照片 将其转为一个int类型的二维数组
	@param imgpath 给定的照片路径
	@param return1返回int类型二维数组*/
	public int[][] getPixelArray(String imgPath){
		 // 根据图片地址 生成一个file对象
		 File file = new File (imgPath);
		 // 声明一个BUfferedImage变量名
		 BufferedImage buffimg = null;
		 // 使用图片IO读取文件
		 try {
			 
		 buffimg = ImageIO.read (file);
		 
		 }catch (IOException e) {
		 e.printStackTrace ();
		 }
		 
		 int w = buffimg.getWidth();
		 int h = buffimg.getHeight();
		// 定义一个与图片像素宽高一致的二维数组
	     int[][] imgarr = new int[w][h];
	    // 遍历BufferedImage 获取RGB值 存储 数组中
	     for(int i = 0; i < w; i++){
	         for(int j = 0; j < h; j++){
	         imgarr[i][j] = buffimg.getRGB (i, j);
	         }
	     }
	     //将存好像素值的数组 返回
	     return imgarr;
	}

将照片转为int类型的二维数组后,我们需要遍历绘制所有的像素值。

//重新读取图片
	int[][] imgarr = getPixelArray ("C:\\user\\311\\Pictures\\toux.jpg");
	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);
            //获取RGB
            int red=color.getRed();
			int green=color.getGreen();
			int blue=color.getBlue();
			Color ncolor=new Color(red,green,blue);
			g.setColor(ncolor);
			g.fillRect(100+i,100+j,1,1);
        }
    }
    

处理像素值,调整RGB颜色,实现多种滤镜效果

一灰度

int[][] imgarr = getPixelArray ("C:\\user\\311\\Pictures\\toux.jpg");
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);
		int red=color.getRed();
		int green=color.getGreen();
		int blue=color.getBlue();
		int gray=(red+green+blue)/3;
		Color ncolor=new Color(gray,gray,gray);
		g.setColor(ncolor);
		g.fillRect(100+i,100+j,1,1);
    }
}

                                                                     

 二黑白

int[][] imgarr = getPixelArray ("C:\\user\\311\\Pictures\\toux.jpg");
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);
        int red=color.getRed();
		int green=color.getGreen();
		int blue=color.getBlue();
		int gray=(red+green+blue)/3;
		if(gray<=100) {
		    Color ncolor=new Color(0,0,0);
			g.setColor(ncolor);
			g.fillRect(100+i,100+j,1,1);
		}else {
			Color ncolor=new Color(255,255,255);
			g.setColor(ncolor);
			g.fillRect(100+i,100+j,1,1);
		}
    }
}

 

三底片

int[][] imgarr = getPixelArray ("C:\\user\\311\\Pictures\\toux.jpg");
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);
        int red=255-color.getRed();
		int green=255-color.getGreen();
		int blue=255-color.getBlue();
		Color ncolor=new Color(red,green,blue);
		g.setColor(ncolor);
		g.fillRect(100+i,100+j,1,1);
    }
}

 

完整的代码如下:

import javax.swing.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.*;
import java.util.Random;
public class Imagepad extends JFrame {
	public void init() {
		this.setTitle("图像处理");
		this.setSize(800,800);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	public static void main(String[]args) {
		Imagepad imgpad=new Imagepad();
		imgpad.init();
	}

	public int[][] getPixelArray(String imgPath){
		 // 根据图片地址 生成一个file对象
		 File file = new File (imgPath);
		 // 声明一个BUfferedImage变量名
		 BufferedImage buffimg = null;
		 // 使用图片IO读取文件
		 try {
			 
		 buffimg = ImageIO.read (file);
		 
		 }catch (IOException e) {
		 e.printStackTrace ();
		 }
		 
		 int w = buffimg.getWidth();
		 int h = buffimg.getHeight();
		// 定义一个与图片像素宽高一致的二维数组
	     int[][] imgarr = new int[w][h];
	    // 遍历BufferedImage 获取RGB值 存储 数组中
	     for(int i = 0; i < w; i++){
	         for(int j = 0; j < h; j++){
	         imgarr[i][j] = buffimg.getRGB (i, j);
	         }
	     }
	     //将存好像素值的数组 返回
	     return imgarr;
	}
	//重新读取图片
	int[][] imgarr = getPixelArray ("C:\\user\\311\\Pictures\\toux.jpg");
	int w = imgarr.length;
    int h = imgarr[0].length;
    
    public void paint(Graphics g) {
		super.paint(g);
		 // 遍历数组 取出所有的数设置为颜色再绘制出来
		for(int i=0;i<w;i++) {
			for(int j=0;j<h;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+green+blue)/3;
				Color ncolor=new Color(gray,gray,gray);
				g.setColor(ncolor);
				g.fillRect(100+i,100+j,1,1);*/
				//图片色调设置为黑白,二值化
				/*int red=color.getRed();
				int green=color.getGreen();
				int blue=color.getBlue();
				int gray=(red+green+blue)/3;
				if(gray<=100) {
					Color ncolor=new Color(0,0,0);
					g.setColor(ncolor);
					g.fillRect(100+i,100+j,1,1);
				}else {
					Color ncolor=new Color(255,255,255);
					g.setColor(ncolor);
					g.fillRect(100+i,100+j,1,1);
				}*/
				//图片滤镜设置为底片效果
				int red=255-color.getRed();
				int green=255-color.getGreen();
				int blue=255-color.getBlue();
				Color ncolor=new Color(red,green,blue);
				g.setColor(ncolor);
				g.fillRect(100+i,100+j,1,1);
				
			}
	    }	
	}
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值