package com.cnc.wsad.taskgenerator.frame; |
import java.awt.Image; |
import java.awt.geom.AffineTransform; |
import java.awt.image.AffineTransformOp; |
import java.awt.image.BufferedImage; |
import java.io.File; |
import java.io.IOException; |
import javax.imageio.ImageIO; |
import javax.swing.Icon; |
import javax.swing.ImageIcon; |
public class picturetest { |
|
public static void getFixedIcon(String filePath, int width, int height) throws Exception{ |
File f = new File(filePath); |
BufferedImage bi = ImageIO.read(f); |
double wRatio = ( new Integer(width)).doubleValue() / bi.getWidth(); //宽度的比例 |
double hRatio = ( new Integer(height)).doubleValue() / bi.getHeight(); //高度的比例 |
Image image = bi.getScaledInstance(width,height,Image.SCALE_SMOOTH); //设置图像的缩放大小 |
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(wRatio,hRatio), null ); //设置图像的缩放比例 |
image = op.filter(bi, null ); |
int lastLength = filePath.lastIndexOf( "." ); |
String subFilePath = filePath.substring( 0 ,lastLength); //得到图片输出路径 |
String fileType = filePath.substring(lastLength); //图片类型 |
File zoomFile = new File(subFilePath + "_" + width + "_" + height + fileType); |
Icon ret = null ; |
try { |
ImageIO.write((BufferedImage)image, "jpg" , zoomFile); |
ret = new ImageIcon(zoomFile.getPath()); |
} catch (Exception e){ |
e.printStackTrace(); |
} |
} |
|
public static void getfiled(String path){ |
File file = new File(path); |
File [] files = file.listFiles(); |
for ( int i = 0 ; i < files.length; i++) |
{ |
File file1 = files[i]; |
try { |
int height = (ImageIO.read(file1)).getHeight( null ); |
int width = (ImageIO.read(file1)).getWidth( null ); |
|
String tts = path+ "/" +file1.getName(); //根据后缀判断 |
System.out.println(tts+ "||" +height); |
getFixedIcon(tts,width,height); |
|
} catch (IOException e) { |
e.printStackTrace(); |
} catch (Exception e) { |
e.printStackTrace(); |
} |
} |
} |
|
public static void main(String arg[]) { |
getfiled( "f:/游玩" ); |
} |
} |