java生成缩略图代码

方法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;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值