java图片读写修改


import  java.awt.Graphics;
import  java.awt.Image;
import  java.awt.image.BufferedImage;
import  java.io.File;
import  java.io.IOException;
import  java.net.URL;
import  java.util.LinkedList;

import  javax.imageio.ImageIO;
import  javax.swing.ImageIcon;

public   class  VKImage  {
    
private BufferedImage bufferdImage = null;
    
private int height = 0;
    
private int width = 0;
    
public int[][] rgb;
    
public int[][] crgb;
    
private int[][] flag;
    
public static void saveBMPImage(BufferedImage bi,File f)throws IOException {
        String   format   
=   "BMP";   
        javax.imageio.ImageIO.write(bi,format,f);
    }

    
public static void saveBMPImage(int[][] irgb,File f)throws IOException {
        BufferedImage bi 
= new BufferedImage(irgb.length,irgb[0].length,BufferedImage.TYPE_INT_RGB);
        
for(int i=0;i<irgb.length;i++){
            
for(int j=0;j<irgb[0].length;j++){
                bi.setRGB(i, j, irgb[i][j]);
            }

        }

        String   format   
=   "BMP";   
        javax.imageio.ImageIO.write(bi,format,f);
    }

    
public VKImage(URL location) throws IOException{
        
this.bufferdImage = javax.imageio.ImageIO.read(location);
        init();
    }

    
public VKImage(String filename)throws IOException{
        
this.bufferdImage = javax.imageio.ImageIO.read(new File(filename));
        init();
    }

    
public VKImage(File file)throws IOException{
        
this.bufferdImage = javax.imageio.ImageIO.read(file);
        init();
    }

    
private void init(){
        
this.height = bufferdImage.getHeight();
        
this.width = bufferdImage.getWidth();
        rgb 
= new int[width][height];
        crgb 
= new int[width][height];
        flag 
= new int[width][height];
        
for(int i=0;i<width;i++){
            
for(int j=0;j<height;j++){
                rgb[i][j] 
= bufferdImage.getRGB(i,j);
                crgb[i][j] 
= rgb[i][j];
            }

        }

    }

    
//
    public void reset(){
        
for(int i=0;i<width;i++){
            
for(int j=0;j<height;j++){
                crgb[i][j] 
= rgb[i][j];
            }

        }

    }

    
    
public int getHeight(){
        
return height;
    }

    
public int getWidth(){
        
return width;
    }

    
int tt;
    
private void intensifyPoint(int x,int y,int by,int px,int py){
        tt
++;
        
if(tt>2000){
            tt
--;
            
return;
        }

        flag[x][y] 
= 1;
        
if(x-1>=0&&y-1>=0&&flag[x-1][y-1]==0&&(Math.abs(crgb[x-1][y-1]-crgb[x][y])<=by)){
            intensifyPoint(x
-1,y-1,by,px,py);
        }

        
if(x-1>=0&&flag[x-1][y]==0&&(Math.abs(crgb[x-1][y]-crgb[x][y])<=by)){
            intensifyPoint(x
-1,y,by,px,py);
        }

        
if(x-1>=0&&y+1<height&&flag[x-1][y+1]==0&&(Math.abs(crgb[x-1][y+1]-crgb[x][y])<=by)){
            intensifyPoint(x
-1,y+1,by,px,py);
        }

        
if(y-1>=0&&flag[x][y-1]==0&&(Math.abs(crgb[x][y-1]-crgb[x][y])<=by)){
            intensifyPoint(x,y
-1,by,px,py);
        }

        
if(y+1<height&&flag[x][y+1]==0&&(Math.abs(crgb[x][y+1]-crgb[x][y])<=by)){
            intensifyPoint(x,y
+1,by,px,py);
        }

        
if(x+1<width&&y-1>=0&&flag[x+1][y-1]==0&&(Math.abs(crgb[x+1][y-1]-crgb[x][y])<=by)){
            intensifyPoint(x
+1,y-1,by,px,py);
        }

        
if(x+1<width&&flag[x+1][y]==0&&(Math.abs(crgb[x+1][y]-crgb[x][y])<=by)){
            intensifyPoint(x
+1,y,by,px,py);
        }

        
if(x+1<width&&y+1<height&&flag[x+1][y+1]==0&&(Math.abs(crgb[x+1][y+1]-crgb[x][y])<=by)){
            intensifyPoint(x
+1,y+1,by,px,py);
        }

        crgb[x][y] 
= crgb[px][py];
        tt
--;
    }

    
private int intensifyOnce(int by){
        
int x = -1;
        
int y = -1;
        
for(int i=0;i<width;i++){
            
for(int j=0;j<height;j++){
                
if(flag[i][j] == 0){
                    x 
= i;
                    y 
= j;
                    
break;
                }

            }

        }

        
if(x==-1){
            
return 0;
        }

        
        intensifyPoint(x,y,by,x,y);
        
return 1;
    }

    
public void intensify(int by){
        
for(int i=0;i<width;i++){
            
for(int j=0;j<height;j++){
                flag[i][j] 
= 0;
            }

        }

        
while(true){
            
int ret = intensifyOnce(by);
            
if(ret==0){
                
break;
            }

        }

    }

}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值