Java获取图片大小 及 尺寸
/*** 获取图片大小 及 尺寸(测试)* @param filePath* @return* @throws FileNotFoundException* @throws IOException*/public static Float getImgWidthTest(String filePath) throws FileNotFoundException, IOException {//图片大小(文件大小)File picture = new File(filePath);Float size = Float.parseFloat(String.format("%.1f",picture.length() / 1024.0 / 1024.0));System.out.println("图片大小:" + size + "MB");//图片尺寸BufferedImage sourceImg = ImageIO.read(new FileInputStream(picture));System.out.println("宽:" + sourceImg.getWidth());System.out.println("高:" + sourceImg.getHeight());return size;}
图片压缩-新(解决打包时报错“程序包com.sun.image.codec.jpeg不存在”的问题)
package com.qyj.utils;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.*;import javax.imageio.ImageIO;import javax.servlet.http.HttpServlet;//import com.sun.image.codec.jpeg.JPEGCodec;//import com.sun.image.codec.jpeg.JPEGImageEncoder;public class CompressImgUtil extends HttpServlet {private static final long serialVersionUID = 1L;/*** 采用指定宽度、高度或压缩比例 的方式对图片进行压缩* like12 modified,0615,支持指定宽/高(高/宽自适应-使图片不失真)** @param imgsrc 源图片地址* @param out 目标图片地址* @param widthdist 压缩后图片宽度(当rate==null时,必传)* @param heightdist 压缩后图片高度(当rate==null时,必传)* @param rate 压缩比例*/public static void compressImg(String imgsrc, ByteArrayOutputStream out, int widthdist,int heightdist, Float rate) {try {File srcfile = new File(imgsrc);// 检查文件是否存在if (!srcfile.exists()) {return;}// 如果rate不为空说明是按比例压缩if (rate != null && rate > 0) {// 获取文件高度和宽度int[] results = getImgWidth(srcfile);if (results == null || results[0] == 0 || results[1] == 0) {return;} else {widthdist = (int) (results[0] * rate);heightdist = (int) (results[1] * rate);}}//like12 modified,0615,支持指定宽/高(高/宽自适应-使图片不失真)else if(rate == null && widthdist > 0 && heightdist == 0){// 获取文件高度和宽度int[] results = getImgWidth(srcfile);if (results == null || results[0] == 0 || results[1] == 0) {return;} else {rate = (float)(widthdist * 1.0 / results[0]);heightdist = (int) (results[1] * rate);}}else if(rate == null && widthdist == 0 && heightdist > 0){// 获取文件高度和宽度int[] results = getImgWidth(srcfile);if (results == null || results[0] == 0 || results[1] == 0) {return;} else {rate = (float)(heightdist * 1.0 / results[1]);widthdist = (int) (results[0] * rate);}}// 开始读取文件并进行压缩Image src = ImageIO.read(srcfile);BufferedImage tag = new BufferedImage((int) widthdist,(int) heightdist, BufferedImage.TYPE_INT_RGB);tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist,Image.SCALE_SMOOTH), 0, 0, null);//like12 modified,1113,解决打包时报错“程序包com.sun.image.codec.jpeg不存在”的问题/*//过时的实现类JPEGImageEncoder,是sun公司的私有实现JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(tag);*///使用统一的ImageIO进行图像格式文件的读写String formatName = imgsrc.substring(imgsrc.lastIndexOf(".") + 1);ImageIO.write(tag, formatName, out);} catch (IOException ex) {ex.printStackTrace();}}/*** 采用指定宽度、高度或压缩比例 的方式对图片进行压缩* like12 modified,0615,支持指定宽/高(高/宽自适应-使图片不失真)** @param imgsrc 源图片地址* @param imgdist 目标图片地址* @param widthdist 压缩后图片宽度(当rate==null时,必传)* @param heightdist 压缩后图片高度(当rate==null时,必传)* @param rate 压缩比例*/public static void compressImg(String imgsrc, String imgdist, int widthdist,int heightdist, Float rate) {try {File srcfile = new File(imgsrc);// 检查文件是否存在if (!srcfile.exists()) {return;}// 如果rate不为空说明是按比例压缩if (rate != null && rate > 0) {// 获取文件高度和宽度int[] results = getImgWidth(srcfile);if (results == null || results[0] == 0 || results[1] == 0) {return;} else {widthdist = (int) (results[0] * rate);heightdist = (int) (results[1] * rate);}}//like12 modified,0615,支持指定宽/高(高/宽自适应-使图片不失真)else if(rate == null && widthdist > 0 && heightdist == 0){// 获取文件高度和宽度int[] results = getImgWidth(srcfile);if (results == null || results[0] == 0 || results[1] == 0) {return;} else {rate = (float)(widthdist * 1.0 / results[0]);heightdist = (int) (results[1] * rate);}}else if(rate == null && widthdist == 0 && heightdist > 0){// 获取文件高度和宽度int[] results = getImgWidth(srcfile);if (results == null || results[0] == 0 || results[1] == 0) {return;} else {rate = (float)(heightdist * 1.0 / results[1]);widthdist = (int) (results[0] * rate);}}// 开始读取文件并进行压缩Image src = ImageIO.read(srcfile);BufferedImage tag = new BufferedImage((int) widthdist,(int) heightdist, BufferedImage.TYPE_INT_RGB);tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist,Image.SCALE_SMOOTH), 0, 0, null);//like12 modified,1113,解决打包时报错“程序包com.sun.image.codec.jpeg不存在”的问题/*//过时的实现类JPEGImageEncoder,是sun公司的私有实现FileOutputStream out = new FileOutputStream(imgdist);JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(tag);out.close();*///使用统一的ImageIO进行图像格式文件的读写String formatName = imgdist.substring(imgdist.lastIndexOf(".") + 1);ImageIO.write(tag, formatName, new File(imgdist));} catch (IOException ex) {ex.printStackTrace();}}/*** 获取图片宽度* * @param file* 图片文件* @return 宽度*/public static int[] getImgWidth(File file) {InputStream is = null;BufferedImage src = null;int result[] = { 0, 0 };try {is = new FileInputStream(file);src = ImageIO.read(is);result[0] = src.getWidth(null); // 得到源图宽result[1] = src.getHeight(null); // 得到源图高is.close();} catch (Exception e) {e.printStackTrace();}return result;}/*** 获取图片大小 及 尺寸(测试)* @param filePath* @return* @throws FileNotFoundException* @throws IOException*/public static Float getImgWidthTest(String filePath) throws FileNotFoundException, IOException {//图片大小(文件大小)File picture = new File(filePath);Float size = Float.parseFloat(String.format("%.1f",picture.length() / 1024.0 / 1024.0));System.out.println("图片大小:" + size + "MB");//图片尺寸BufferedImage sourceImg = ImageIO.read(new FileInputStream(picture));System.out.println("宽:" + sourceImg.getWidth());System.out.println("高:" + sourceImg.getHeight());return size;}public static void main(String[] args) throws FileNotFoundException, IOException {System.out.println("图片压缩测试...");/*//获取图片大小CompressImgUtil.getImgWidthTest("D:/test.jpg");*///图片压缩String srcFile = "D://test.jpg";String destFile = "D://tempCompressImg.jpg";//String destFile = "D://test.jpg";//相同路径可以覆盖File srcfile = new File(srcFile);System.out.println("压缩前srcfile size:" + srcfile.length());//压缩(相同路径可以覆盖)//pressImg(srcFile, destFile, 600, 800, null);//pressImg(srcFile, destFile, 110, 130, null);//pressImg(srcFile, destFile, 110, 0, null);//pressImg(srcFile, destFile, 0, 130, null);pressImg(srcFile, destFile, 0, 0, Float.parseFloat("0.5"));File distfile = new File(destFile);System.out.println("压缩后distfile size:" + distfile.length());System.out.println("Finish!");}}
图片压缩-老版本(新架构打包时会报错“程序包com.sun.image.codec.jpeg不存在”)
CompressImgUtil.java
package com.qyj.utils;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.*;import javax.imageio.ImageIO;import javax.servlet.http.HttpServlet;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class CompressImgUtil extends HttpServlet {private static final long serialVersionUID = 1L;/*** 采用指定宽度、高度或压缩比例 的方式对图片进行压缩(流模式 不保存中间文件)* like12 modified,0615,支持指定宽/高(高/宽自适应-使图片不失真)** @param imgsrc 源图片地址* @param out 目标图片地址* @param widthdist 压缩后图片宽度(当rate==null时,必传)* @param heightdist 压缩后图片高度(当rate==null时,必传)* @param rate 压缩比例*/public static void compressImg(String imgsrc, ByteArrayOutputStream out, int widthdist,int heightdist, Float rate) {try {File srcfile = new File(imgsrc);// 检查文件是否存在if (!srcfile.exists()) {return;}// 如果rate不为空说明是按比例压缩if (rate != null && rate > 0) {// 获取文件高度和宽度int[] results = getImgWidth(srcfile);if (results == null || results[0] == 0 || results[1] == 0) {return;} else {widthdist = (int) (results[0] * rate);heightdist = (int) (results[1] * rate);}}//like12 modified,0615,支持指定宽/高(高/宽自适应-使图片不失真)else if(rate == null && widthdist > 0 && heightdist == 0){// 获取文件高度和宽度int[] results = getImgWidth(srcfile);if (results == null || results[0] == 0 || results[1] == 0) {return;} else {rate = (float)(widthdist * 1.0 / results[0]);heightdist = (int) (results[1] * rate);}}else if(rate == null && widthdist == 0 && heightdist > 0){// 获取文件高度和宽度int[] results = getImgWidth(srcfile);if (results == null || results[0] == 0 || results[1] == 0) {return;} else {rate = (float)(heightdist * 1.0 / results[1]);widthdist = (int) (results[0] * rate);}}// 开始读取文件并进行压缩Image src = ImageIO.read(srcfile);BufferedImage tag = new BufferedImage((int) widthdist,(int) heightdist, BufferedImage.TYPE_INT_RGB);tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist,Image.SCALE_SMOOTH), 0, 0, null);JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(tag);} catch (IOException ex) {ex.printStackTrace();}}/*** 采用指定宽度、高度或压缩比例 的方式对图片进行压缩* like12 modified,0615,支持指定宽/高(高/宽自适应-使图片不失真)** @param imgsrc 源图片地址* @param imgdist 目标图片地址* @param widthdist 压缩后图片宽度(当rate==null时,必传)* @param heightdist 压缩后图片高度(当rate==null时,必传)* @param rate 压缩比例*/public static void compressImg(String imgsrc, String imgdist, int widthdist,int heightdist, Float rate) {try {File srcfile = new File(imgsrc);// 检查文件是否存在if (!srcfile.exists()) {return;}// 如果rate不为空说明是按比例压缩if (rate != null && rate > 0) {// 获取文件高度和宽度int[] results = getImgWidth(srcfile);if (results == null || results[0] == 0 || results[1] == 0) {return;} else {widthdist = (int) (results[0] * rate);heightdist = (int) (results[1] * rate);}}//like12 modified,0615,支持指定宽/高(高/宽自适应-使图片不失真)else if(rate == null && widthdist > 0 && heightdist == 0){// 获取文件高度和宽度int[] results = getImgWidth(srcfile);if (results == null || results[0] == 0 || results[1] == 0) {return;} else {rate = (float)(widthdist * 1.0 / results[0]);heightdist = (int) (results[1] * rate);}}else if(rate == null && widthdist == 0 && heightdist > 0){// 获取文件高度和宽度int[] results = getImgWidth(srcfile);if (results == null || results[0] == 0 || results[1] == 0) {return;} else {rate = (float)(heightdist * 1.0 / results[1]);widthdist = (int) (results[0] * rate);}}// 开始读取文件并进行压缩Image src = ImageIO.read(srcfile);BufferedImage tag = new BufferedImage((int) widthdist,(int) heightdist, BufferedImage.TYPE_INT_RGB);tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist,Image.SCALE_SMOOTH), 0, 0, null);FileOutputStream out = new FileOutputStream(imgdist);JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(tag);out.close();} catch (IOException ex) {ex.printStackTrace();}}/*** 获取图片宽度* * @param file* 图片文件* @return 宽度*/public static int[] getImgWidth(File file) {InputStream is = null;BufferedImage src = null;int result[] = { 0, 0 };try {is = new FileInputStream(file);src = ImageIO.read(is);result[0] = src.getWidth(null); // 得到源图宽result[1] = src.getHeight(null); // 得到源图高is.close();} catch (Exception e) {e.printStackTrace();}return result;}/*** 获取图片大小 及 尺寸(测试)* @param filePath* @return* @throws FileNotFoundException* @throws IOException*/public static Float getImgWidthTest(String filePath) throws FileNotFoundException, IOException {//图片大小(文件大小)File picture = new File(filePath);Float size = Float.parseFloat(String.format("%.1f",picture.length() / 1024.0 / 1024.0));System.out.println("图片大小:" + size + "MB");//图片尺寸BufferedImage sourceImg = ImageIO.read(new FileInputStream(picture));System.out.println("宽:" + sourceImg.getWidth());System.out.println("高:" + sourceImg.getHeight());return size;}public static void main(String[] args) throws FileNotFoundException, IOException {System.out.println("图片压缩测试...");/*//获取图片大小CompressImgUtil.getImgWidthTest("D:/test.jpg");*///图片压缩String srcFile = "D://test.jpg";String destFile = "D://tempCompressImg.jpg";//String destFile = "D://test.jpg";//相同路径可以覆盖File srcfile = new File(srcFile);System.out.println("压缩前srcfile size:" + srcfile.length());//压缩//pressImg(srcFile, destFile, 600, 800, null);//pressImg(srcFile, destFile, 110, 130, null);//pressImg(srcFile, destFile, 110, 0, null);//pressImg(srcFile, destFile, 0, 130, null);pressImg(srcFile, destFile, 0, 0, Float.parseFloat("0.5"));File distfile = new File(destFile);System.out.println("压缩后distfile size:" + distfile.length());System.out.println("Finish!");}}
如果觉得《Java获取图片大小 及 尺寸 图片压缩 jpg压缩》对你有帮助,请点赞、收藏,并留下你的观点哦!