java生成缩略图代码
2005-10-26 14:08
459 查看
方法1:
Image src = ToolKit.getToolKit().createImage("文件路径");
然后先生成一个BufferedImage bi作为画布.
BufferedImage bi = new BufferedImage(目标宽, 目标高,BufferedImage.TYPE_INT_RGB);
得到它的Graphics对象:
Graphics g = bi.getGraphics();
然后往这个画而上画原图就行了:
g.grawImage(src,0,0,目标宽,目标高,null);
找一个编码类,如JPEGEncoder,GIFEncoder
把bi编码输出就行了.
方法2:
import javax.imageio.ImageIO;
import javax.imageio.IIOException;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.io.File;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;
public class Test {
public static void main (String argv[]) {
try {
File fi = new File("C:/1.jpg"); //大图文件
File fo = new File("C:/2.jpg"); //将要转换出的小图文件
AffineTransform transform = new AffineTransform();
BufferedImage bis = ImageIO.read(fi);
int w = bis.getWidth();
int h = bis.getHeight();
double scale = (double)w/h;
int nw = 120;
int nh = (nw * h) / w;
if(nh>120) {
nh = 120;
nw = (nh * w) / h;
}
double sx = (double)nw / w;
double sy = (double)nh / h;
transform.setToScale(sx,sy);
AffineTransformOp ato = new AffineTransformOp(transform, null);
BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
ato.filter(bis,bid);
ImageIO.write(bid, "jpeg", fo);
} catch(Exception e) {
e.printStackTrace();
}
}
}
* 照片缩放处理 */public class ScaleImageImpl {
/** * 照片缩放处理 * @param imgWidth 照片的宽度 * @param imgHeight 照片的高度 * @param fromdir 照片的原始目录 * @param todir 处理后的照片存放目录 * @param imgfile 原始照片 * @param sysimgfile 处理后的照片文件名前缀 * @return * @throws Exception */ public boolean createScaleImage(String imgWidth, String imgHeight, String fromdir, String todir, String imgfile, String sysimgfile) throws Exception { boolean isSuc = true; int width = 0; int height = 0; if (imgWidth == null || "".equals(imgWidth)) { width = 120; } else { width = Integer.parseInt(imgWidth); } if (imgHeight == null || "".equals(imgHeight)) { height = 120; } else { height = Integer.parseInt(imgHeight); } File F = new File(fromdir, imgfile); if (!F.isFile()) { throw new Exception(F + " 不是照片文件!"); } BufferedImage Bi = ImageIO.read(F); double sx = width*1.0 / Bi.getWidth(); double sy = height*1.0 / Bi.getHeight(); String ext = "jpg"; File ThF = new File(todir, sysimgfile + "." + ext); Image Itemp = null; //Image Itemp = Bi.getScaledInstance(width, height, Bi.SCALE_SMOOTH); AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(sx, sy), null); Itemp = op.filter(Bi, null); try { ImageIO.write((BufferedImage) Itemp, ext, ThF); F.delete(); } catch (Exception ex) { throw new Exception(" ImageIo.write error in createScaleImage.: " + ex.getMessage()); } return isSuc;}在改写上面的方法后/**
* 照片缩放处理
* @param imgWidth 照片的宽度
* @param imgHeight 照片的高度
* @param filename 修改图片文件
* @return
* @throws Exception
*/ public boolean createScaleImage2(String imgWidth, String imgHeight,
String filename) throws Exception {
boolean isSuc = true;
int width = 0;
int height = 0;
if (imgWidth == null || "".equals(imgWidth)) {
width = 120;
} else {
width = Integer.parseInt(imgWidth);
}
if (imgHeight == null || "".equals(imgHeight)) {
height = 120;
} else {
height = Integer.parseInt(imgHeight);
}
File F = new File(filename);
if (!F.isFile()) {
throw new Exception(F + " 不是照片文件!");
}
BufferedImage Bi = ImageIO.read(F);
double sx = width*1.0 / Bi.getWidth();
double sy = height*1.0 / Bi.getHeight();
String ext = "jpg";
Image Itemp = null;
//Image Itemp = Bi.getScaledInstance(width, height, Bi.SCALE_SMOOTH);
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(sx, sy), null);
Itemp = op.filter(Bi, null);
try {
ImageIO.write((BufferedImage) Itemp, ext, F);
} catch (Exception ex) {
throw new Exception(" ImageIo.write error in createScaleImage.: " + ex.getMessage());
}
return isSuc;
}
Image src = ToolKit.getToolKit().createImage("文件路径");
然后先生成一个BufferedImage bi作为画布.
BufferedImage bi = new BufferedImage(目标宽, 目标高,BufferedImage.TYPE_INT_RGB);
得到它的Graphics对象:
Graphics g = bi.getGraphics();
然后往这个画而上画原图就行了:
g.grawImage(src,0,0,目标宽,目标高,null);
找一个编码类,如JPEGEncoder,GIFEncoder
把bi编码输出就行了.
方法2:
import javax.imageio.ImageIO;
import javax.imageio.IIOException;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.io.File;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;
public class Test {
public static void main (String argv[]) {
try {
File fi = new File("C:/1.jpg"); //大图文件
File fo = new File("C:/2.jpg"); //将要转换出的小图文件
AffineTransform transform = new AffineTransform();
BufferedImage bis = ImageIO.read(fi);
int w = bis.getWidth();
int h = bis.getHeight();
double scale = (double)w/h;
int nw = 120;
int nh = (nw * h) / w;
if(nh>120) {
nh = 120;
nw = (nh * w) / h;
}
double sx = (double)nw / w;
double sy = (double)nh / h;
transform.setToScale(sx,sy);
AffineTransformOp ato = new AffineTransformOp(transform, null);
BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
ato.filter(bis,bid);
ImageIO.write(bid, "jpeg", fo);
} catch(Exception e) {
e.printStackTrace();
}
}
}
* 照片缩放处理 */public class ScaleImageImpl {
/** * 照片缩放处理 * @param imgWidth 照片的宽度 * @param imgHeight 照片的高度 * @param fromdir 照片的原始目录 * @param todir 处理后的照片存放目录 * @param imgfile 原始照片 * @param sysimgfile 处理后的照片文件名前缀 * @return * @throws Exception */ public boolean createScaleImage(String imgWidth, String imgHeight, String fromdir, String todir, String imgfile, String sysimgfile) throws Exception { boolean isSuc = true; int width = 0; int height = 0; if (imgWidth == null || "".equals(imgWidth)) { width = 120; } else { width = Integer.parseInt(imgWidth); } if (imgHeight == null || "".equals(imgHeight)) { height = 120; } else { height = Integer.parseInt(imgHeight); } File F = new File(fromdir, imgfile); if (!F.isFile()) { throw new Exception(F + " 不是照片文件!"); } BufferedImage Bi = ImageIO.read(F); double sx = width*1.0 / Bi.getWidth(); double sy = height*1.0 / Bi.getHeight(); String ext = "jpg"; File ThF = new File(todir, sysimgfile + "." + ext); Image Itemp = null; //Image Itemp = Bi.getScaledInstance(width, height, Bi.SCALE_SMOOTH); AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(sx, sy), null); Itemp = op.filter(Bi, null); try { ImageIO.write((BufferedImage) Itemp, ext, ThF); F.delete(); } catch (Exception ex) { throw new Exception(" ImageIo.write error in createScaleImage.: " + ex.getMessage()); } return isSuc;}在改写上面的方法后/**
* 照片缩放处理
* @param imgWidth 照片的宽度
* @param imgHeight 照片的高度
* @param filename 修改图片文件
* @return
* @throws Exception
*/ public boolean createScaleImage2(String imgWidth, String imgHeight,
String filename) throws Exception {
boolean isSuc = true;
int width = 0;
int height = 0;
if (imgWidth == null || "".equals(imgWidth)) {
width = 120;
} else {
width = Integer.parseInt(imgWidth);
}
if (imgHeight == null || "".equals(imgHeight)) {
height = 120;
} else {
height = Integer.parseInt(imgHeight);
}
File F = new File(filename);
if (!F.isFile()) {
throw new Exception(F + " 不是照片文件!");
}
BufferedImage Bi = ImageIO.read(F);
double sx = width*1.0 / Bi.getWidth();
double sy = height*1.0 / Bi.getHeight();
String ext = "jpg";
Image Itemp = null;
//Image Itemp = Bi.getScaledInstance(width, height, Bi.SCALE_SMOOTH);
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(sx, sy), null);
Itemp = op.filter(Bi, null);
try {
ImageIO.write((BufferedImage) Itemp, ext, F);
} catch (Exception ex) {
throw new Exception(" ImageIo.write error in createScaleImage.: " + ex.getMessage());
}
return isSuc;
}
相关文章推荐
- Java 生成缩略图的代码
- Java生成缩略图之Thumbnailator
- php生成缩略图质量较差解决方法代码示例
- Java代码生成时间戳
- 无需编写Java代码就能生成增删改查功能的CZTZ-JavaEE平台介绍
- java画布生成验证码2.0与第三方代码生成
- web service开发时,wsdl2java 客户端代码的生成
- 使用jaxb根据xsd逆向生成java代码
- java生成动态gif格式与png格式的验证码(代码1)
- 转根据wsdl生成java代码的方法
- Java缩略图生成库之Thumbnailator应用说明
- powerDesigner类图生成JAVA代码
- java 生成EXCEL完整实现(附加代码)
- Java代码生成利器之rapid-generate应用二
- Java代码生成利器之rapid-generate应用三
- java中dom4j解析xml生成xml文档,dom4j工具压缩包原代码
- java代码(生成long类型数字)
- asp.net生成高质量缩略图通用函数(c#代码)
- asp.net上传图片自动生成缩略图功能代码
- idea创建webservice客户端、服务端,并将生成的java的代码迁移其他项目