java图片压缩技术

package com.want.util;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import javax.imageio.ImageIO;

/**

 * * @author WQ * @date 2011-01-14 * @versions 1.0 图片压缩工具类 提供的方法中可以设定生成的

 * 缩略图片的大小尺寸等

 */

public class ImgCompress {

 public static File thefiles = null;

/** * 图片文件读取 * * @param srcImgPath * @return 

 * @throws Exception */

public static BufferedImage InputImage(String srcImgPath) throws Exception {

BufferedImage srcImage = null;

FileInputStream in = new FileInputStream(srcImgPath);

srcImage = javax.imageio.ImageIO.read(in);

return srcImage;

}

// public static void main(String args[]) {

// try {

// //获取文件路径可有多张图片

// Map<Integer, String> map = readfile("C:/Users/liquan/Desktop/img", null);

//循环遍历Map

// for (int i = 0; i < map.size(); i++) {

// System.out.println(map.get(i) + " ==" + i);

// System.out.println();

// String oldpath = map.get(i);

// compressImage(map.get(i), "C:/Users/liquan/Desktop/ii/ii_" + i + ".jpg", 100, 100);

// }

// } catch (Exception ex) {

// }

// System.out.println("ok");

//单张图片压缩 初始文件路径 输出文件路径 宽      高

//这个图片类型是能改的  什么格式都可以读出,都可以写成其他类型

// try {

// compressImage("/Users/00310978/Desktop/img/", "/Users/00310978/Desktop/img/compress/屏幕快照 2017-04-19 下午2.10.59.png");

// } catch (Exception e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// }

// System.out.println("OK");

// }

/**

 * * 将图片按照指定的图片尺寸压缩 * * @param srcImgPath :源图片路径 * @param outImgPath *

 * :输出的压缩图片的路径 * @param new_w * :压缩后的图片宽 * @param new_h * :压缩后的图片高

 * @throws Exception 

 */

public static void compressImage(String srcImgPath, String outImgPath

) throws Exception {

//更改后文件宽

int new_w = 0;

//更改后文件宽

int new_h = 0;

//源文件宽

int ynew_w = 0;

//原文件高

int ynew_h = 0;

File file = new File(srcImgPath);

try {

BufferedImage sourceImg =ImageIO.read(new FileInputStream(file));

//获取源文件宽

ynew_w=sourceImg.getWidth();

//获取源文件高

ynew_h = sourceImg.getHeight();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//获取文件大小

long size =file.length();

//小于50K图片大小不变

if(size<51200){

new_w=ynew_w;

new_h=ynew_h;

//大于等于50K小于1M缩小为原图片的0.8倍

}else if(81920<=size&&size<1048576){

new_w = (int)Math.floor(ynew_w*0.8);

new_h = (int)Math.floor(ynew_h*0.8);

//大于等于1M小于3M缩小为原图片的0.45倍

}else if(1048576<=size &&size<3145728){

new_w = (int)Math.floor(ynew_w*0.5);

new_h = (int)Math.floor(ynew_h*0.5);

//大于等于3M缩小于8M为原图片的0.35倍

}else if(3145728<=size && size<8388608){

new_w = (int)Math.floor(ynew_w*0.35);

new_h = (int)Math.floor(ynew_h*0.35);

//大于等于8M缩为原图片的0.2倍

}else if(8388608<=size){

new_w = (int)Math.floor(ynew_w*0.2);

new_h = (int)Math.floor(ynew_h*0.2);

}

BufferedImage src = InputImage(srcImgPath);

disposeImage(src, outImgPath, new_w, new_h);

}

/**

 * * 指定长或者宽的最大值来压缩图片 * * @param srcImgPath * :源图片路径 * @param outImgPath *

 * :输出的压缩图片的路径 * @param maxLength * :长或者宽的最大值

 * @throws Exception 

 */

public static void compressImage(String srcImgPath, String outImgPath,

int maxLength) throws Exception {

// 得到图片

BufferedImage src = InputImage(srcImgPath);

if (null != src) {

int old_w = src.getWidth();

// 得到源图宽

int old_h = src.getHeight();

// 得到源图长

int new_w = 0;

// 新图的宽

int new_h = 0;

// 新图的长

// 根据图片尺寸压缩比得到新图的尺寸

if (old_w > old_h) {

// 图片要缩放的比例

new_w = maxLength;

new_h = (int) Math.round(old_h * ((float) maxLength / old_w));

} else {

new_w = (int) Math.round(old_w * ((float) maxLength / old_h));

new_h = maxLength;

}

disposeImage(src, outImgPath, new_w, new_h);

}

}

/** * 处理图片 * * @param src * @param outImgPath * @param new_w * @param new_h */

private synchronized static void disposeImage(BufferedImage src,

String outImgPath, int new_w, int new_h) {

// 得到图片

int old_w = src.getWidth();

// 得到源图宽

int old_h = src.getHeight();

// 得到源图长

BufferedImage newImg = null;

// 判断输入图片的类型

switch (src.getType()) {

case 13:

// png,gifnewImg = new BufferedImage(new_w, new_h,

// BufferedImage.TYPE_4BYTE_ABGR);

break;

default:

newImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);

break;

}

Graphics2D g = newImg.createGraphics();

// 从原图上取颜色绘制新图

g.drawImage(src, 0, 0, old_w, old_h, null);

g.dispose();

// 根据图片尺寸压缩比得到新图的尺寸

newImg.getGraphics().drawImage(

src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0, 0,

null);

// 调用方法输出图片文件

OutImage(outImgPath, newImg);

}

/**

 * * 将图片文件输出到指定的路径,并可设定压缩质量 * * @param outImgPath * @param newImg * @param

 * per

 */

private static void OutImage(String outImgPath, BufferedImage newImg) {

// 判断输出的文件夹路径是否存在,不存在则创建

File file = new File(outImgPath);

if (!file.getParentFile().exists()) {

file.getParentFile().mkdirs();

}// 输出到文件流

try {

thefiles = file;

ImageIO.write(newImg,

outImgPath.substring(outImgPath.lastIndexOf(".") + 1),

new File(outImgPath));

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

 * 获取路径里面照片路径

 * @param filepath

 * @param pathMap

 * @return

 * @throws Exception

 */

public static Map<Integer, String> readfile(String filepath,

Map<Integer, String> pathMap) throws Exception {

if (pathMap == null) {

pathMap = new HashMap<Integer, String>();

}

File file = new File(filepath);

// 文件

if (!file.isDirectory()) {

pathMap.put(pathMap.size(), file.getPath());

} else if (file.isDirectory()) { // 如果是目录, 遍历所有子目录取出所有文件名

String[] filelist = file.list();

for (int i = 0; i < filelist.length; i++) {

File readfile = new File(filepath + "/" + filelist[i]);

if (!readfile.isDirectory()) {

pathMap.put(pathMap.size(), readfile.getPath());

} else if (readfile.isDirectory()) { // 子目录的目录

readfile(filepath + "/" + filelist[i], pathMap);

}

}

}

return pathMap;

}

}

发布了47 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_34233080/article/details/83655871