Java 实现生成图片缩略图,缩小高清图片详解编程语言
2023-06-13 09:20:29 时间
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 { /** * @param im 原始图像 * @param resizeTimes 需要缩小的倍数,缩小2倍为原来的1/2 ,这个数值越大,返回的图片越小 * @return 返回处理后的图像 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; /** * @param im 原始图像 * @param resizeTimes 倍数,比如0.5就是缩小一半,0.98等等double类型 * @return 返回处理后的图像 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; /** * @param path 要转化的图像的文件夹,就是存放图像的文件夹路径 * @param type 图片的后缀名组成的数组 * @return public List BufferedImage getImageList(String path, String[] type) throws IOException{ Map String,Boolean map = new HashMap String, Boolean for(String s : type) { map.put(s,true); List BufferedImage result = new ArrayList BufferedImage 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; /** * 把图片写到磁盘上 * @param im * @param path eg: C://home// 图片写入的文件夹地址 * @param fileName DCM1987.jpg 写入图片的名字 * @return 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;
/*输出到文件流*/ 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); /*近JPEG编码*/ newimage.close(); return true; } catch (Exception e) { return false; /** * 返回文件的文件后缀名 * @param fileName * @return 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{
ResizeImage r = new ResizeImage(); List BufferedImage imageList = r.getImageList(inputFoler,new String[] {"jpg"}); for(BufferedImage i : imageList) { r.writeHighQuality(r.zoomImage(i,times),outputFolder); 如果不能导入jar包就用下面解决方案: Access restriction:The type JPEGCodec is not accessible due to restriction on required library C:/Program Files/Java/jre6/lib/rt.jar 解决方法: Project - Properties - libraries, 先remove掉JRE System Library,然后再Add Library重新加入。 ============================================ 在Eclipse中处理图片,需要引入两个包: import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required library C:/Java/jre1.6.0_07/lib/rt.jar
此时解决办法: Eclipse 默认把这些受访问限制的API设成了ERROR。只要把Windows-Preferences-Java-Complicer- Errors/Warnings里面的Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。 优先选择第一种方法
import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; 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 { /** * @param im 原始图像 * @param resizeTimes 需要缩小的倍数,缩小2倍为原来的1/2 ,这个数值越大,返回的图片越小 * @return 返回处理后的图像 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; /** * @param im 原始图像 * @param resizeTimes 倍数,比如0.5就是缩小一半,0.98等等double类型 * @return 返回处理后的图像 public BufferedImage zoomImage(BufferedImage im) { /*原始图像的宽度和高度*/ /*调整后的图片的宽度和高度*/ int toWidth=64; int toHeight=64; /*新生成结果图片*/ 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; /** * @param path 要转化的图像的文件夹,就是存放图像的文件夹路径 * @param type 图片的后缀名组成的数组 * @return public List BufferedImage getImageList(String path, String[] type) throws IOException{ Map String,Boolean map = new HashMap String, Boolean for(String s : type) { map.put(s,true); List BufferedImage result = new ArrayList BufferedImage 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 BufferedImage getImage(String path) throws IOException{ return javax.imageio.ImageIO.read(new File(path)); /** * 把图片写到磁盘上 * @param im * @param path eg: C://home// 图片写入的文件夹地址 * @param fileName DCM1987.jpg 写入图片的名字 * @return 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;
FileOutputStream newimage = new FileOutputStream(fileFullPath); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage); JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im); /* 压缩质量 */ jep.setQuality(1f, true); encoder.encode(im, jep); /*近JPEG编码*/ newimage.close(); return true; } catch (Exception e) { return false; /** * 返回文件的文件后缀名 * @param fileName * @return 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{ /* System.out.println(123); String inputFoler = "F://testimages//yuan"; 这儿填写你存放要缩小图片的文件夹全地址 String outputFolder = "F://testimages//ys"; 这儿填写你转化后的图片存放的文件夹 float times = 0.5f; 这个参数是要转化成的倍数,如果是1就是转化成1倍
List BufferedImage imageList = r.getImageList(inputFoler,new String[] {"png"}); for(BufferedImage i : imageList) { r.writeHighQuality(r.zoomImage(i,times),outputFolder); System.out.println("..."); }*/ ResizeImage r=new ResizeImage(); String filepath="E://file//"; String filename="1.jpg"; BufferedImage im=r.getImage(filepath+filename); r.writeHighQuality(r.zoomImage(im), filepath+"s_"+filename);//为防止覆盖原图片,加s_区分是压缩以后的图片 }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10725.html
cjavawindows相关文章
- java 堆栈的声明_Java 堆栈[通俗易懂]
- Java实现文件夹删除方式总结详解编程语言
- Java Activiti6.0 spring5 SSM 工作流引擎 审批流程 java项目框架详解编程语言
- 数据结构基础 归并排序 java 实现详解编程语言
- java压缩与解压(Java.util.zip)详解编程语言
- Java正则表达式验证格式 手机、邮箱、字符串详解编程语言
- 使用java NIO进行读文件详解编程语言
- Java虚拟机-类文件详解编程语言
- java容器类总结(更新中。。)详解编程语言
- Java使用阻塞队列BlockingQueue实现生产者消费者详解编程语言
- java中排序实现简单的策略模式详解编程语言
- Java经典实例:正则表达式,替换匹配的文本详解编程语言
- JMX-Java Management Extensions详解编程语言
- Lombok 安装、入门 – 消除冗长的 java 代码详解编程语言
- java 数据结构与算法—栈详解编程语言
- 实现使用Java代码实现MySQL数据库连接(java连接mysql数据库代码)
- Java实现文本创建、删除、编辑内容详解编程语言
- JAVA list集合详解编程语言
- Kafka 使用Java实现数据的生产和消费demo详解编程语言
- Java 转JSON串详解编程语言
- 分布式Java实现Redis分布式:从入门到精通(java实现redis)
- Java配置Oracle实现稳定的跨平台数据库连接(java配置oracle)