一图片压缩的java类,大小变小,宽度高度不变
来源:互联网 发布:污是什么意思网络语言 编辑:程序博客网 时间:2024/05/02 04:16
package Test;import java.awt.Color; import java.awt.Graphics2D;import java.awt.Image;import java.awt.image.BufferedImage;import java.awt.image.ConvolveOp;import java.awt.image.Kernel;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import javax.imageio.ImageIO;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGEncodeParam;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class ImageCompress { /** * 描述: * * @param path * 需要压缩的图片路径 * @param fileName * 要压缩的图片名称 * @param toFileName * 压缩后的图片名称 * @param scale * 压缩比例 不能大于1,默认0.5 * @param quality * 压缩品质介于0.1~1.0之间 * @param width * 压缩后的图片的宽度 * @param height * 压缩后的图片的高度 返回值:void */ static void imageCompress(String path, String fileName, String toFileName, float scale, float quality, int width, int height) { try { // 原图路径 原图名称 目标路径 压缩比率0.5 0.75 原图宽度 原图高度 long start = System.currentTimeMillis(); System.out.println(path + fileName); Image image = javax.imageio.ImageIO.read(new File(path + fileName)); int imageWidth = image.getWidth(null); int imageHeight = image.getHeight(null); if (scale > 0.5) scale = 0.5f;// 默认压缩比为0.5,压缩比越大,对内存要去越高,可能导致内存溢出 // 按比例计算出来的压缩比 float realscale = getRatio(imageWidth, imageHeight, width, height); float finalScale = Math.min(scale, realscale);// 取压缩比最小的进行压缩 /*imageWidth = (int) (finalScale * imageWidth); imageHeight = (int) (finalScale * imageHeight);*/ imageWidth = width; imageHeight=height; image = image.getScaledInstance(imageWidth, imageHeight, Image.SCALE_AREA_AVERAGING); // Make a BufferedImage from the Image. BufferedImage mBufferedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = mBufferedImage.createGraphics(); g2.drawImage(image, 0, 0, imageWidth, imageHeight, Color.white, null); g2.dispose(); float[] kernelData2 = { -0.125f, -0.125f, -0.125f, -0.125f, 2, -0.125f, -0.125f, -0.125f, -0.125f }; Kernel kernel = new Kernel(3, 3, kernelData2); ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); mBufferedImage = cOp.filter(mBufferedImage, null); FileOutputStream out = new FileOutputStream(path + toFileName); System.out.println(path + toFileName); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder .getDefaultJPEGEncodeParam(mBufferedImage); param.setQuality(quality, true);// 默认0.75 encoder.setJPEGEncodeParam(param); encoder.encode(mBufferedImage); out.close(); long end = System.currentTimeMillis(); System.out.println("图片:" + fileName + ",压缩时间:" + (end - start) + "ms"); } catch (FileNotFoundException fnf) { } catch (IOException ioe) { System.out.println("亲 哪里不对了"); //ioe.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } finally { } } public static void imageCompress(String path, String fileName, String toFileName, float scale, int width, int height) { imageCompress(path, fileName, toFileName, scale, 0.75f, width, height); } private static float getRatio(int width, int height, int maxWidth, int maxHeight) {// 获得压缩比率的方法 float Ratio = 1.0f; float widthRatio; float heightRatio; widthRatio = (float) maxWidth / width; heightRatio = (float) maxHeight / height; if (widthRatio < 1.0 || heightRatio < 1.0) { Ratio = widthRatio <= heightRatio ? widthRatio : heightRatio; } return Ratio; } public static byte[] convertImage2Type(String imageFile, String imageType) throws Exception {// 图片格式转换 File inputFile = new File(imageFile); ByteArrayOutputStream output = new ByteArrayOutputStream(); BufferedImage input = ImageIO.read(inputFile); ImageIO.write(input, imageType, output); return output.toByteArray(); } public static void convertImage2TypePng(String imageFile, String imageType) throws Exception {// 图片格式转换 File inputFile = new File(imageFile); int suffixIndex = imageFile.lastIndexOf("."); String suffix = imageFile.substring(suffixIndex + 1); if (!"png".equals(suffix)) {// 如果原图片的不是PNG格式的图片 String fileName = imageFile.substring(0, suffixIndex + 1) + "png"; File output = new File(fileName); BufferedImage input = ImageIO.read(inputFile); ImageIO.write(input, imageType, output); // 转换后删除原文件 if (inputFile.exists()) inputFile.delete(); } } public static void main(String[] args) { //需要压缩的图片路径 String firpath ="E:/upload/"; //要压缩的图片名称 String firpathname ="a.jpg"; //压缩后的图片名称 String laspathname ="b.jpg"; //压缩后的图片的宽度 int width=1124; //压缩后的图片的高度 int height=768; imageCompress(firpath, firpathname, laspathname, 0.5f,0.75f, width, height);}}
0 0
- 一图片压缩的java类,大小变小,宽度高度不变
- 一个关于图片压缩的java类,大小变小,宽度高度不变
- 压缩图片,word文档大小变小
- UIWebView 实现图片适应屏幕的宽度 文字内容的大小不变
- 获取图片宽度和高度的类
- 最新Javascript按比例压缩图片,解决onload无法读取图片的高度和宽度问题
- 使用ASP中取得图片宽度和高度的类(无组件)(方法一)
- 图像压缩成指定大小(高度/宽度)
- 图像压缩成指定大小(高度/宽度)
- Java读取图片文件的宽度和高度
- 根据屏幕的高度宽度,调整大小
- //压缩图片的大小
- 实现图片的任意压缩,可以根据宽度或者高度进行
- asp获取图片的高度和宽度的类
- java获取上传图片宽度高度
- Android 自定义imageview图片高度固定大小宽度按比例自适应
- Android 自定义imageview 图片宽度固定大小高度按比例自适应
- Android 自定义imageview 图片高度固定大小宽度按比例自适应
- 给存储过程传递一个表(XML运用)
- 高内聚,低耦合
- js prototype 详解
- matlab的fdatool生成的fdacoefs.h注解
- 处理select下拉框默认选中
- 一图片压缩的java类,大小变小,宽度高度不变
- Jetty服务器报错
- 一个男人关心的东西 决定了他的层次
- chrome扩展程序开发之在目标页面运行自己的JS
- PHP中冒号、endif、endwhile、endfor这些都是什么
- rpm2cpio和cpio
- Visual Studio 2010已安装,sql server 2008 management studio安装教程
- Ubuntu的wired Networks 变灰色,无法连接网络
- 重庆OA办公系统目前推出PC端、移动终端(安卓、IOS、平板)- 上弦科技