HelloWorld 善戰(zhàn)者,求之于勢,不責(zé)于人;故能擇人而任勢。

          知止而后有定,定而后能靜,靜而后能安,安而后能慮,慮而后能得。物有本末,事有終始。知所先后,則近道矣。

            BlogJava :: 首頁 ::  :: 聯(lián)系 ::  :: 管理 ::
            167 隨筆 :: 1 文章 :: 40 評論 :: 0 Trackbacks
          package com.img;

          import java.awt.Rectangle;
          import java.awt.geom.AffineTransform;
          import java.awt.image.AffineTransformOp;
          import java.awt.image.BufferedImage;
          import java.awt.image.Raster;
          import java.io.File;

          import javax.imageio.ImageIO;

          public class ABMImage {
                /**
                 * 剪切圖片
                 * @param source 源圖片
                 * @param dest 目標(biāo)圖片
                 * @param x 切點x
                 * @param y 切點y
                 * @param width 要切的寬度
                 * @param height 要切的高度
                 */
                public static void cutFile(File source, File dest,
                        int x, int y, int width, int height) throws Exception{
                    if (!dest.exists())
                        dest.createNewFile();
                    BufferedImage bis = ImageIO.read(source);
                    int w = bis.getWidth();
                    int h = bis.getHeight();
                    if (w < width+x) {
                        width = w - x;
                    }
                    if (h < height+y) {
                        height = h - y;
                    }
                    BufferedImage bf = bis.getSubimage(x,y,width,height);
                    ImageIO.write(bf, "jpeg" , dest);
                }
             
                /**
                 * 縮放圖片
                 * @param source 源圖片
                 * @param dest 目標(biāo)圖片
                 * @param width 縮放后的寬度
                 * @param height 縮放后的高度
                 */
                public static void reduceImage(File source, File dest, int width, int height)
                    throws Exception{
                    BufferedImage bis = ImageIO.read(source);
                    int w = bis.getWidth();
                    int h = bis.getHeight();
                    double scale = (double)width/w;
                    int disW = 0;
                    int disH = 0;
                    if ((double)height/h > scale) {
                        scale = (double)height/h;
                        disW = (int) ((w*scale - width)/2);
                    } else {
                        disH = (int) ((h*scale - height)/2);
                    }
                    AffineTransform transform = new AffineTransform();
                    transform.setToScale(scale, scale);
                    System.out.println(scale);
                    AffineTransformOp ato = new AffineTransformOp(transform, null);
                    BufferedImage bid = new BufferedImage(width+disW*2, height+disH*2, BufferedImage.TYPE_3BYTE_BGR);
                    ato.filter(bis, bid);
                    if (width+disW*2>bid.getWidth())
                        width = bid.getWidth()-disW*2;
                    if (height+disH*2>bid.getHeight())
                        height = bid.getHeight()-disH*2;
                    BufferedImage bf = bid.getSubimage(disW, disH, width, height);
                    ImageIO.write(bf, "jpeg" , dest);
                }
             
                public static void main(String args[]) {
                    File f = new File("c:/樣品.jpg");
                    File f2 = new File("c:/1234.jpg");
                    try {
                        ABMImage.reduceImage(f, f2, 250, 150);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
          }



          </script>

          posted on 2007-08-13 18:51 helloworld2008 閱讀(422) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 宝坻区| 明溪县| 马龙县| 平舆县| 通榆县| 永泰县| 增城市| 资溪县| 大石桥市| 临邑县| 鄂尔多斯市| 赤水市| 古丈县| 哈尔滨市| 新沂市| 惠水县| 诸城市| 台北县| 宜丰县| 房产| 定远县| 万源市| 乌苏市| 苏尼特左旗| 太仆寺旗| 武威市| 枣强县| 寿光市| 庆云县| 沙洋县| 米易县| 拉萨市| 塘沽区| 漠河县| 镇赉县| 宁城县| 丰台区| 屏山县| 佳木斯市| 馆陶县| 六枝特区|