亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問板塊 發(fā)新帖
查看: 1006 | 回復(fù): 0
打印 上一主題 下一主題

java 圖片切割,縮放,轉(zhuǎn)換類型等等 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2006-06-26 13:54 |只看該作者 |倒序?yàn)g覽

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.Graphics;
import java.awt.color.ColorSpace;
import javax.imageio.ImageIO;
public class ImageCut {
/**
  * 縮放圖像
  *
  * @param srcImageFile
  *            源圖像文件地址
  * @param result
  *            縮放后的圖像地址
  * @param scale
  *            縮放比例
  * @param flag
  *            縮放選擇:true 放大; false 縮小;
  */
public static void scale(String srcImageFile, String result, int scale,
   boolean flag) {
  try {
   BufferedImage src = ImageIO.read(new File(srcImageFile)); // 讀入文件
   int width = src.getWidth(); // 得到源圖寬
   int height = src.getHeight(); // 得到源圖長(zhǎng)
   if (flag) {
    // 放大
    width = width * scale;
    height = height * scale;
   } else {
    // 縮小
    width = width / scale;
    height = height / scale;
   }
   Image image = src.getScaledInstance(width, height,
     Image.SCALE_DEFAULT);
   BufferedImage tag = new BufferedImage(width, height,
     BufferedImage.TYPE_INT_RGB);
   Graphics g = tag.getGraphics();
   g.drawImage(image, 0, 0, null); // 繪制縮小后的圖
   g.dispose();
   ImageIO.write(tag, "JPEG", new File(result));// 輸出到文件流
  } catch (IOException e) {
   e.printStackTrace();
  }
}
/**
  * 圖像切割
  *
  * @param srcImageFile
  *            源圖像地址
  * @param descDir
  *            切片目標(biāo)文件夾
  * @param destWidth
  *            目標(biāo)切片寬度
  * @param destHeight
  *            目標(biāo)切片高度
  */
public static void cut(String srcImageFile, String descDir, int destWidth,
   int destHeight) {
  try {
   Image img;
   ImageFilter cropFilter;
   // 讀取源圖像
   BufferedImage bi = ImageIO.read(new File(srcImageFile));
   int srcWidth = bi.getHeight(); // 源圖寬度
   int srcHeight = bi.getWidth(); // 源圖高度
   if (srcWidth > destWidth && srcHeight > destHeight) {
    Image image = bi.getScaledInstance(srcWidth, srcHeight,
      Image.SCALE_DEFAULT);
    destWidth = 200; // 切片寬度
    destHeight = 150; // 切片高度
    int cols = 0; // 切片橫向數(shù)量
    int rows = 0; // 切片縱向數(shù)量
    // 計(jì)算切片的橫向和縱向數(shù)量
    if (srcWidth % destWidth == 0) {
     cols = srcWidth / destWidth;
    } else {
     cols = (int) Math.floor(srcWidth / destWidth) + 1;
    }
    if (srcHeight % destHeight == 0) {
     rows = srcHeight / destHeight;
    } else {
     rows = (int) Math.floor(srcHeight / destHeight) + 1;
    }
    // 循環(huán)建立切片
    // 改進(jìn)的想法:是否可用多線程加快切割速度
    for (int i = 0; i
      cropFilter = new CropImageFilter(j * 200, i * 150,
        destWidth, destHeight);
      img = Toolkit.getDefaultToolkit().createImage(
        new FilteredImageSource(image.getSource(),
          cropFilter));
      BufferedImage tag = new BufferedImage(destWidth,
        destHeight, BufferedImage.TYPE_INT_RGB);
      Graphics g = tag.getGraphics();
      g.drawImage(img, 0, 0, null); // 繪制縮小后的圖
      g.dispose();
      // 輸出為文件
      ImageIO.write(tag, "JPEG", new File(descDir
        + "pre_map_" + i + "_" + j + ".jpg"));
     }
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
}
/**
  * 圖像類型轉(zhuǎn)換
     * GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)
     */
public static void convert(String source, String result) {
  try {
   File f = new File(source);
   f.canRead();
   f.canWrite();
   BufferedImage src = ImageIO.read(f);
   ImageIO.write(src, "JPG", new File(result));
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
}
/**
  * 彩色轉(zhuǎn)為黑白
  *
  * @param source
  * @param result
  */
public static void gray(String source, String result) {
  try {
   BufferedImage src = ImageIO.read(new File(source));
   ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
   ColorConvertOp op = new ColorConvertOp(cs, null);
   src = op.filter(src, null);
   ImageIO.write(src, "JPEG", new File(result));
  } catch (IOException e) {
   e.printStackTrace();
  }
}
/**
  * @param args
  */
public static void main(String[] args) {
  //cut("e:/1.jpg", "e:/t/", 200, 150);
}
}


本文來(lái)自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u/8718/showart_132832.html
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP