图片处理工具-局部像素替换

时间:2021-08-15 20:31:18
package com.szcentral.its.util;
 
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
 
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
 
/**
 * 图形识别技术
 * <a href="http://my.oschina.net/arthor" target="_blank" rel="nofollow">@author</a>  marker
 * 时间:2012-4-6
 * */
public class ImageUtil {
	
	/**
	 * 扫描指定区域的RGB 
	 * 并将区域内的一定范围的RGB像素点替换为红色
	 * @param x1 图片起始X坐标
	 * @param y1 图片起始Y坐标
	 * @param x2 图片结束X坐标
	 * @param y2 图片结束Y坐标
	 * @param path 图片路径
	 * @return 修改图片RGB后的存储路径
	 * @throws IOException 
	 */
	public static String replaceRGB(int x1,int x2,int y1,int y2,String path,String destPath) throws IOException{
		 BufferedImage bi = (BufferedImage)ImageIO.read(new File(path));
	        //扫描图片选定区域
	        for(int i=y1;i<y2;i++){
	           for(int j=x1;j<x2;j++){//行扫描
	                int dip = bi.getRGB(j, i);
	                int red   =(dip & 0xff0000) >> 16;  
	                int green =(dip & 0xff00) >> 8;
	                int blue  =(dip & 0xff);
	                //判断RGB分量范围 偏黄的分量范围
	                if(red>=200&&green>100&&blue>60){
	                	Color ss = new Color(red,50,50);
	                	int ssd = ss.getRGB();
	                	bi.setRGB(j, i, ssd);
	                }
	                
	            }
	        }
	        
	        File newFile = new File(destPath);
	        ImageIO.write(bi, "bmp", newFile);
	        return path;
	}
	

	/** * 根据文件名判断文件是否为图片 * 
	 * * @param filePath  文件路径 
	 * * @return {@code true}: 是<br>{@code false}: 否 */ 
	public static boolean isImage(String filePath) { 
		String path = filePath.toUpperCase(); 
		return path.endsWith(".PNG") || path.endsWith(".JPG") || 
		path.endsWith(".JPEG") || path.endsWith(".BMP") || path.endsWith(".GIF"); 
	} 
	
	public static void main(String[] args) throws IOException {
		ImageUtil.replaceRGB(100,150,663,763,"D:\\image\\1.bmp","D:\\imageA\\1.bmp");
	}
	
    /*public static void main(String[] args) throws IOException {
  
        BufferedImage bi = (BufferedImage)ImageIO.read(new File("C://Users//TAN//Desktop//temp//电警抓拍图片泛黄//电警抓拍图片泛黄//1.bmp"));
         
        //获取图像的宽度和高度
        int width = bi.getWidth();
        int height = bi.getHeight();
        //扫描图片
        System.out.println(width+"*"+height);
        for(int i=bi.getMinX();i<height;i++){
            for(int j=bi.getMinY();j<width;j++){//行扫描
                int dip = bi.getRGB(j, i);
                int p=dip;
                int red   =(dip & 0xff0000) >> 16;  
                int green =(dip & 0xff00) >> 8;
                int blue  =(dip & 0xff);
                if(green!=blue){
                	System.out.println(red+"|"+green+"|"+blue);//换行
                }
                if(red>=230&&green>220&&blue>200){
                	System.out.println(red+"|"+green+"|"+blue+"{"+i+j+"}");//换行
                	Color ss = new Color(red,20,20);
                	int ssd = ss.getRGB();
                	bi.setRGB(j, i, ssd);
                }
                
            }
        }
        for(int i=115;i<150;i++){
           for(int j=679;j<714;j++){//行扫描
                int dip = bi.getRGB(j, i);
                int p=dip;
                int red   =(dip & 0xff0000) >> 16;  
                int green =(dip & 0xff00) >> 8;
                int blue  =(dip & 0xff);
                if(green!=blue){
                	System.out.println(red+"|"+green+"|"+blue);//换行
                }
                if(red>=200&&green>100&&blue>60){
                	System.out.println(red+"|"+green+"|"+blue+"{"+i+","+j+"}");//换行
                	Color ss = new Color(red,50,50);
                	int ssd = ss.getRGB();
                	bi.setRGB(j, i, ssd);
                }
                
            }
        }
        
        File newFile = new File("C://Users//TAN//Desktop//temp//电警抓拍图片泛黄//1.bmp");
        ImageIO.write(bi, "bmp", newFile);
        
    }*/
 
}