对于该项目我们首先明确实现它的整体思路:
1.首先给定一张照片
2.将照片的每一个像素点的rgb值存放在一个二维数组里
3.再将照片的像素点显示出来
首先创建一个新的类来继承JFrame从而获得JFrame方法,在接下来要通过JFrame来获得相关功能,创建窗体并设置窗体基本信息:
public class ImagePad extends JFrame { //创建一个子类继承JFrame从而得到JFrame其中方法 public static void main(String args[]){ //主函数通过对象调用窗体方法 ImagePad ip=new ImagePad(); ip.initUI(); } public void initUI(){ //此处通过调用JFrame中的方法创建一个窗体 //设置窗体名称,尺寸,开关 this.setTitle("图像处理"); this.setSize(2000,1000); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); }
然后我们实现将照片保存,这里我们采用二维数组,大致思路是将照片的每一个像素点的顺序以及rgb值保存在一个二维数组里,在读取照片时,我们采用文件流的方式,在这里我们需要注意到,我们保存图片的意义是方便使用它,因此要注意将返回值设定好是我们保存后的二维数组,供我们后续盖面照片颜色参数使用。
public int[][] getimagdata(String imgPath){
File file=new File(imgPath);
BufferedImage buffing=null;
try{
buffing=ImageIO.read(file)
; }
catch(IOException e){
e.printStackTrace();
}
//获得照片尺寸
int w=buffing.getWidth();
int h=buffing.getHeight();
int[][]imagarr=new int[w][h];
//通过一个二维数组同时记录出每一个像素的位置和颜色比例
for(int i=0;i<w;i++){
for(int j=0;j<h;j++){
imagarr[i][j]=buffing.getRGB(i, j);
}
}
return imagarr;
那么接下来我们要将图片重新弄出来,思路是我们已经将每一个像素点的顺序以及rgb值保存了下俩,那么我们就可以将每一个像素点按顺序传出来,并通过Color类去改变每一个像素点的rgb值,从而达到我们想要的效果;
//继承父类中窗体获取画笔方法
//super.paint(g);
//此处调用图像数据获得方法
int [][]imagarr1=getimagdata("C:\\Users\\86177\\Pictures\\Saved Pictures\\untitled.png");
//得到二维数组行数
int w=imagarr1.length;
//得到二维数组列数
int h=imagarr1[0].length;
for(int i=0;i<w;i++){
for(int j=0;j<h;j++){
//
int rgb=imagarr1[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 ocolor=new Color(red,green,blue);
Color gcolor=new Color(gray,gray,gray);
Color ncolor=new Color(red,green*4/5,blue*4/5);
g.setColor(ncolor);
g.fillRect(800+i, 100+j, 1, 1);
g.setColor(ocolor);
g.fillRect(100+i, 100+j, 1, 1);
}
}
在这里我仅显示了我认为对该图片颜色处理的最好效果:
右侧为该图片处理后结果,经思考,不同位置的颜色经过统一处理后达到的效果并不尽如人意,那么在接下来的过程中我们将实现不同位置的具体处理。