Java不修改尺寸压缩图片

版权声明:转载请注名出处 https://blog.csdn.net/meism5/article/details/88407117

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

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

猜你喜欢

转载自blog.csdn.net/meism5/article/details/88407117