轉自:http://blog.csdn.net/mainstream_code/article/details/7036302
- <pre name="code" class="java">/**
- * 改變圖片的大小到寬爲size,然後高隨着寬等比例變化
- * @param is 上傳的圖片的輸入流
- * @param os 改變了圖片的大小後,把圖片的流輸出到目標OutputStream
- * @param size 新圖片的寬
- * @param format 新圖片的格式
- * @throws IOException
- */
- public static void resizeImage(InputStream is, OutputStream os, int size, String format) throws IOException {
- BufferedImage prevImage = ImageIO.read(is);
- double width = prevImage.getWidth();
- double height = prevImage.getHeight();
- double percent = size/width;
- int newWidth = (int)(width * percent);
- int newHeight = (int)(height * percent);
- BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_BGR);
- Graphics graphics = image.createGraphics();
- graphics.drawImage(prevImage, 0, 0, newWidth, newHeight, null);
- ImageIO.write(image, format, os);
- os.flush();
- is.close();
- os.close();
- } </pre><br>
- <br>
- <pre></pre>
- <pre name="code" class="java"></pre><pre name="code" class="java"></pre><pre name="code" class="java"></pre><pre name="code" class="java"></pre><pre name="code" class="java">package graphicsTest;
- import java.awt.Image;
- 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.JPEGImageEncoder;
- public class GraphicsTest1 {
- // 圖片寬和高的最大尺寸
- public static final int IMAGEMAXBIG = 2000;
- // 圖片寬和高的最小尺寸
- public static final int IMAGEMINBIG = 10;
- // 按原圖大小生成新圖
- public static final int CREATENEWIMAGETYPE_0 = 0;
- // 按指定的大小生成新圖
- public static final int CREATENEWIMAGETYPE_1 = 1;
- // 按原圖寬高比例生成新圖-按指定的寬度
- public static final int CREATENEWIMAGETYPE_2 = 2;
- // 按原圖寬高比例生成新圖-按指定的高度
- public static final int CREATENEWIMAGETYPE_3 = 3;
- // 按原圖寬高比例生成新圖-按指定的寬和高中較大的尺寸
- public static final int CREATENEWIMAGETYPE_4 = 4;
- // 按原圖寬高比例生成新圖-按指定的寬和高中較小的尺寸
- public static final int CREATENEWIMAGETYPE_5 = 5;
- /**
- *
- * @param _file
- * 原圖片
- * @param createType
- * 處理類型
- * @param newW
- * 新寬度
- * @param newH
- * 新高度
- * @return
- * @throws Exception
- */
- public static String createNewImage(File _file, int createType, int newW,
- int newH) throws Exception {
- if (_file == null)
- return null;
- String fileName = _file.getPath();
- if (fileName == null || "".equals(fileName)
- || fileName.lastIndexOf(".") == -1)
- return null;
- String newFileName = "_NEW";
- /*
- * else newFileName = "_" + newFileName;
- */
- String outFileName = fileName.substring(0, fileName.lastIndexOf("."))
- + newFileName
- + fileName.substring(fileName.lastIndexOf("."), fileName
- .length());
- String fileExtName = fileName.substring(
- (fileName.lastIndexOf(".") + 1), fileName.length());
- if (newW < IMAGEMINBIG)
- newW = IMAGEMINBIG;
- else if (newW > IMAGEMAXBIG)
- newW = IMAGEMAXBIG;
- if (newH < IMAGEMINBIG)
- newH = IMAGEMINBIG;
- else if (newH > IMAGEMAXBIG)
- newH = IMAGEMAXBIG;
- // 得到原圖信息
- if (!_file.exists() || !_file.isAbsolute() || !_file.isFile()
- || !checkImageFile(fileExtName))
- return null;
- if ((new File(outFileName)).exists()) {
- System.out.println("file [" + outFileName + "] already exists");
- throw new Exception();
- }
- Image src = ImageIO.read(_file);
- int w = src.getWidth(null);
- int h = src.getHeight(null);
- // 確定目標圖片的大小
- int nw = w;
- int nh = h;
- if (createType == CREATENEWIMAGETYPE_0)
- ;
- else if (createType == CREATENEWIMAGETYPE_1) {
- nw = newW;
- nh = newH;
- } else if (createType == CREATENEWIMAGETYPE_2) {
- nw = newW;
- nh = (int) ((double) h / (double) w * nw);
- } else if (createType == CREATENEWIMAGETYPE_3) {
- nh = newH;
- nw = (int) ((double) w / (double) h * nh);
- } else if (createType == CREATENEWIMAGETYPE_4) {
- if ((double) w / (double) h >= (double) newW / (double) newH) {
- nh = newH;
- nw = (int) ((double) w / (double) h * nh);
- } else {
- nw = newW;
- nh = (int) ((double) h / (double) w * nw);
- }
- } else if (createType == CREATENEWIMAGETYPE_5) {
- if ((double) w / (double) h <= (double) newW / (double) newH) {
- nh = newH;
- nw = (int) ((double) w / (double) h * nh);
- } else {
- nw = newW;
- nh = (int) ((double) h / (double) w * nw);
- }
- }
- // 構造目標圖片
- BufferedImage tag = new BufferedImage(nw, nh,
- BufferedImage.TYPE_INT_RGB);
- // 得到目標圖片輸出流
- FileOutputStream out = new FileOutputStream(outFileName);
- // 根據需求畫出目標圖片 方式1
- tag.getGraphics().drawImage(src, 0, 0, nw, nh, null);
- // 將畫好的目標圖輸出到輸出流 方式1
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(tag);
- out.close();
- return outFileName;
- }
- public static boolean checkImageFile(String extName) {
- if ("jpg".equalsIgnoreCase(extName))
- return true;
- if ("gif".equalsIgnoreCase(extName))
- return true;
- if ("bmp".equalsIgnoreCase(extName))
- return true;
- if ("jpeg".equalsIgnoreCase(extName))
- return true;
- if ("png".equalsIgnoreCase(extName))
- return true;
- return false;
- }
- } </pre>
- <p><br>
- </p>
- <p><br>
- </p>
- <p><br>
- </p>