자바 이미지 모자이크 처리

3233 단어 기초 지식
package com.fh.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 *         
 * @param source      ( :F:/images/6.jpg)
 * @param imageType     ( :jpg)
 * @param size      ,        
 * @return
 */
public class Mosaic {
	
	  public static String markImageByMosaic(String source){
		  
		  String result = null;
		  int size = 40;  //     
		  try{
			    File file = new File(source);
			    String fileName = file.getName();//      
			    String s[] = fileName.split("\\.");
			    String imageType = s[1];
			    BufferedImage img = ImageIO.read(file); //      
			    int width = img.getWidth(null); //    
			    int height = img.getHeight(null); //    
			    
			    BufferedImage mosaic = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
			    //           
			    if (width < size || height < size) {
			    	return "        ";
			    }
			    if(size<=0){
			    	return "        ";
			    }
			    
			    int xcount = 0; //x      
			    int ycount = 0; //y      
			    
			    if (width % size == 0) {
			      xcount = width / size;
			    } else {
			      xcount = width / size + 1;
			    }
			    if (height % size == 0) {
			      ycount = height / size;
			    } else {
			      ycount = height / size + 1;
			    }
			    int x = 0; //x  
			    int y = 0; //y  
			    
			    //     (         )
			    Graphics2D g = mosaic.createGraphics();
			    for (int i = 0; i < xcount; i++) {
			      for (int j = 0; j < ycount; j++) {
			        //        
			        int mwidth = size;
			        int mheight = size;
			        
			        if(i==xcount-1){  		//          size
			        	mwidth = width-x;
			        }
			        if(j == ycount-1){ //          size
			        	mheight = height-y;
			        }
			        //          RGB 
			        int centerX = x;
			        int centerY = y;
			        
			        if (mwidth % 2 == 0) {
			        	centerX += mwidth / 2;
			        } else {
			        	centerX += (mwidth - 1) / 2;
			        }
			        if (mheight % 2 == 0) {
			        	centerY += mheight / 2;
			        } else {
			        	centerY += (mheight - 1) / 2;
			        }
			        
			        Color color = new Color(img.getRGB(centerX, centerY));
			        g.setColor(color);
			        g.fillRect(x, y, mwidth, mheight);
			        y = y + size;//         y  
			      }
			      y = 0;//   y  
			      x = x + size;//   x  
			    }
			    //       
			    g.dispose();
			    File sf = new File(source);
			    ImageIO.write(mosaic, imageType, sf); //     
			    result = "      ";
		  }catch(Exception e){
			  	e.printStackTrace();
		  }
		    return result;
		  }
	  
	  public static void main(String[] args){
		 /* String source = "F:/Tomcat/webapps/AMBER/uploadFiles/uploadImg/456.jpg";
		  Mosaic.markImageByMosaic(source);*/
	  } 
	}

글 전재 출처:https://blog.csdn.net/cnsd_liuliu/article/details/52450585

좋은 웹페이지 즐겨찾기