图形处理规划
界面:
监听器:
图像处理类:
功能:
- 基础滤镜处理 - R G B配置
- 画图功能 - 改变图片部分区域
- 图片 存储 打开
- 图层 - 撤回 录制 回放
- 缩放 矩阵线性插值 矩阵映射
- 卷积 - 哈哈镜 高斯模糊 锐化
- 二维码编码生成
- 以图搜图
- 视频处理
点击按钮 调用图片处理效果
- 把图片转成二维数组:
- 图片 --> BufferedImage --> 二维数组
- ImageIO.read();
- BufferedImage – 缓冲图片类
- Graphics 绘制像素
try {
BufferedImage buffimg = ImageIO.read(file);
} catch (IOException e) {
}
二维数组:还是一维数组 – 内存条 内存是线性结构
int[] arr = new int[10]; {0,0,0,0,0,0,0,0} int[][] arr2 = new int[10][10];
int[][] arr3={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}};
arr3.length -- 有多少个一维数组arr3[i].length 每个一维数组有多少个元素int[] arr1_1 = arr3[2];
{0,0,0,0
0,0,0,0
0,0,0,0
}
完善图像处理效果
public class ImageUI extends JFrame{
String[] btnstrs = new String[]{
"原图","灰度","二值化","马赛克","油画","轮廓检测","撤回", "画笔","放大","缩小","RGB"
};
ImageListener imageListener = new ImageListener ();
public void addButtons(JFrame jf, ActionListener al){ Dimension dim = new Dimension(120,55);
Font font = new Font ("黑体",Font.BOLD,22);
for(int i = 0; i < btnstrs.length; i++){ JButton btn = new JButton (btnstrs[i]); btn.addActionListener (al); btn.setBackground (Color.WHITE); btn.setFont (font); btn.setPreferredSize (dim);
jf.add (btn);
}
}
public void initUI(){
setTitle ("图形图像处理编程"); setSize (800,800);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); setLayout (new FlowLayout ()); setLocationRelativeTo (null); addButtons(this,imageListener);
setVisible (true);
}
public static void main(String[] args){ new ImageUI ().initUI ();
}
}
package image.zyf0926;
import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class ImageListener implements ActionListener{ ImageEff imageEff = new ImageEff ("img/img02.jpeg");
Graphics g;
public void setGraphics(Graphics g){this.g = g;}
public void actionPerformed(ActionEvent e){ System.out.println (e.getActionCommand ());
String btnstr = e.getActionCommand();
if(btnstr.equals (" 原 图 ")){ imageEff.drawImage_01 (g);
}else if(btnstr.equals ("灰度")){ imageEff.drawImage_02(g);
}else if(btnstr.equals ("马赛克")){ imageEff.drawImage_03(g);
}else if(btnstr.equals ("二值化")){ imageEff.drawImage_04(g);
}else if(btnstr.equals ("油画")){ imageEff.drawImage_05(g);
}else if(btnstr.equals ("轮廓检测")){ imageEff.drawImage_06(g);
}
}
}
package image.zyf0926;
import javax.imageio.ImageIO; import java.awt.*;
import java.awt.image.BufferedImage; import java.io.File;
import java.io.IOException; import java.util.Random;
public class ImageEff{ int[][] imgarr;
ImageEff(String path){
imgarr = imgeToArray(path);
}
public int[][] imgeToArray(String path){
File file = new File(path);
buffimg = ImageIO.read(file);
} catch (IOException e) { e.printStackTrace ();
}
int[][] imgData = new int[w][h];
for(int i = 0; i < imgData.length; i++){ for(int j = 0; j < imgData[0].length; j++){
imgData[i][j] = buffimg.getRGB(i,j);
}
}
return imgData;
}
public void drawImage_01(Graphics g){ System.out.println ("绘制原图!!!");
for(int i = 0; i < imgarr.length; i++){ for(int j = 0; j< imgarr[i].length; j++){
int rgb = imgarr[i][j];
Color color = new Color (rgb); g.setColor (color); g.fillRect (i,j+120,1,1);
}
}
}
public void drawImage_02(Graphics g){ System.out.println ("绘制灰度图 !!!");
for(int i = 0; i < imgarr.length; i++){ for(int j = 0; j< imgarr[i].length; j++){
int rgb = imgarr[i][j];
int green = (rgb>>8)&0xFF; int blue = (rgb>>0)&0xFF;
int gray = (red+green+blue)/3;
Color color = new Color (gray,gray,gray); g.setColor (color);
g.fillRect (i,j+120,1,1);
}
}
}
public void drawImage_03(Graphics g){ for(int i = 0; i < imgarr.length; i+=8){
for(int j = 0; j< imgarr[i].length; j+=8){ int rgb = imgarr[i][j];
Color color = new Color (rgb); g.setColor (color); g.fillOval (i,j+120,8,8);
}
}
}
public void drawImage_04(Graphics g){ for(int i = 0; i < imgarr.length; i++){
for(int j = 0; j< imgarr[i].length; j++){ int rgb = imgarr[i][j];
int red = (rgb>>16)&0xFF; int green = (rgb>>8)&0xFF; int blue = (rgb>>0)&0xFF;
int gray = (red+green+blue)/3; if(gray<80){
g.setColor(Color.BLACK);
}else{
g.setColor (Color.WHITE);
}
g.fillRect (i,j+120,1,1);
}
}
}
public void drawImage_05(Graphics g){ Random rannum = new Random ();
int num = rannum.nextInt (100)-50; for(int i = 0; i < imgarr.length; i+=5){
for(int j = 0; j< imgarr[i].length; j+=5){ int rgb = imgarr[i][j];
Color color = new Color (rgb); g.setColor (color);