压缩图片(三)
来源:互联网 发布:新开的淘宝店怎么经营 编辑:程序博客网 时间:2024/04/29 15:55
import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.math.BigDecimal;import java.math.MathContext;import java.util.ArrayList;/*** * 对图片进行操作 * * @author chenzheng_java * @since 2011/7/29 * */public class ImageHelper { private static ImageHelper imageHelper = null; public static ImageHelper getImageHelper() { if (imageHelper == null) { imageHelper = new ImageHelper(); } return imageHelper; } public static void main(String[] args) { getImageHelper().scaleImage("F:\\oldImage\\20155913wm5y.png","F:\\newImage\\shijian.png",1.2,"jpg"); } /*** * 按指定的比例缩放图片 * * @param sourceImagePath * 源地址 * @param destinationPath * 改变大小后图片的地址 * @param scale * 缩放比例,如1.2 */ public static void scaleImage(String sourceImagePath, String destinationPath, double scale,String format) { File file = new File(sourceImagePath); BufferedImage bufferedImage; try { bufferedImage = ImageIO.read(file); int width = bufferedImage.getWidth(); int height = bufferedImage.getHeight(); width = parseDoubleToInt(width * scale); height = parseDoubleToInt(height * scale); Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH); BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics graphics = outputImage.getGraphics(); graphics.drawImage(image, 0, 0, null); graphics.dispose(); ImageIO.write(outputImage, format, new File(destinationPath)); outputImage.flush(); } catch (IOException e) { System.out.println("scaleImage方法压缩图片时出错了"); e.printStackTrace(); } } /*** * 将图片缩放到指定的高度或者宽度 * @param sourceImagePath 图片源地址 * @param destinationPath 压缩完图片的地址 * @param width 缩放后的宽度 * @param height 缩放后的高度 * @param auto 是否自动保持图片的原高宽比例 * @param format 图图片格式 例如 jpg */ public static void scaleImageWithParams(String sourceImagePath, String destinationPath, int width, int height, boolean auto,String format) { try { File file = new File(sourceImagePath); BufferedImage bufferedImage = null; bufferedImage = ImageIO.read(file); if (auto) { ArrayList<Integer> paramsArrayList = getAutoWidthAndHeight(bufferedImage,width,height); width = paramsArrayList.get(0); height = paramsArrayList.get(1); System.out.println("自动调整比例,width="+width+" height="+height); } Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_DEFAULT); BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics graphics = outputImage.getGraphics(); graphics.drawImage(image, 0, 0, null); graphics.dispose(); ImageIO.write(outputImage, format, new File(destinationPath)); } catch (Exception e) { System.out.println("scaleImageWithParams方法压缩图片时出错了"); e.printStackTrace(); } } /** * 将double类型的数据转换为int,四舍五入原则 * * @param sourceDouble * @return */ private static int parseDoubleToInt(double sourceDouble) { int result = 0; result = (int) sourceDouble; return result; } /*** * * @param bufferedImage 要缩放的图片对象 * @param width_scale 要缩放到的宽度 * @param height_scale 要缩放到的高度 * @return 一个集合,第一个元素为宽度,第二个元素为高度 */ private static ArrayList<Integer> getAutoWidthAndHeight(BufferedImage bufferedImage,int width_scale,int height_scale){ ArrayList<Integer> arrayList = new ArrayList<Integer>(); int width = bufferedImage.getWidth(); int height = bufferedImage.getHeight(); double scale_w =getDot2Decimal( width_scale,width); System.out.println("getAutoWidthAndHeight width="+width + "scale_w="+scale_w); double scale_h = getDot2Decimal(height_scale,height); if (scale_w<scale_h) { arrayList.add(parseDoubleToInt(scale_w*width)); arrayList.add(parseDoubleToInt(scale_w*height)); } else { arrayList.add(parseDoubleToInt(scale_h*width)); arrayList.add(parseDoubleToInt(scale_h*height)); } return arrayList; } /*** * 返回两个数a/b的小数点后三位的表示 * @param a * @param b * @return */ public static double getDot2Decimal(int a,int b){ BigDecimal bigDecimal_1 = new BigDecimal(a); BigDecimal bigDecimal_2 = new BigDecimal(b); BigDecimal bigDecimal_result = bigDecimal_1.divide(bigDecimal_2,new MathContext(4)); Double double1 = new Double(bigDecimal_result.toString()); System.out.println("相除后的double为:"+double1); return double1; }}
第二个
import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.*;/** * @author wuyu * @versions 1.0 图片压缩工具类 提供的方法中可以设定生成的 * 缩略图片的大小尺寸等 */public class ImageZip { /** * 程序入口 * 将图片按照指定的图片尺寸压缩 * * @param srcImgPath * 源图片路径 * @param outImgPath * 输出的压缩图片的路径 * @param new_w * 压缩后的图片宽 * @param new_h * 压缩后的图片高 */ public static void compressImage(InputStream inputStream, String outImgPath, int new_w, int new_h) { System.err.println(outImgPath); BufferedImage src = InputImage(inputStream); disposeImage(src, outImgPath, new_w, new_h); } public static void main(String[] args) throws FileNotFoundException { String inpath="F:\\oldImage\\20155913wm5y.png"; String outImagepath="F:\\newImage\\123.png"; File file = new File(inpath); InputStream in = new FileInputStream(file); disposeImage(InputImage(in),outImagepath,100,200); } /** * 读取图片 */ private static BufferedImage InputImage(InputStream inputStream) { BufferedImage srcImage = null; try { srcImage = ImageIO.read(inputStream); } catch (IOException e) { System.out.println("读取图片文件出错!" + e.getMessage()); e.printStackTrace(); } return srcImage; } /** * 处理图片 * @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 */ private static void OutImage(String outImgPath, BufferedImage newImg) { // 判断输出的文件夹路径是否存在,不存在则创建 File file = new File(outImgPath); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } // 输出到文件流 try { ImageIO.write(newImg, outImgPath.substring(outImgPath.lastIndexOf(".") + 1), new File(outImgPath)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
阅读全文
1 0
- 压缩图片(三)
- Android应用开发之图片(Bitmap)压缩(三)---------采样率压缩
- Android应用开发之图片(Bitmap)压缩(三)---------采样率压缩
- 三种图片的压缩方式
- Android图片压缩的三种方法
- android图片压缩的三种方式
- 对于图片压缩的 三种方式
- 图片压缩的三种方法?
- 图片的三种压缩方法
- gulp-imagemin图片压缩----gulp系列(三)
- Android图片压缩的三种方式
- android图片压缩的三种方式
- android图片压缩的三种方式
- 三种Android图片压缩方法 压缩到指定大小
- 三种Android图片压缩方法 压缩到指定大小
- 压缩图片(-)
- 压缩图片(二)
- 图片压缩旋转大全(质量压缩,比列压缩)
- 情感分类语料库
- spring boot+RabbitMQ
- jQuery简单插件开发
- 一键格式化linux服务器上nginx.conf文件
- July.5一个简单的计算器程序
- 压缩图片(三)
- Mac CMake安装和使用
- URL、URN、URI区别
- 【Spring】Spring Cloud的思维导图
- PHP CURL实现GET、POST请求
- Redis的发布订阅
- html-webpack-plugin 中使用 title选项设置模版中的值无效
- 全面深入分析TCP的TIME_WAIT和CLOSE_WAIT(下)
- Android读书笔记之Activity的configChanges属性