欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAVA生成缩略图 博客分类: java  

程序员文章站 2024-03-22 09:58:34
...
package com.my.image;

import javax.imageio.ImageIO;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ImageUtils {

public static void main(String[] args) throws FileNotFoundException, IOException { 
     File picture = new File("C:/Users/Administrator/Desktop/buys/1.png");
        BufferedImage sourceImg =ImageIO.read(new FileInputStream(picture));
        System.out.println(picture.getPath().substring(picture.getPath().lastIndexOf(".")));
        System.out.println(picture.length()/1024.0); 
        System.out.println("width:"+sourceImg.getWidth()+"px"); 
        System.out.println("height:"+sourceImg.getHeight()+"px");
        System.out.println(picture.getName().substring(0,picture.getName().lastIndexOf("."))+"_pic"+picture.getName().substring(picture.getName().lastIndexOf(".")));
        try {
        String[] rets = ImageUtils.saveImage("C:/Users/Administrator/Desktop/buys/1_1.png","C:/Users/Administrator/Desktop/buys/1_3.png", 200);
        System.out.println("code:"+rets[0]+",Msg:"+rets[1]);
} catch (Exception e) {
e.printStackTrace();
}

//System.out.println(Double.parseDouble(String.format("%.1f",1000d/600)));

}

/**
  * 生成文件
  * @param image 源文件流
  * @param newWidth 缩小宽度
  * @param newHeigh 缩小高度
  * @return
  */
public static BufferedImage resize(BufferedImage image, int newWidth,int newHeigh) {
int type = image.getType();
BufferedImage newImage = null;
double sx = (double) newWidth / image.getWidth();
double sy = (double) newHeigh / image.getHeight();
if (type == BufferedImage.TYPE_CUSTOM) {
ColorModel cm = image.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(newWidth,newHeigh);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
newImage = new BufferedImage(cm, raster, alphaPremultiplied, null);
} else {
newImage = new BufferedImage(newWidth, newHeigh, type);
Graphics2D g = newImage.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(image, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
}

return newImage;
}

/**
  * 保存缩略图
  * @param fromFilePath 源文件路径
  * @param saveToFilePath 生成新文件路径
  * @param size 限制尺寸
  * @return
  * @throws Exception
  */
public static String[] saveImage(String fromFilePath, String saveToFilePath,int size) throws Exception {
String[] rets = new String[2];
BufferedImage srcImage;
String imgType = "JPEG";
double maxLeng = 0d;
double ratio = 0;
if (fromFilePath.toLowerCase().endsWith(".png")) {
imgType = "PNG";
}
else if (fromFilePath.toLowerCase().endsWith(".jpg")) {
imgType = "JPEG";
}
else if (fromFilePath.toLowerCase().endsWith(".bmp")) {
imgType = "BMP";
}
else if (fromFilePath.toLowerCase().endsWith(".gif")) {
imgType = "GIF";
} else {
rets[0]="0";
rets[1]="不支持此图片格式!";
return rets;
}
File saveFile = new File(saveToFilePath);
File fromFile = new File(fromFilePath);
srcImage = ImageIO.read(fromFile);
int fileSize = 2048;
if (fromFile.length()/1024.0>fileSize) {
rets[0]="0";
rets[1]="图片过大,请上传不大于2M的图片!";
return rets;
}
double width = (double)srcImage.getWidth();
double height = (double)srcImage.getHeight();
if (width > 0 || height > 0) {
//计算比例
if (width > height) {
maxLeng = width;
ratio = maxLeng/size;
} else {
maxLeng = height;
ratio = maxLeng/size;
}
if (maxLeng > size) {
int newWidth = (int) (width/ratio);
int newHeight = (int) (height/ratio);
srcImage = resize(srcImage, newWidth, newHeight);
}
}
boolean isSuccess = ImageIO.write(srcImage, imgType, saveFile);
if (isSuccess) {
rets[0]="1";
rets[1]="上传成功!";
return rets;
}else {
rets[0]="0";
rets[1]="上传发生错误!";
return rets;
}
}

}

以上代码复制可用