当前位置: 代码迷 >> J2SE >> 散分了:改变图像的大小解决思路
  详细解决方案

散分了:改变图像的大小解决思路

热度:295   发布时间:2016-04-24 16:56:36.0
散分了:改变图像的大小
1.一个图像的大小的为:100*100    格式为jpg
要把它变为32*32大小作为图标在组件里显示出来,比如在JLable里显示出来,可何实现?
2.如何实现像Windows文件浏览器一样显示大图标、小图标、列表、详细信息?用哪个组件?

答完散分,谢谢!

------解决方案--------------------
/**
* 等比例缩放图像, 本方法目前支持三个图片格式jpg, gif, png, 所有生成都被转化为jpg
*fullname 图片路径
* path 目标路径(不带文件名)
*prtfix 生成的文件名的前缀
*photomaxw 文件宽度范围
*photomaxh 文件高度范围
*/
public static String getSmallJpg(String fullname, String path, String prefix,
float photomaxw, float photomaxh) throws
Exception {
File filePath = null;
try {
File file = new File(fullname); //读入文件
if (!file.exists()) {
return null;
}
else {
//上传flash
if (fullname.endsWith( "swf ")) {
String fileName = file.getName();
String filePiexName = FileUtil.getFilePrefixName(fileName);
String newPmgPath = path + "/ " + prefix + "_ " + filePiexName + ".swf ";
FileUtil.copyFile(fullname, newPmgPath);
File newFile = new File(newPmgPath);
return newFile.getName();
}
}

Image src = javax.imageio.ImageIO.read(file); //构造Image对象
// System.out.println( "压缩文件名: " + " " + file.getAbsoluteFile() );
float wideth = src.getWidth(null); //得到源图宽
float height = src.getHeight(null); //得到源图长
System.out.println( "class = " + src.getClass());

System.out.println( "源文件宽度: " + wideth + "¥ "); //测试
System.out.println( "源文件高度: " + height + "¥ ");

if (wideth > photomaxw) {
float mw = wideth;
System.out.println( "限制高度: " + photomaxh);
System.out.println( "限制宽度: " + photomaxw);
wideth = photomaxw;
height = height * photomaxw / mw;

System.out.println( "收缩后的实际宽度: " + wideth + "● "); //测试
System.out.println( "收缩后的实际高度: " + height + "● ");

if (height > photomaxh) {
float mo = height;
height = photomaxh;
wideth = wideth * photomaxh / mo;
}
}
else if (wideth < photomaxw || wideth == photomaxw) {
float mo = height;
if (height > photomaxh) {
height = photomaxh;
wideth = wideth * height / mo;
}
}

if (wideth == 0) {
wideth = src.getWidth(null);
}

if (height == 0) {
height = src.getHeight(null);
}

System.out.println( "最终宽度: " + wideth + "● "); //测试
System.out.println( "最终高度: " + height + "● ");

if (height == 0 || wideth == 0) {
return null;
}

BufferedImage tag = new BufferedImage( (int) wideth, (int) height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, (int) wideth, (int) height, null); //绘制后的图

String fileName = file.getName();

File newPmgPathDir = new File(path);
if (!newPmgPathDir.exists() || !newPmgPathDir.isDirectory()) {
newPmgPathDir.mkdirs();
}

String filePiexName = FileUtil.getFilePrefixName(fileName);
String newPmgPath = path + "/ " + prefix + "_ " + filePiexName + ".jpg ";
  相关解决方案