2015-03-06 17:12

[Java] 製作縮圖筆記

  1. package test_image; 
  2.  
  3. import java.awt.Graphics; 
  4. import java.awt.image.BufferedImage; 
  5. import java.io.File; 
  6.  
  7. import javax.imageio.ImageIO; 
  8.  
  9. public class TestImageResize { 
  10.  
  11.    private static int targetWidth = 120; 
  12.    private static int targetHeight = 80; 
  13.    private static double targetRate = (double) targetWidth / targetHeight; 
  14.  
  15.    public static void main(String[] args) throws Exception { 
  16.  
  17.        System.out.printf("Target w:%s, h:%s, r:%s\n",  
  18.            targetWidth, targetHeight, targetRate); 
  19.  
  20.  
  21.        BufferedImage image = ImageIO.read(new File("input.jpg")); 
  22.  
  23.        int type = image.getType(); 
  24.        if(type == 0) { type = BufferedImage.TYPE_INT_ARGB; } 
  25.  
  26.        int width = image.getWidth(); 
  27.        int height = image.getHeight(); 
  28.        double rate = (double) width / height; 
  29.  
  30.        System.out.printf("Source w:%s, h:%s, r:%s\n", width, height, rate); 
  31.  
  32.  
  33.  
  34.        /* 等比例縮小至指定大小內 */ 
  35.        int rWidth = targetWidth; 
  36.        int rHeight = targetHeight; 
  37.  
  38.        if(width < targetWidth && height < targetHeight) { 
  39.            rWidth = width; 
  40.            rHeight = height; 
  41.        } else if(rate > targetRate) { 
  42.            rHeight = (int) (targetWidth / rate); 
  43.        } else { 
  44.            rWidth = (int) (targetHeight * rate); 
  45.        } 
  46.        System.out.printf("Resize w:%s, h:%s\n", rWidth, rHeight); 
  47.  
  48.        BufferedImage resize1 = new BufferedImage(rWidth, rHeight, type); 
  49.        Graphics g1 = resize1.getGraphics(); 
  50.        g1.drawImage(image, 0, 0, rWidth, rHeight, null); 
  51.        g1.dispose(); 
  52.  
  53.        ImageIO.write(resize1, "jpg", new File("output_1.jpg")); 
  54.  
  55.  
  56.  
  57.  
  58.        /* 等比例縮小填滿指定大小 */ 
  59.        BufferedImage resize2 = new BufferedImage(targetWidth,targetHeight,type); 
  60.        Graphics g2 = resize2.getGraphics(); 
  61.  
  62.        int startX = 0; 
  63.        int startY = 0; 
  64.        int size = 0; 
  65.  
  66.        if(rate > targetRate) { 
  67.            startX = (int) (width - height * targetRate) / 2; 
  68.            size = height; 
  69.        } else { 
  70.            startY = (int) (height - width / targetRate) / 2; 
  71.            size = width; 
  72.        } 
  73.        System.out.printf("x:%s, y:%s, size:%s\n", startX, startY, size); 
  74.  
  75.        g2.drawImage( 
  76.            image, 
  77.            0, 0, targetWidth, targetHeight, 
  78.            startX, startY, (size + startX), (size + startY), 
  79.            null 
  80.        ); 
  81.  
  82.        g2.dispose(); 
  83.  
  84.        ImageIO.write(resize2, "jpg", new File("output_2.jpg")); 
  85.    } 
  86. } 

參考文件:
Graphics (Java 2 Platform SE 6)

0 回應: