자바 사진 압축 데모

3836 단어
더 읽 기
package com.wangxuegang.image;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

/**
 * 
 * @description      Demo 
 * @author wangxuegang
 * @since JDK 1.6
 */
public class ImageZipTest {
	
	/**
	 *     
	 */
	private static final Integer PHOTO_SIZE = 102400;
	
	/**
	 * 
	 * @description   
	 * @param bt       
	 * @param scale     
	 * @param flag     :true   ; false   
	 * @throws IOException 
	 * @since JDK 1.6
	 */
	public static void pic(byte[] bt,Integer scale,boolean flag) throws IOException{
		
		System.out.println("    :"+scale);
		
		InputStream is = new ByteArrayInputStream(bt);
		
		//    
		BufferedImage oldImage = ImageIO.read(is);
		
		//    
		BufferedImage newImage = picCompress(oldImage, scale, flag);
		
		//       
		ByteArrayOutputStream bs = new ByteArrayOutputStream();
		
		//     
		ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
		ImageIO.write(newImage, "jpg", imOut);
		
		//     
		InputStream contentStream = new ByteArrayInputStream(bs.toByteArray());
		System.out.println("     :"+contentStream.available());
		System.out.println("    ::"+PHOTO_SIZE);
		
		//          
		while(contentStream.available() > PHOTO_SIZE){
			scale +=1;
			//       ,        
			pic(bs.toByteArray(), scale, flag);
			ImageIO.write(newImage, "JPEG", new File("C:\\Users\\wangxuegang\\Desktop\\ _01.jpg"));
			
			contentStream.close();
			bs.close();
			is.close();
			break;
		}
	}
	
	/**
	 * 
	 * @description     
	 * @param image     
	 * @param scale     
	 * @param flag     :true   ; false   
	 * @since JDK 1.6
	 */
	private static BufferedImage picCompress(BufferedImage image,Integer scale,boolean flag) {
		//      
		int width = image.getWidth();
		int height = image.getHeight();
		if(flag) {//   
            width = width * scale;
            height = height * scale;
        } else {//   
            width = width / scale;
            height = height / scale;
        }
		System.out.println("width:"+width);
		System.out.println("height:"+height);
		Image img = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
		BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics graphics = outputImage.getGraphics();
		graphics.drawImage(img, 0, 0, null);
		graphics.dispose();
		return outputImage;
	}
	
	/**
	 * 
	 * @description   Main
	 * @since JDK 1.6
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException {
		
		InputStream is = new FileInputStream(new File("C:\\ .jpg"));
		ByteArrayOutputStream bo = new ByteArrayOutputStream();
		byte[] bt = new byte[1024];
		int len = 0;
		while((len = is.read(bt))>0){
			bo.write(bt,0,len);
		}
		
		bo.close();
		is.close();
		
		pic(bo.toByteArray(),5,false);
		
	}
}

좋은 웹페이지 즐겨찾기