Java不修改尺寸压缩图片

Java不修改尺寸压缩图片

不修改图片的大小尺寸,压缩图片占用内存

1、使用thumbnailator库

支持所有格式图片,存在压缩后图片文件占用内存变大的情况

maven依赖

<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.8</version>
</dependency>

github源码:https://github.com/coobird/thumbnailator 

工具代码

/**
	 * 使用thumbnailator库,不修改图片尺寸,压缩图片占用内存
	 * 支持所有格式图片,存在压缩后图片文件占用内存变大的情况
	 * @param srcPath 原文件路径
	 * @param destPath 压缩后的文件路径
	 * @param quality 图片质量,取值范围[0, 1]
	 * @return
	 * @throws IOException 
	 */
	public static void compressImageByThumbnails (String srcPath, String destPath, float quality) throws IOException {
		Thumbnails.of(srcPath).scale(1f).outputQuality(quality).toFile(destPath);
	}

 

2、使用jdk

仅仅支持文件头是jpg格式图片(png格式图片压缩后背景颜色存在问题) 

工具代码

/**
	 * 使用JDK,不修改图片尺寸,压缩图片占用内存
	 * 仅仅支持文件头是jpg格式图片(png格式图片压缩后背景颜色存在问题)
	 * 存在压缩后图片文件占用内存变大的情况
	 * @param srcPath 原文件路径
	 * @param destPath 压缩后的文件路径
	 * @param quality 图片质量,取值范围[0, 1]
	 * @return
	 * @throws IOException
	 */
	public static boolean compressImageByJdk(String srcPath, String destPath, float quality) {
		boolean isSuccess = false;
		File image = new File(srcPath);
		if (image.exists()) {
			String imageType = null;
			try {
				imageType = getImageType(image);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			Iterator<ImageWriter> imageWriterIte = ImageIO.getImageWritersByFormatName(imageType);
			if (imageWriterIte != null && imageWriterIte.hasNext()) {
				
				ImageWriter writer = imageWriterIte.next();
				ImageWriteParam imgWriteParam;
				BufferedImage srcImage = null;
				FileOutputStream fos = null;
				try {
					//写图片参数
					imgWriteParam = writer.getDefaultWriteParam();
					//必须指定压缩方式为MODE_EXPLICIT
					imgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
					//设置图片质量
					imgWriteParam.setCompressionQuality(quality);
//					imgWriteParam.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
//					//图片颜色模式
//					ColorModel colorModel = ImageIO.read(image).getColorModel();
//					imgWriteParam.setDestinationType(new ImageTypeSpecifier(
//							colorModel, colorModel.createCompatibleSampleModel(16, 16)));
					
					fos = new FileOutputStream(destPath);
					writer.reset();
					writer.setOutput(ImageIO.createImageOutputStream(fos));
					srcImage = ImageIO.read(image);
					writer.write(null, new IIOImage(srcImage, null, null), imgWriteParam);
					isSuccess = true;
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					if (fos != null) {
						try {
							fos.flush();
							fos.close();
							writer.dispose();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}
		} else {
			throw new RuntimeException("文件不存在[ " + srcPath + "]");
		}
		return isSuccess;
	}

 测试代码

/**
	 * 测试图片压缩
	 * @throws IOException
	 */
	@Test
	public void testCompressImageByThumbnails() throws IOException {
		String imageName = "java_coffee.png";
		String srcPath = IMAGE_PATH + imageName;
		
		imageName = "java_coffee_compress.png";
		String destPath = IMAGE_PATH + imageName;
		
		ImageUtil.compressImageByThumbnails(srcPath, destPath, 0.5f);
		
		long srcImageLength = new File(srcPath).length();
		long destImageLength = new File(destPath).length();
		
		System.out.println(srcPath + ":" + srcImageLength);
		System.out.println(destPath + ":" + destImageLength);
		
	}
	
	/**
	 * 测试Jdk图片压缩
	 * @throws IOException
	 */
	@Test
	public void testCompressImageByJdk() throws IOException {
		String imageName = "java_coffee.jpg";
		String srcPath = IMAGE_PATH + imageName;
		System.out.println(ImageUtil.getImageType(new File(srcPath)));
		
		imageName = "java_coffee_compress.jpg";
		String destPath = IMAGE_PATH + imageName;
		Assert.assertTrue(ImageUtil.compressImageByJdk(srcPath, destPath, 0.5f));
		
		long srcImageLength = new File(srcPath).length();
		long destImageLength = new File(destPath).length();
		
		System.out.println(srcPath + ":" + srcImageLength);
		System.out.println(destPath + ":" + destImageLength);
		
	}

完整源码:https://github.com/ConstXiong/xtools

 


【Java面试题与答案】整理推荐

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值