栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 自动生成略缩图示例代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 自动生成略缩图示例代码

当你要做一个图库的项目时,对图片大小、像素的控制是首先需要解决的难题。

一、单图生成略缩图
单图经过重新绘制,生成新的图片。新图可以按一定比例由旧图缩小,也可以规定其固定尺寸。
详细代码如下:
复制代码 代码如下:
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.util.Map;
public class PicChange {
   
    public BufferedImage resizeImage(BufferedImage im, float resizeTimes) {
       
        int width = im.getWidth();
        int height = im.getHeight();
       
        int toWidth = (int) (Float.parseFloat(String.valueOf(width)) / resizeTimes);
        int toHeight = (int) (Float.parseFloat(String.valueOf(height)) / resizeTimes);
       
        BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
        result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
        return result;
    }
   
    public BufferedImage zoomImage(BufferedImage im, float resizeTimes) {
       
        int width = im.getWidth();
        int height = im.getHeight();
       
        int toWidth = (int) (Float.parseFloat(String.valueOf(width)) * resizeTimes);
        int toHeight = (int) (Float.parseFloat(String.valueOf(height)) * resizeTimes);
       
        BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
        result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
        return result;
    }
    public boolean writeHighQuality(BufferedImage im, String fileFullPath) {
        try {
           
            FileOutputStream newimage = new FileOutputStream(fileFullPath+System.currentTimeMillis()+".jpg");
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im);
           
            jep.setQuality(1f, true);
            encoder.encode(im, jep);
          
            newimage.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    public static void main(String[] args) throws Exception{
        String inputFoler = "F:\pic" ;
        
        String outputFolder = "F:\picNew\"; 
       
        float times = 0.25f;
       
        PicChange r = new PicChange();
        File ff = new File("F:\pic\Chrysanthemum1.jpg");
        BufferedImage f = javax.imageio.ImageIO.read(ff);
        r.writeHighQuality(r.zoomImage(f,times), outputFolder);

    }
}

当你把上面的代码移至myEclipse时,可能会在引入一下工具包时出错。
复制代码 代码如下:
import com.sun.image.codec.

解决方法:只要把Windows - Preferences - Java - Compiler - Errors/Warnings里面的Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。

二、批量生成略缩图
批量生成略缩图,即将已知文件夹中后缀为.jpg 或其他图片后缀名的文件  统一转化后 放到 已定的另外文件夹中
复制代码 代码如下:
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.util.Map;
public class ResizeImage {
   
    public BufferedImage resizeImage(BufferedImage im, float resizeTimes) {
       
        int width = im.getWidth();
        int height = im.getHeight();
       
        int toWidth = (int) (Float.parseFloat(String.valueOf(width)) / resizeTimes);
        int toHeight = (int) (Float.parseFloat(String.valueOf(height)) / resizeTimes);
       
        BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
        result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
        return result;
    }
   
    public BufferedImage zoomImage(BufferedImage im, float resizeTimes) {
       
        int width = im.getWidth();
        int height = im.getHeight();
       
        int toWidth = (int) (Float.parseFloat(String.valueOf(width)) * resizeTimes);
        int toHeight = (int) (Float.parseFloat(String.valueOf(height)) * resizeTimes);
       
        BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
        result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
        return result;
    }
   
    public List getImageList(String path, String[] type) throws IOException{
        Map map = new HashMap();
        for(String s : type) {
            map.put(s,true);
        }
        List result = new ArrayList();
        File[] fileList = new File(path).listFiles();
        for (File f : fileList) {
            if(f.length() == 0)
                continue;
            if(map.get(getExtension(f.getName())) == null)
                continue;
            result.add(javax.imageio.ImageIO.read(f));
        }
        return result;
    }
   
    public boolean writeToDisk(BufferedImage im, String path, String fileName) {
        File f = new File(path + fileName);
        String fileType = getExtension(fileName);
        if (fileType == null)
            return false;
        try {
            ImageIO.write(im, fileType, f);
            im.flush();
            return true;
        } catch (IOException e) {
            return false;
        }
    }
    public boolean writeHighQuality(BufferedImage im, String fileFullPath) {
        try {
           
            FileOutputStream newimage = new FileOutputStream(fileFullPath+System.currentTimeMillis()+".jpg");
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im);
           
            jep.setQuality(1f, true);
            encoder.encode(im, jep);
          
            newimage.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
   
    public String getExtension(String fileName) {
        try {
            return fileName.split("\.")[fileName.split("\.").length - 1];
        } catch (Exception e) {
            return null;
        }
    }
    public static void main(String[] args) throws Exception{
        String inputFoler = "F:\pic" ;
        
        String outputFolder = "F:\picNew\"; 
       
        float times = 0.25f;
       
        ResizeImage r = new ResizeImage();
   List imageList = r.getImageList(inputFoler,new String[] {"jpg"});
        for(BufferedImage i : imageList) {
         r.writeHighQuality(r.zoomImage(i,times),outputFolder);
  }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/153047.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号