java code implements picture processing functions. The picture quality compression.

java picture processing. Original article path: https://www.cnblogs.com/puqiuxiaomao/p/3854046.html

Requirements: browsing picture need to 1M bit.

1. The true picture of the quality of compressed (not high by modifying pictures, zoom out wide picture. It just reduced picture quality)

  Pros: do not modify the picture size, easy.

  Disadvantages: jpg format can handle well, for GIF , PNG Other formats are not suitable.

compressPic (image path, processing format) ;

1.	/**
2.	* 
3.	* 修改图片大小
4.	* <p>描述</p>
5.	* @date 2014-7-10 下午4:27:51
6.	* @version 
7.	* @param srcFilePath
8.	* @param fileExtName
9.	* @return
10.	* @throws IOException
11.	*/
12.	public static boolean compressPic(String srcFilePath,String fileExtName) throws IOException {
13.	File file = null;
14.	BufferedImage src = null;
15.	FileOutputStream out = null;
16.	ImageWriter imgWrier;
17.	ImageWriteParam imgWriteParams;
18.	
19.	
20.	long start1 = System.currentTimeMillis();
21.	// 指定写图片的方式为 jpg
22.	imgWrier = ImageIO.getImageWritersByFormatName(fileExtName).next();
23.	imgWriteParams = imgWrier.getDefaultWriteParam();
24.	// imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(
25.	// null);
26.	// imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(
27.	// null);
28.	
29.	
30.	imgWriteParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
31.	// 这里指定压缩的程度,参数qality是取值0~1范围内,
32.	imgWriteParams.setCompressionQuality((float) 0.2);
33.	//imgWriteParams.setProgressiveMode(ImageWriteParam.MODE_DISABLED);//
34.	ColorModel colorModel =ImageIO.read(new File(srcFilePath)).getColorModel();// ColorModel.getRGBdefault();
35.	// 指定压缩时使用的色彩模式
36.	
37.	imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(
38.	colorModel, colorModel.createCompatibleSampleModel(16, 16)));
39.	
40.	long end1 = System.currentTimeMillis();
41.	System.out.println("11111消耗时间:"+((double)end1-(double)start1)/1000+"秒");
42.	try {
43.	if (isBlank(srcFilePath)) {
44.	return false;
45.	} else {
46.	file = new File(srcFilePath);
47.	src = ImageIO.read(file);
48.	out = new FileOutputStream(srcFilePath);
49.	
50.	System.out.println("22222");
51.	imgWrier.reset();
52.	
53.	// 必须先指定 out值,才能调用write方法, ImageOutputStream可以通过任何
54.	// OutputStream构造
55.	imgWrier.setOutput(ImageIO.createImageOutputStream(out));
56.	System.out.println("3333333");
57.	// 调用write方法,就可以向输入流写图片
58.	
59.	long start4 = System.currentTimeMillis();
60.	imgWrier.write(null, new IIOImage(src, null, null),
61.	imgWriteParams);
62.	long end4 = System.currentTimeMillis();
63.	
64.	System.out.println("4444消耗时间:"+((double)end4-(double)start4)/1000+"秒");
65.	
66.	src.flush();
67.	out.flush();
68.	out.close();
69.	imgWrier.dispose();
70.	}
71.	} catch (Exception e) {
72.	e.printStackTrace();
73.	return false;
74.	}
75.	return true;
76.	}
77.	
78.	
79.	/**
80.	* 
81.	* 修改单独图片大小
82.	* <p>描述</p>
83.	* @date 2014-7-10 下午4:26:30
84.	* @version 
85.	* @param filePath
86.	* @param createType
87.	* @throws Exception
88.	*/
89.	public static void changeNoneImgSize(String filePath,String createType) throws Exception
90.	{
91.	File tempFile = new File(filePath);
92.	if(tempFile.length()>ImagesUtils.IMAGEMAXSIZE){
93.	
94.	long start = System.currentTimeMillis();
95.	
96.	
97.	compressPic(filePath,createType);
98.	long end = System.currentTimeMillis();
99.	
100.	
101.	System.out.println(filePath+"消耗时间:"+((double)end-(double)start)/1000+"秒");
102.	}
103.	
104.	}
105.	
106.	
107.	/**
108.	* 
109.	* 修改多个图片大小
110.	* <p>描述</p>
111.	* @date 2014-7-10 下午4:26:52
112.	* @version 
113.	* @param file
114.	* @throws Exception
115.	*/
116.	public static void changeManyImgSize(File file) throws Exception
117.	{
118.	try {
119.	// 判断文件是否是文件,如果是文件,获取路径,并计数
120.	if (file.isFile()) {
121.	String fileExtName = file.getName().substring( 
122.	(file.getName().lastIndexOf(".") + 1), file.getName().length()); 
123.	if(ImagesUtils.isImageFile(fileExtName))
124.	changeNoneImgSize(file.getAbsolutePath(), fileExtName);
125.	//ImagesUtils.changeImgSize(file.getAbsolutePath(), ImagesUtils.CREATENEWIMAGETYPE_6);
126.	} else {
127.	// 如果是文件夹,声明一个数组放文件夹和他的子文件
128.	File[] f = file.listFiles();
129.	// 遍历文件件下的文件,并获取路径
130.	for (File file2 : f) {
131.	changeManyImgSize(file2);
132.	}
133.	}
134.	} catch (RuntimeException e) {
135.	e.printStackTrace();
136.	}
137.	}

2. This need to use a java-image-scaling-0.8.5.jar package. This is necessary to set the width and height (I go according to the original ratio of width in accordance with the two. A4 , take the width) JAR me out here: http://pan.baidu.com/s/1c0pekVm

Pros: Simple, format support is also OK. I tested GIF , PNG can be used,

Disadvantages: width and height are needed.

1.	public static boolean compressPic(String srcFilePath,String fileExtName) throws IOException {
2.	
3.	
4.	try {
5.	BufferedImage sourceImage = ImageIO.read(new File(srcFilePath));
6.	int newwidth = 1700;
7.	double newheight = ((double) sourceImage.getHeight() / (double) sourceImage.getWidth()) * 1700;
8.	ResampleOp resizeOp = new ResampleOp(newwidth, (int) newheight);
9.	resizeOp.setFilter(ResampleFilters.getTriangleFilter());
10.	BufferedImage resizedImage = resizeOp.filter(sourceImage, null);
11.	ImageIO.write(resizedImage, "jpg", new File(srcFilePath));
12.	} catch (Exception e) {
13.	log.error("compressPic error", e);
14.	return false;
15.	}
16.	return true;
}

 

Guess you like

Origin blog.csdn.net/weixin_42123784/article/details/91505065