【浅谈java图片原理与处理】

【浅谈java图片原理与处理】,第1张

【浅谈java图片原理与处理】

有没有想过,生活中的图片,是怎样呈现在电脑屏幕上的?

实际上,电脑屏幕上所呈现的内容,都是由计算机二进制的0与1处理而来。

——————第一部分——————

一、像素

首先,我们熟知像素是图片组成的要素。我们看到的图片经过放大,可以看出“马赛克”,即每一个小格子都为一种颜色。即三原色。每个格子对应一个像素值。

像素:是指在由一个数字序列表示的图像中的一个最小单位,称为像素。

例如手机像素几千万,4K画质,分辨率1080p,1960*1080,即屏幕的长与宽。

我们熟悉的数据类型,有bit(二进制),byte(字节),short(短整型),int(整型),long(长整型);

二、三原色

我们所说的三原色:红色、绿色、蓝色,在java中以RGB来进行处理。

一个个三原色像素点,组成了一幅图片,r -red,g-green,b-blue;

每个像素值在0-255*255*255之间,我们用R,G,B三个值来表示一个颜色。

颜色的区间为0-255,三个颜色的组合又形成不同的颜色。

比如像素值16,581,375写成(255,255,255),对应白色。

像素值0写成(0,0,0),对应黑色。

三原色之间比例的组合,可以实现生成生活中的其他颜色,

三、储存图像

在java里,我们可以用二维数组来存储图片。

将每一行每一列对应的像素点储存在二维数组中的arr[i][j],对应位置中。

——————第二部分——————

一、处理图像(用代码实现)

Jframe的相关信息可以参考这位大佬:Jframe的使用方法_oldsweet老甜甜的博客-CSDN博客_jframe

写一个处理图像的类ImagePad,继承Jframe,显示界面,储存图像,显示图像的相关 *** 作方法均放在该类。

public class ImagePad extend Jframe(){
    
public static void main(String[] args)
	{ 
		ImagePad imgpad = new ImagePad (); 
	
		imgpad.initUI (); 
	}

        

public void initUI(){ 
	
		//设置标题
        this.setTitle ("图像处理"); 
		
		this.setSize (800, 800); 
		
		this.setDefaultCloseOperation (Jframe.EXIT_ON_CLOSE); 
		
        //在可视化之后获取
		this.setVisible (true); 
		}

   

    

    public int[][] getPixelArray(String imgPath){ 
		
		// 根据图片地址 生成一个file对象 ,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; }
		
	//————————————————————————————————————————————————————————————————————————————————
     
    public void paint(Graphics g)
	{ 
		super.paint (g);  
        //申请一个二维数组,用来储存图片信息
      int[][] imgarr = getPixelArray ("此处填写文件路径");    
               //示例:                     路径\文件名.后缀
               // ("C:\Users\Desktop\picture\picture2.jpeg"); 
		
		int w = imgarr.length; 
		
		int h = imgarr[0].length; 
		
         
        public void drawImage_01(int[][] imgarr,Graphics g) {

		for(int i=0;i

 

接下来可以换其他的方法

第二种:灰度图

 
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 blue = color.getBlue();
				int green = color.getGreen();
				
				int gray = (red+blue+green)/3;
				Color ncolor = new Color(gray,gray,gray);			
			    
                g.setColor(ncolor);  	
				g.fillRect(100+i,100+j,1,1); 
    }
}
 

 

第三种:二值化图

 
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 blue = color.getBlue();
				int green = color.getGreen();
				
				int gray = (red+blue+green)/3;
				Color ncolor = new Color(gray,gray,gray);
			
				// 二值化 在灰度的基础上 通过灰度值来判断  
				if(gray < 80){ 
					g.setColor (Color.BLACK); 
					} 			
                else{ 
					g.setColor (Color.WHITE); 
					}
				g.fillRect (100 + i, 100 + j, 1, 1);
			
			}	
		}
	}  

                                                 

 

二、分部剖析详解:

        1、主函数部分,创建一个类对象,用类对象创建成员方法

//主函数部分
public static void main(String[] args) {
		ImagePad imgPad = new ImagePad();
		imgPad.intUI();
	}

       2、生成函数界面部分,直接用this来调用方法设置标题,设置大小,设置退出方法,设置可视化;

        

public void initUI(){ 
	
		//设置标题
        this.setTitle ("图像处理"); 
		
		this.setSize (800, 800); 
		
		this.setDefaultCloseOperation (Jframe.EXIT_ON_CLOSE); 
		
        //在可视化之后获取
		this.setVisible (true); 
		}

        3、将图像转换为二维数组的方法部分


    public int[][] getPixelArray(String imgPath){ 
		
		// 根据图片地址 生成一个file对象 ,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; }

        4、

 
public void paint(Graphics g)
	{ 
		super.paint (g);  
        //申请一个二维数组,用来储存图片信息
      int[][] imgarr = getPixelArray ("此处填写文件路径");    
               //示例:                     路径\文件名.后缀
               // ("C:\Users\Desktop\picture\picture2.jpeg"); 
		
		int w = imgarr.length; 
		
		int h = imgarr[0].length; 
		
         
        public void drawImage_01(int[][] imgarr,Graphics g) {

		for(int i=0;i

总结:

在界面上显示出图片,原理为导入图片文件,获取图片的高和宽,将每一个像素点存在数组中南,将图片文件转换为二维数组,最后利用绘制图片的方法将图片绘制出。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5712401.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存