利用RGB特性,给图片加上各种滤镜(原图,灰度,二值化,冷色,暖色,怀旧,灵魂出窍(照片底色),铁砂网,美白,放大2倍,乐高,ARGB,画布刷新)
原图
灰度
二值化
随机马赛克
灵魂出窍(照片底色)
冷色
暖色
怀旧
铁砂网
美白(和原图对比看,效果不明显)
乐高
转化为ARGB格式,透明度变成原来的一半
放大两倍(左上角是原图,为了方便对比放在最上面)
代码实现(太多就不分开写了,具体有注释)
public class Drawlistener implements MouseListener, ActionListener {
Graphics gr;
DrawUI ui;
String graphics_type;
DrawUI.DrawPanel drawPanel;
BufferedImage image_display;
int[][]imageArray_display;
Drawlistener(Graphics gr){ this.gr =gr;}
public void setGraphics(Graphics graphics){
this.gr = graphics;
}
public void setGrawUI(DrawUI.DrawPanel drawPanel){
this.drawPanel = drawPanel;
}
public void refrash(){
this.drawPanel.paint(gr);
}
@Override
public void actionPerformed(ActionEvent e) {
String btnstr = e.getActionCommand ();
if (btnstr.equals("原图")){
graphics_type ="原图";
paint_original(gr);
}else if(btnstr.equals("灰度")){
graphics_type ="灰度";
draw_gray_Image(imageArray_display,gr);
}else if(btnstr.equals("二值化")){
graphics_type ="二值化";
draw_Image_binarization(imageArray_display,gr);
}else if(btnstr.equals("随机马赛克")){
graphics_type ="随机马赛克";
draw_Mosaic(gr);
}else if (btnstr.equals("冷色")){
graphics_type ="冷色";
draw_cold(gr);
}else if (btnstr.equals("暖色")){
graphics_type ="暖色";
draw_warm(gr);
}else if(btnstr.equals("灵魂出窍")){
graphics_type ="灵魂出窍";
draw_photo_negative(gr);
}else if(btnstr.equals("怀旧")){
graphics_type ="怀旧";
draw_oldimage(gr);
}else if(btnstr.equals("放大2倍")){
graphics_type ="放大2倍";
draw_bigger_2(gr);
}else if(btnstr.equals("美白")){
graphics_type ="美白";
draw_Whitening(gr);
}else if (btnstr.equals("铁砂网")){
graphics_type ="铁砂网";
draw_oil_painting(gr);
}else if (btnstr.equals("乐高")){
graphics_type ="乐高";
draw_LEGO(gr);
}else if (btnstr.equals("刷新")){
graphics_type ="刷新";
refrash();
}else if (btnstr.equals("ARGB")){
graphics_type ="ARGB";
draw_ARGB(gr);
pixelToRGB();
}
}
public void pixelToRGB(){
int num = 8421504;
int red = (num >> 16) & 0xFF;
System.out.println ("红"+red);
int green = (num >> 8) & 0xFF;
System.out.println ("绿"+green);
int blue = (num >> 0) & 0xFF;
System.out.println ("蓝"+blue);
}
public void draw_ARGB(Graphics g){
for(int i = 0; i < imageArray_display.length; i++){
for(int j = 0; j < imageArray_display[i].length; j++){
int i1 = imageArray_display[i][j];
int red = (i1 >> 16) & 0xFF;
int green = (i1 >> 8) & 0xFF;
int blue = (i1 >> 0) & 0xFF;
int transparency =(i1 >> 24) & 0xFF;
Color color = new Color (red,green,blue,transparency/2);
g.setColor(color);
g.fillRect ( i, j, 1, 1);
}
}
}
public void draw_LEGO(Graphics g){
for(int i = 0; i < imageArray_display.length; i++){
for(int j = 0; j < imageArray_display[i].length; j++){
int i1 = imageArray_display[i][j];
int red = (i1 >> 16) & 0xFF;
int green = (i1 >> 8) & 0xFF;
int blue = (i1 >> 0) & 0xFF;
Color color = new Color (red,green,blue);
if ((i&3) ==0 || (j&3) ==0){
color = Color.BLACK;
}
g.setColor(color);
g.fillRect ( i+500, j, 1, 1);
}
}
}
public void draw_oil_painting(Graphics g){
for(int i = 0; i < imageArray_display.length; i+=2){
for(int j = 0; j < imageArray_display[i].length; j+=2){
int i1 = imageArray_display[i][j];
int red = (i1 >> 16) & 0xFF;
int green = (i1 >> 8) & 0xFF;
int blue = (i1 >> 0) & 0xFF;
Color color = new Color (red,green,blue);
g.setColor(color);
g.fillOval ( i+500, j, 1, 1);
}
}
}
public void draw_Whitening(Graphics g){
for(int i = 0; i < imageArray_display.length; i++){
for(int j = 0; j < imageArray_display[i].length; j++){
int i1 = imageArray_display[i][j];
int red = (i1 >> 16) & 0xFF;
int green = (i1 >> 8) & 0xFF;
int blue = (i1 >> 0) & 0xFF;
if (green+10 <= 255 && red+10 <= 255 && blue+10 <= 255 ){
red =red+10;
green =green+10;
blue =blue+10;
}
Color color = new Color (red,green,blue);
g.setColor(color);
g.fillRect ( i, j, 1, 1);
}
}
}
public void draw_bigger_2(Graphics g){
for(int i = 0; i < imageArray_display.length; i++){
for(int j = 0; j < imageArray_display[i].length; j++){
int i1 = imageArray_display[i][j];
int red = (i1 >> 16) & 0xFF;
int green = (i1 >> 8) & 0xFF;
int blue = (i1 >> 0) & 0xFF;
Color color = new Color (red,green,blue);
g.setColor(color);
g.fillRect ( i*2, j*2, 2, 2);
}
}
}
public void draw_oldimage(Graphics g){
for(int i = 0; i < imageArray_display.length; i++){
for(int j = 0; j < imageArray_display[i].length; j++){
int i1 = imageArray_display[i][j];
int red = (i1 >> 16) & 0xFF;
int green = (i1 >> 8) & 0xFF;
int blue = (i1 >> 0) & 0xFF;
if (green+25 <= 255 && red+25 <= 255 &&blue-25>=0 ){
green =green+25;
red =red+25;
blue =blue-25;
}
Color color = new Color (red,green,blue);
g.setColor(color);
g.fillRect ( i, j, 1, 1);
}
}
}
public void draw_cold(Graphics g){
for(int i = 0; i < imageArray_display.length; i++){
for(int j = 0; j < imageArray_display[i].length; j++){
int i1 = imageArray_display[i][j];
int red = (i1 >> 16) & 0xFF;
int green = (i1 >> 8) & 0xFF;
int blue = (i1 >> 0) & 0xFF;
if (blue+50 <= 255 && red-50>=0 &&green-50>=0 ){
blue =blue+50;
red =red-50;
green =green-50;
}
Color color = new Color (red,green,blue);
g.setColor(color);
g.fillRect ( i, j, 1, 1);
}
}
}
public void draw_warm(Graphics g){
for(int i = 0; i < imageArray_display.length; i++){
for(int j = 0; j < imageArray_display[i].length; j++){
int i1 = imageArray_display[i][j];
int red = (i1 >> 16) & 0xFF;
int green = (i1 >> 8) & 0xFF;
int blue = (i1 >> 0) & 0xFF;
if (red+50 <= 255 && blue-50>=0 &&green-50>=0 ){
red =red+50;
blue =blue-50;
green =green-50;
}
Color color = new Color (red,green,blue);
g.setColor(color);
g.fillRect ( i, j, 1, 1);
}
}
}
public void draw_photo_negative(Graphics g){
for(int i = 0; i < imageArray_display.length; i++){
for(int j = 0; j < imageArray_display[i].length; j++){
int i1 = imageArray_display[i][j];
int red = (i1 >> 16) & 0xFF;
int green = (i1 >> 8) & 0xFF;
int blue = (i1 >> 0) & 0xFF;
Color color = new Color (255-red,255-green,255-blue);
g.setColor(color);
g.fillRect ( i, j, 1, 1);
}
}
}
public void draw_Mosaic_trangel(Graphics g){
Random ran = new Random ();
for(int i = 0; i < 500; i += 10){
for(int j = 0; j < 500; j += 10){
Color color = new Color (ran.nextInt (Integer.MAX_VALUE));
g.setColor (color);
g.fillRect (i, j, 10, 10);
}
}
}
public void draw_Mosaic(Graphics g){
Random ran = new Random ();
int number=0;
for(int i = 0; i < imageArray_display.length; i++){
for(int j = 0; j < imageArray_display[i].length; j++){
int i1 = imageArray_display[i][j];
int red = (i1 >> 16) & 0xFF;
int green = (i1 >> 8) & 0xFF;
int blue = (i1 >> 0) & 0xFF;
if ((number&3) ==0){
int number_Mosaic =ran.nextInt (Integer.MAX_VALUE);
int red_Mosaic = (number_Mosaic >> 16) & 0xFF;
int green_Mosaic = (number_Mosaic >> 8) & 0xFF;
int blue_Mosaic = (number_Mosaic >> 0) & 0xFF;
Color color = new Color (red_Mosaic,green_Mosaic,blue_Mosaic);
g.setColor(color);
}else{
Color color = new Color (red,green,blue);
g.setColor(color);
}
g.fillRect ( i, j, 1, 1);
number++;
}
}
}
public int[][] get_Mosaic_arry(Graphics g){
Random ran = new Random ();
int[][] imgarr_10 = new int[500][500];
for(int i = 0; i < 500; i += 10){
for(int j = 0; j < 500; j += 10){
imgarr_10[i][j] = ran.nextInt (Integer.MAX_VALUE);
Color color = new Color (imgarr_10[i][j]);
g.setColor (color);
g.fillRect (i, j, 10, 10);
}
}
return imgarr_10;
}
public void draw_gray_Image(int[][] imgarr,Graphics g){
for(int i = 0; i < imgarr.length; i++){
for(int j = 0; j < imgarr[i].length; j++){
int i1 = imgarr[i][j];
int red = (i1 >> 16) & 0xFF;
int green = (i1 >> 8) & 0xFF;
int blue = (i1 >> 0) & 0xFF;
int gray = (red + green + blue) / 3;
Color color = new Color (gray,gray,gray);
g.setColor(color);
g.fillRect ( i, j, 1, 1);
}
}
}
public void draw_Image_binarization(int[][] imgarr,Graphics g){
for(int i = 0; i < imgarr.length; i++){
for(int j = 0; j < imgarr[i].length; j++){
int i1 = imgarr[i][j];
int red = (i1 >> 16) & 0xFF;
int green = (i1 >> 8) & 0xFF;
int blue = (i1 >> 0) & 0xFF;
int gray = (red + green + blue) / 3;
if(gray < 55){
g.setColor (Color.BLACK);
} else{
g.setColor (Color.WHITE);
}
g.fillRect ( i, j, 1, 1);
}
}
}
public int[][] getImageArray(BufferedImage img){
int[][] imgarr = new int[img.getWidth ()][img.getHeight ()];
for(int i = 0; i < img.getWidth (); i++){
for(int j = 0; j < img.getHeight (); j++){
int rgb = img.getRGB (i, j);
imgarr[i][j] = rgb;
}
}
return imgarr;
}
public BufferedImage getImage(String path){
File file = new File (path);
System.out.println (file.getPath ());
BufferedImage image =null;
try {
image = ImageIO.read (file);
}catch (Exception e){
e.printStackTrace ();
}
return image;
}
public void paint_original(Graphics g){
String path_folder ="C:\\Users\\lenovo\\Desktop\\img\\";
String name_img ="SteveJobs2.jpeg";
String str =path_folder + name_img;
image_display = getImage(str);
g.drawImage(image_display,0,0,null);
imageArray_display=getImageArray(image_display);
}