多种效果处理的选择方法与存储
1.UI类
-初始化界面
-添加监听器 -创建监听器对象
-获取Graphics 传入监听器中
package cs0122;
import javax.swing.*;
import java.awt.*;
/*
* image processing class01
* -@author cs0122
* -@version v1.0
* */
public class ImageUI extends JFrame {
//Create listener instance
ImageListener imglisten = new ImageListener();
public void initUI(){ // Initialization interface
setTitle("图像处理");
setSize(800,800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); //居中显示
FlowLayout flow = new FlowLayout(); //add layout: flow layout
setLayout(flow);
addButton();
setVisible(true);
imglisten.g = getGraphics();
}
//add button function
public void addButton(){
String[] btnstrs = {
"Original","Mosaic","Negative","Brightenface","Gray","Binarization","Whitening","Oil","Outline","fuse","withdraw"
};
for (int i = 0 ; i < btnstrs.length;i++){
String btnstr = btnstrs[i];
JButton btn = new JButton(btnstr);
add(btn);
//add Action Listener
btn.addActionListener(imglisten);
}
}
// @param args
public static void main(String[] args){
ImageUI imageUI = new ImageUI();
imageUI.initUI();
}
}
2.Listener类
-实现监听器
-重写监听器的方法
-在方法中可以获取按钮上的字符串
-通过字符串 选择使用哪种滤镜来绘制图片
package cs0122;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
* implements ActionListener
/*@author cs0122
-version1.0
* */
public class ImageListener implements ActionListener {
Graphics g = null ; // Graphics class
ImageEff imgeff = new ImageEff(); //initialize the instance image processing class
int[][] imgarr = {};
int[][] imgarr01 = {};
int[][] imgarr02 ={};
public ImageListener(){
//Initialize the image array according to the path
String imagPath01 = "C:\\Users\\ch\\Desktop\\zxc.png";
String imagPath02 = "C:\\Users\\ch\\Desktop\\fj1.png";
String imagPath03 = "C:\\Users\\ch\\Desktop\\fj2.png";
imgarr = imgeff.getImagePixArray(imagPath01);
imgarr01 = imgeff.getImagePixArray(imagPath02);
imgarr02 = imgeff.getImagePixArray(imagPath03);
}
@Override
public void actionPerformed(ActionEvent e){
//Get the string on the button
String btnstr = e.getActionCommand();
System.out.println("click"+btnstr);
if(btnstr.equals ("Original")){
imgeff.drawImage_01 (imgarr, g);
} else if(btnstr.equals ("Mosaic")){
imgeff.drawImage_02 (imgarr, g);
} else if(btnstr.equals ("Negative")){
imgeff.drawImage_03 (imgarr, g);
} else if(btnstr.equals ("Brightenface")){
imgeff.drawImage_04 (imgarr, g);
} else if(btnstr.equals ("Gray")){
imgeff.drawImage_05 (imgarr, g);
} else if(btnstr.equals ("Binarization")){
imgeff.drawImage_06 (imgarr, g);
} else if(btnstr.equals ("Whitening")){
imgeff.drawImage_07 (imgarr, g);
} else if(btnstr.equals ("Oil")){
imgeff.drawImage_08 (imgarr, g);
} else if(btnstr.equals ("Outline")){
System.out.println ("btnstr=-" + btnstr);
imgeff.drawImage_09 (imgarr, g);
} else if(btnstr.equals ("fuse")){
imgeff.drawImage_10 (imgarr01, imgarr02, g);
} else if(btnstr.equals ("withdraw")){
}
}
}
3.ImageEff 图片处理类
-将计算机图片转为二维数组
-提供各种滤镜处理方法
package cs0122;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
/**
* Image processing class
* Convert the picture on the computer into a two-dimensional array
* Provides methods for image processing
*
* @author cs0122
* @version v1.0
*
*/
public class ImageEff{
// start coordinate
final int X = 100;
final int Y = 100;
/**
* Get the picture on the computer according to the path
* and return the two-dimensional array represented by the picture
* @param path
*
* @return two-dimensional
*/
public int[][] getImagePixArray(String path){
File file = new File (path);
BufferedImage buffimg = null;
try {
buffimg = ImageIO.read (file);
} catch (IOException e) {
e.printStackTrace ();
}
// ????????
int w = buffimg.getWidth ();
int h = buffimg.getHeight ();
int[][] imgarr = new int[w][h];
for(int i = 0; i < w; i++){
for(int j = 0; j < h; j++){
imgarr[i][j] = buffimg.getRGB (i, j);
}
}
return imgarr;
}
/**
* Original
* @param imgarr
* @param g
*/
public void drawImage_01(int[][] imgarr, Graphics g){
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 (X + i, Y + j, 1, 1);
}
}
}
/**
* Mosaic
* @param imgarr
* @param g
*/
public void drawImage_02(int[][] imgarr, Graphics g){
for(int i = 0; i < imgarr.length - 10; i += 10){
for(int j = 0; j < imgarr[i].length - 10; j += 10){
int rgb = imgarr[i][j];
Color color = new Color (rgb);
g.setColor (color);
g.fillRect (X + i, Y + j, 10, 10);
}
}
}
/**
* Negative
*
* @param imgarr
* @param g
*/
public void drawImage_03(int[][] imgarr, Graphics g){
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);
int red = color.getRed ();
int green = color.getGreen ();
int blue = color.getBlue ();
Color ncolor = new Color (255 - red, 255 - green, 255 - blue);
g.setColor (ncolor);
g.fillRect (X + i, Y + j, 1, 1);
}
}
}
/*
* Brighten face
* @param imgarr
* @param g
* */
public void drawImage_04(int[][] imgarr, Graphics g){
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);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int nred = red > 220 ? 250 : red + 30;
int ngreen = green > 220 ? 250 : green + 30;
int nblue = blue > 220 ? 250: blue + 30;
Color ncolor = new Color(nred,ngreen,nblue);
int gray = (red+green+blue)/3;
if(gray<100){
g.setColor(color);
}else {
g.setColor(ncolor);
}
g.fillRect(X+i,Y+j,1,1);
}
}
}
/**
* Gray
*
* @param imgarr
* @param g
*/
public void drawImage_05(int[][] imgarr, Graphics g){
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);
int red = color.getRed ();
int green = color.getGreen ();
int blue = color.getBlue ();
int gray = (int) (red * 0.41 + green * 0.36 + blue * 0.23);
Color ncolor = new Color (gray, gray, gray);
g.setColor (ncolor);
g.fillRect (X + i, Y + j, 1, 1);
}
}
}
/*
* Binarization
* @param imgarr
* @param g
* */
public void drawImage_06(int[][] imgarr, Graphics g){
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);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int gray = (red + green + blue)/3;
if(gray<165){
g.setColor(Color.BLACK);
}else{
g.setColor(Color.WHITE);
}
g.fillRect(50+i,50+j,1,1);
}
}
}
/*
* Whitening
* @param imgarr
* @param g
* */
public void drawImage_07(int[][] imgarr, Graphics g){
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);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int nred = red > 220 ? 250 : red + 30;
int ngreen = green > 220 ? 250 : green + 30;
int nblue = blue > 220 ? 250: blue + 30;
Color ncolor = new Color(nred,ngreen,nblue);
int gray = (red+green+blue)/3;
if(gray<100){
g.setColor(color);
}else {
g.setColor(ncolor);
}
g.fillRect(X+i,Y+j,1,1);
}
}
}
/*
* oil painting
* @param imgarr
* @param g
* */
public void drawImage_08(int[][] imgarr, Graphics g){
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);
Random random = new Random();
int ran = random.nextInt(20)+5;
g.setColor(color);
g.fillOval(X+i,Y+j,ran,ran);
}
}
}
/**
* Outline
*
* @param imgarr
* @param g
*/
public void drawImage_09(int[][] imgarr, Graphics g){
for(int i = 0; i < imgarr.length-2; i++){
for(int j = 0; j < imgarr[i].length-2; 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 = (int) (red * 0.41 + green * 0.36 + blue * 0.23);
// ???????б?????????? ???? ?????
int nrgb = imgarr[i + 2][j + 2];
Color ncolor = new Color (nrgb);
int nred = ncolor.getRed ();
int ngreen = ncolor.getGreen ();
int nblue = ncolor.getBlue ();
int ngray = (int) (nred * 0.41 + ngreen * 0.36 + nblue * 0.23);
if(Math.abs (gray - ngray) > 15){
g.setColor (Color.black);
} else{
g.setColor (Color.white);
}
g.fillRect (X + i, Y + j, 1, 1);
}
}
}
/**
* fuse
* Merge the two picture
* Take half each
* @param imgarr01 imgarr02
* @param g
*/
public void drawImage_10(int[][] imgarr01, int[][] imgarr02,Graphics g){
for(int i = 0; i < imgarr01.length; i++){
for(int j = 0; j < imgarr01[i].length; j++){
int rgb1 = imgarr01[i][j];
int rgb2 = imgarr02[i][j];
Color color1 = new Color (rgb1);
Color color2 = new Color (rgb2);
int red1 = color1.getRed();
int green1 = color1.getGreen();
int blue1 = color1.getBlue();
int red2 = color2.getRed();
int green2 = color2.getGreen();
int blue2 = color2.getBlue();
Color ncolor = new Color((red1+red2)/2,(green1+green2)/2,(blue1+blue2)/2);
g.setColor (ncolor);
g.fillRect (X + i, Y + j, 1, 1);
}
}
}
}
最终实现效果:
原图:
马赛克:
反片:
脸部提亮:
灰度:
二值化:
油画:
轮廓提取:
图片融合:
还有一些功能尚未解决,如撤回,界面拉长导致图片消失等问题。